This page contains a collection of some really short samples of frequently asked functions in batch files.
| Function | Batch Code | Remarks |
|---|---|---|
| Batch file's directory | ECHO "%~dp0" | Shows the batch file's directory with trailing backslash (credits: Sean Brannery) |
| Batch file's directory (SFN) | ECHO "%~dps0" | Shows the batch file's directory as Short File Name (8.3 notation) |
| Battery icon in notification area | POWERCFG /GlobalPowerFlag ON /Option:BatteryIcon | Will even work on desktops and servers |
| Beep | ECHO ˆG | ˆG is Ctrl+G (ASCII character 7) |
| Count files | DIR /A | FIND "File(s)" | Counts the number of files in the current directory. Will show its result like this: 47 File(s).Language dependent, FIND filter shown is
for English Windows versions only. |
| Count files | DIR /A-D /B | FIND /C /V "" | Counts the number of files in the current directory. Will show its result like this: 47.Language independent. |
| Count files (1) | XCOPY * "%TEMP%" /H /L /Q /S /Y | Counts the number of files in the current directory. Will show its result like this: 47 File(s).Language independent. Credits: Andrew J. Macbeth |
| Count files in subfolders | FOR /D %%A IN (*) DO ( ECHO.%%A DIR "%%~A" /A-D /B /S | FIND /C /V "" ) |
Counts the number of files in each subdirectory. Will show its result like this: My PicturesLanguage independent. |
| Count files in subfolders (1) | FOR /D %%F IN (*) DO ( ECHO "%%F" XCOPY "%%F\*" "%TEMP%" /H /L /Q /S /Y ) |
Counts the number of files in each subdirectory. Will show its result like this: "My Pictures"Language independent. Credits: Andrew J. Macbeth |
| Count files in subfolders (1) | FOR /D %%F IN (*) DO ( FOR /F %%C IN ('XCOPY "%%F\*" "%TEMP%" /H /L /Q /S /Y') DO ( ECHO.%%C File(s) %%F ) ) |
Counts the number of files in each subdirectory. Will show its result like this: 47 File(s) My Pictures.Language independent. Credits: Andrew J. Macbeth |
| Count files in subfolders | FOR /D %%A IN (*) DO ( FOR /F %%B IN ('DIR "%%~A" /A-D /B /S ˆ| FIND /C /V ""') DO ( ECHO.%%B File(s) %%A ) |
Counts the number of files in each subdirectory. Will show its result like this: 47 File(s) My PicturesLanguage independent. |
| Count files modified since ... (1) | XCOPY * "%TEMP%" /H /L /S /Q /Y /D:12-29-2005 | Counts all files modified since December 29, 2005, in the
current directory and subdirectories. Will show its result like this: 13 File(s)Regardless of language or international settings, the date format for XCOPY's /D switch is alway
M-D-Y. |
| Count files modified before ... (1) | FOR /F %%A IN ('XCOPY * "%TEMP%" /H /L /S /Q /Y /D:12-29-2005') DO ( FOR /F %%B IN ('XCOPY * "%TEMP%" /H /L /S /Q /Y') DO ( SET /A OldFiles = %%B - %%A ) ) ECHO.%OldFiles% File(s) |
Counts all files modified before December 29, 2005, in the
current directory and subdirectories. Will show its result like this: 34 File(s)Regardless of language or international settings, the date format for XCOPY's /D switch is alway
M-D-Y. |
| Counter | SET /A Counter += 1 | Increments the variable %Counter% by 1;
if %Counter% is not defined, its starting
value is 0 |
| Current Directory | CD | Displays the current directory |
| Current Directory | ECHO "%CD%" | Displays the current directory in double quotes |
| Current Directory (SFN) | FOR %%A IN ("%CD%") DO ECHO.%%~sA | Displays the current directory as Short File Name (8.3 notation) |
| Current Drive | FOR %%A IN (%CD%) DO ECHO.%%~dA | Displays the current drive letter (or \\ for UNC paths) |
| Date | DATE /T | |
| Date | ECHO.%Date% | |
| Date | FOR %%A IN (%Date:/=%) DO SET Today=%%A | Removes forward slashes and day of week, so the result can be used in a file name |
| Date (YYYYMMDD format) | FOR %%A IN (%Date%) DO ( FOR /F "tokens=1-3 delims=/-" %%B in ("%%~A") DO ( SET Today=%%D%%B%%C ) ) |
Stores current date in YYYYMMDD format variable %TODAY%.
Code shown is for (US) systems with date in MM/DD/YYYY format.
Use %%D%%C%%B for systems with date in DD-MM-YYYY format. (Credits: Matthew C. Miller) |
| Date | VER | DATE | FIND /V "(" | Will also show the text "The current date is" and, depending on OS version and regional settings, the day of the week |
| Date and Time in file name | REN *.LOG "* %Date:/= % %Time::=.%.*" | Will append the current date and time to all log file names, replacing forward slashes by spaces and colons by dots |
| Date and Time in file name | FOR %%A IN (%Date%) DO ( FOR /F "tokens=1-3 delims=/-" %%B IN ("%%~A") DO ( SET /A Today = 10000 * %%D + 100 * %%B + %%C ) ) FOR /F "tokens=1,2 delims=:.," %%E IN ("%Time%") DO ( SET /A Now = 10000 + 100 * %%E + %%F ) REN *.LOG "*_%Today%_%Now:~1%.*" |
Will append the current date and time to all log file names, with the date in YYYYMMDD format and the time in HHmm format. The code shown is for systems with the date in MM/DD/YYYY format, change the order of %%B, %%C and %%D for other date settings. |
| Drive letters in use | FSUTIL FSINFO DRIVES | Lists all drive letters in a single line on screen;
however, to parse that line with
FOR /F
you'll need MORE /E /T0
to remove vertical tabs or orphaned line feeds,
as demonstrated in my own
DrivUsed.bat |
| File size | FOR %%A IN (filename.ext) DO SET FileSize=%%~zA | Will store the size of filename.ext into variable %FileSize% |
| Hexadecimal to Decimal | SET /A DecValue = 0x%HexString% | Maximum value 65536 (216) |
| IP Address | FOR /F "tokens=2 delims=[]" %%A IN ('PING %ComputerName% -n 1') DO ( SET IP=%%A ) |
Subtitute %ComputerName% with a remote computer name
to get that computer's IP address |
| Jump to batch file's directory | PUSHD "%~dp0" | Will map a network drive if the batch file was started from a UNC path, and
then make the batch file's location the current (or working) directory. Use POPD before closing the batch file, otherwise the mapping will persist. (Credits: Reinhard Irnberger) |
| Linefeed | ECHO. | Actually a carriage return/linefeed combination; displays an empty line |
| List directories in PATH | FOR %%A IN (%PATH%) DO ECHO.%%A | Will fail if directory entries contain spaces |
| List files modified since ... (1) | XCOPY * "%TEMP%" /H /L /S /Y /D:12-29-2005 | Will lists all files modified since December 29, 2005, in the
current directory and subdirectories. Regardless of language or international settings, the date format for XCOPY's /D switch is alway
M-D-Y. |
| Math | SET /A | Positive integers only |
| Check if a value is numeric | FOR /F "tokens=1 delims=0123456789" %%A IN ("%Value%") DO ( ECHO.%Value% is NOT a valid decimal, octal or binary number ) |
Positive numbers only, no hexadecimal. If the value is numeric the FOR loop won't iterate, because %VALUE% will contain nothing but delimiters. Use constructs like SET Invalid=1 inside the loop if you want
to use the result outside the loop. |
| Check if a value is numeric | FOR /F "tokens=1 delims=-0123456789abcdefABCDEF" %%A IN ("%Value%") DO ( ECHO.%Value% is NOT a number ) |
All numbers, positive or negative, including hexadecimal without "x" or "0x" prefix. "False positive" on strings like "0FA7-A33E-7049-E444-1DEA". |
| Octal to Decimal | SET /A DecValue = 0%OctalString% | Maximum value 65536 (216) |
| Short File Name | FOR %%A IN (filename.ext) DO ECHO.%%~sA | Shows filename.ext's Short File Name (8.3 notation) |
| Short File Name | FOR %%A IN (filename.ext) DO ECHO.%%~sfA | Shows filename.ext's fully qualified path as Short File Name (8.3 notation) |
| String Substitution | SET NewString=%OldString:old=new% | Substitutes "old" with "new" everywhere in the string %OldString%; case sensitive! |
| Time | ECHO.%Time% | Hours, minutes, seconds and hundredths of seconds |
| Time | FOR /F "tokens=1-3 delims=:.," %%A IN ("%Time%") DO ( SET Time=%%A:%%B:%%C ) |
Hours, minutes and seconds |
| Time | TIME /T | Hours and minutes only |
| Time | VER | TIME | FINDSTR /R /C:"[0-9]" | Hours, minutes, seconds and hundredths of seconds, prefixed by "The current time is" |
| Open Windows Update in Internet Explorer | WUPDMGR | |
| Wipe an empty partition or the empty space on a partition | > wipe.txt ECHO AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA FOR /L %%A IN (1,1,1000) DO (TYPE wipe.txt >> wipe.txt) DEL wipe.txt |
This may take some time... |
| Open .ZIP file in Explorer | RUNDLL32.EXE ZIPFLDR.DLL,RouteTheCall zipfile.ZIP | Unfortunately, there seems to be no (native) command to copy files into the .ZIP file |
| Notes: | (1) | When using XCOPY * "%TEMP%".../S
on root directory of the drive where the %TEMP% directory
is located, you may get an error message stating
Cannot perform a cyclic copy.That's because the XCOPY command tries to
copy the contents of the root directory and all its
subfolders, including %TEMP%, to %TEMP%.To prevent this, you may either use a directory on a different drive as an alternative to %TEMP% (the /L switch prevents anything from actually
being copied anyway), or use XCOPY's
/EXCLUDE switch and exclude the %TEMP%
directory. |