Skip to content

Instantly share code, notes, and snippets.

@dpraimeyuu
Forked from jennings/BatchfileTricks.cmd
Created June 15, 2020 07:19
Show Gist options
  • Save dpraimeyuu/ed54a4db29c290a2fc7a06eb8fe91edf to your computer and use it in GitHub Desktop.
Save dpraimeyuu/ed54a4db29c290a2fc7a06eb8fe91edf to your computer and use it in GitHub Desktop.
Stupid batch file tricks
:: Sleep for N seconds:
ping localhost -n N -w 1
:: Get the date and time (US LOCALE ONLY)
set YEAR=%DATE:~-4,4%
set TWOYEAR=%DATE:~-2,2%
set MONTH=%DATE:~-10,2%
set DAY=%DATE:~-7,2%
set HOUR=%TIME:~0,2%
set MINUTE=%TIME:~3,2%
set SECOND=%TIME:~6,2%
:: Remove leading zeroes from a number (with a known maximum):
set DAYOFWEEK=06
set /a DAYOFWEEK=7%DAYOFWEEK% %% 7
:: Call a subroutine
call :SubroutineName ParamOne "ParamTwo"
:SubroutineName
echo This is a subroutine!
echo Arg1=%~1
echo Arg2=%~2
goto :EOF
:: Exit a subroutine batch file without killing the outer batch file
exit /b
:: Send the last line of output to a variable
for /F "delims=" %%A in ('uuidgen.exe') do set MYVAR=%%A
:: Generate a guid and store it in %GUID%
for /F "delims=" %%A in ('powershell -command "$([guid]::NewGuid().ToString())"') do set GUID=%%A
:: Command line arguments
%0 -- File name of the running batch file
%* -- All command line arguments, as entered
%1 -- First command line argument
%~1 -- First command line argument with surrounding quotes removed
%~f1 -- First command line argument as a fully qualified path name
%~d1 -- First command line argument as a drive letter
%~p1 -- First command line argument as a path
%~n1 -- First command line argument as a file name
%~x1 -- First command line argument as a file extension
%~s1 -- First command line argument as a short file name
%~a1 -- First command line argument as file attributes
%~t1 -- First command line argument as a time and date of file
%~z1 -- First command line argument as a file size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment