Skip to content

Microsoft Command Prompt Batch File

Microsoft Command Prompt Batch Files are the oldest of old-school methods of scripting in Microsoft DOS and Windows.

Much maligned these days as too old to be useful, however, the command-line interpreter (cmd.exe) is still available (if not installed by default) on all current Window OS versions, including all the server variants (as of the year 2020, when this was written).

I do have to admit, the language, structure and how it accomplishes it's tasks do make it look a bit incomprehensible. Especially since no other programming language is based on it, and (to my ability to research) is not based on any other language used at the time of it's original creation with MS-DOS, as it's really just a file containing related Command Prompt commands. This makes it somewhat difficult to learn, sometimes surprising, but also very powerful as it runs as close to the bottom of the code stack as is possible without going binary.

If you want to learn more about Batch Files, I highly recommend Rob van der Woude's excellent documentation and examples found on his website.

Here are a few of the batch files I still have copies of, and I hope they might be of use to you.


This batch file shows off all the color combinations you can set the Command Window to.

@echo off
set NUM=0 1 2 3 4 5 6 7 8 9 A B C D E F
for %%x in (%NUM%) do ( 
    for %%y in (%NUM%) do (
            color %%x%%y
        echo background color: %%x
        echo text color: %%y
            timeout 5 


This batch file deletes all Apple trash bins from the specified drive.

IF NOT EXIST B:\.Spotlight-V100 GOTO Step2
RD /S /Q B:\.Spotlight-V100

IF NOT EXIST B:\.Trashes GOTO Step3
RD /S /Q B:\.Trashes

IF NOT EXIST B:\._.Trashes GOTO End
DEL /F /Q /A:AHRS "B:\._.Trashes"



This batch file deletes all files from a folder that are older than an input date.

@echo off

for /f "tokens=2" %%i in ('date /t') do set thedate=%%i

set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%

set /A dd=%dd% - %OLDERTHAN%
set /A mm=%mm% + 0

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

goto ERROR

set /A dd=31 + %dd%
goto DONE

set /A dd=30 + %dd%
goto DONE

set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

set /A dd=28 + %dd%
goto DONE

set /A dd=29 + %dd%

if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (*.*) do (
set FileName=%%i
call :PROCESSFILE %%~ti

set mm=
set yyyy=
set dd=
set thedate=
goto EXIT

ECHO   Where X is the number of days previous to Today.
ECHO EX: "DELOLD 5" Deletes files older than 5 days.

set temp=%1
set fyyyy=20%temp:~6%
set fmm=%temp:~0,2%
set fdd=%temp:~3,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%

:: +*************************************+
:: | This is where the files are deleted |
:: | Change the ECHO command to DEL to   |
:: | delete. ECHO is used for test.      |
:: +*************************************+
if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
ECHO %FileName%

set temp=
set fyyyy=
set fmm=
set fdd=


:: ----------END-DELOLD.BAT-------------


This batch file merges all files in a folder with a specified file extension into a new file.

::To merge all .csv files in a single folder to a single .csv:

for %F in (*.csv) do type %F>>AllData.csv


This batch file renames all files in a folder to include the current date.


:: Output system date
ECHO The system date is    "%date%"

:: Create new variable 'mydate' formatted as 'YYYYMMDD' using the environment variable 'date' formatted as 'ddd MM/DD/YYYY'
:: FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate=%%I-%%J-%%K
FOR /F "tokens=1-3 delims=/ " %%I IN ('DATE /t') DO SET mydate=%%K%%I%%J

:: Output formatted date
ECHO The formatted date is "%mydate%"

:: Copy file using formated date
copy c:\new.txt c:\file-%mydate%.txt

::pause  to see the output :-)