DEVenv/win_helpers.bat

155 lines
4.6 KiB
Batchfile
Raw Normal View History

2022-07-08 14:40:23 +00:00
@echo off
setlocal EnableDelayedExpansion
2022-08-23 12:12:39 +00:00
REM Win Helpers - Version 10
2022-07-08 14:40:23 +00:00
call %*
goto exit
:DownloadFile
REM call win_helpers.bat :DownloadFile <url> <dest>
REM ------------------------------------------------------------------------------------------------
set url=%~1
set dest_file=%~2
if exist "!dest_file!" (
echo - [DownloadFile/Cached] !url! to !dest_file!
) else (
echo - [DownloadFile] !url! to !dest_file!
call powershell -NoLogo -NoProfile -NonInteractive -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest !url! -OutFile !dest_file! -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox"
)
if exist "!dest_file!" (
exit /B 0
) else (
2022-07-10 07:25:29 +00:00
echo [DownloadFile] Download failed [url=!url!]
2022-07-08 14:40:23 +00:00
exit /B 1
)
:OverwriteCopy
REM ------------------------------------------------------------------------------------------------
set src_file=%~1
set dest_file=%~2
if not exist "!src_file!" (
echo - [OverwriteCopy] File to copy does not exist [file=%src_file%]
exit /B 1
)
echo - [OverwriteCopy] !src_file! to !dest_file!
call copy /Y !src_file! !dest_file! > nul
exit /B 0
:Unzip
REM call win_helpers.bat :Unzip <path/to/7z.exe> <zip_file> <dest>
2022-07-12 13:37:15 +00:00
REM Overwrite mode: "-aos" Skip extracting of existing files
2022-07-08 14:40:23 +00:00
REM ------------------------------------------------------------------------------------------------
set zip7_exe=%~1
set zip_file=%~2
set dest=%~3
if not exist "!zip7_exe!" (
echo - [Unzip] Failed, 7zip exe not found [path=%zip7_exe%]
exit /B 1
)
if not exist "!zip_file!" (
echo - [Unzip] Failed, zip to unzip does not exist [path=%zip_file%]
exit /B 1
)
2022-07-12 13:37:15 +00:00
echo - [Unzip] !zip_file! to !dest!
call !zip7_dir!\7z.exe x -y -aos -spe -o!dest! !zip_file!
exit /B %ERRORLEVEL%
2022-07-08 14:40:23 +00:00
:FileHashCheck
REM call win_helpers.bat :FileHashCheck [sha256|md5|...] <file> <hash>
REM ------------------------------------------------------------------------------------------------
set algorithm=%~1
set file=%~2
set expected=%~3
if not exist "!file!" (
echo - [FileHashCheck] File does not exist [file=%file%]
exit /B 1
)
REM Calculate hash
2022-08-01 05:15:44 +00:00
for /F %%c in ('powershell -NoLogo -NoProfile -NonInteractive "Get-FileHash -algorithm !algorithm! \"!file!\" | Select-Object -ExpandProperty Hash "') do ( set "actual=%%c" )
2022-07-08 14:40:23 +00:00
REM Verify Hash
if /I "!expected!" neq "!actual!" (
2022-07-10 05:44:05 +00:00
echo - [FileHashCheck] !algorithm! failed [file=!file!,
echo expect=!expected!,
echo actual=!actual!
echo ]
2022-07-08 14:40:23 +00:00
exit /B 1
)
2022-07-10 05:44:05 +00:00
echo - [FileHashCheck] !algorithm! OK [file=!file! hash=!expected!]
2022-07-08 14:40:23 +00:00
exit /B 0
2022-07-12 13:37:15 +00:00
:MoveDir
REM call win_helpers.bat :MoveDir <src> <dest>
2022-07-08 14:40:23 +00:00
REM ------------------------------------------------------------------------------------------------
set src=%~1
set dest=%~2
if not exist "!src!" (
2022-07-12 13:37:15 +00:00
echo - [MoveDir] Directory does not exist [dir=%src%]
2022-07-08 14:40:23 +00:00
exit /B 1
)
2022-07-12 13:37:15 +00:00
echo - [MoveDir] "!src!" to "!dest!"
robocopy "!src!" "!dest!" /E /MOVE /MT /NP /NJS /NS /NC /NFL /NDL
2022-07-08 14:40:23 +00:00
exit /B 0
:MakeBatchShortcut
2022-08-23 12:12:39 +00:00
REM call win_helpers.bat :MakeBatchShortcut <name> <exe> <dest_dir>
2022-07-08 14:40:23 +00:00
REM ------------------------------------------------------------------------------------------------
REM NOTE we make a batch file instead of a symlink because symlinks require
REM admin privileges in windows ...
set name=%~1
set executable=%~2
set dest_dir=%~3
if not exist "!executable!" (
2022-08-23 12:12:39 +00:00
echo - [MakeBatchShortcut] Executable for shortcut does not exist [exe=%executable%]
exit /B 1
2022-07-08 14:40:23 +00:00
)
if not exist "!dest_dir!" (
echo - [MakeBatchShortcut] Shortcut destination directory does not exist [dir=%dest_dir%]
2022-08-23 12:12:39 +00:00
exit /B 1
2022-07-08 14:40:23 +00:00
)
2022-07-10 05:44:05 +00:00
echo - [MakeBatchShortcut] Create [name=!name!, exe=!executable!, dest=!dest_dir!]
2022-07-08 14:40:23 +00:00
echo @echo off> "!dest_dir!\!name!.bat"
echo !executable! %%*>> "!dest_dir!\!name!.bat"
exit /B 0
2022-08-23 12:12:39 +00:00
:MakeRelativeBatchShortcut
REM call win_helpers.bat :MakeRelativeBatchShortcut <name> <exe> <dest_dir>
REM ------------------------------------------------------------------------------------------------
REM NOTE we make a batch file instead of a symlink because symlinks require
REM admin privileges in windows ...
set name=%~1
set executable=%~2
set dest_dir=%~3
if not exist "!dest_dir!\!executable!" (
echo - [MakeRelativeBatchShortcut] Executable for shortcut does not exist [exe=!dest_dir!\%executable%]
exit /B 1
)
if not exist "!dest_dir!" (
echo - [MakeRelativeBatchShortcut] Shortcut destination directory does not exist [dir=%dest_dir%]
exit /B 1
)
echo - [MakeRelativeBatchShortcut] Create [name=!name!, exe=!dest_dir!\!executable!, dest=!dest_dir!]
echo @echo off> "!dest_dir!\!name!.bat"
echo %%~dp0!executable! %%*>> "!dest_dir!\!name!.bat"
exit /B 0
2022-07-08 14:40:23 +00:00
:exit
exit /B