Windows Batch File Notes

Script Header

Put this at the top of all your scripts. More details here on StackOverflow.


Demo of file manipulation

Put the code below into a batch file and run it. It will show you details about the file to help you understand these commands

echo off
echo %~n0 is the file name of this file
echo %~x0 is the extension of this file
echo It is on the %~d0 drive
echo In the %~p0 path
echo To be complete we can write the whole thing:
echo %~dp0%~nx0
echo or if we don't want the extension:
echo %~dp0%~n0


::This code will grab the current monthe and store it in logMonth. Also today's date and store it in logDay.
set logMonth=%date:~4,2%
set logDay=%date:~7,2%
::This code will grab the 4 digit year and store it in logYear. Also the 2 digit year will be stored in twologyear.
set logYear=%date:~10,4%
set twologyear=%date:~12,2%
::Now we can convert the month number that we stored in logMonth into text
if %logMonth%==01 SET logMonthtxt=JAN
if %logMonth%==02 SET logMonthtxt=FEB
if %logMonth%==03 SET logMonthtxt=MAR
if %logMonth%==04 SET logMonthtxt=APR
if %logMonth%==05 SET logMonthtxt=MAY
if %logMonth%==06 SET logMonthtxt=JUN
if %logMonth%==07 SET logMonthtxt=JUL
if %logMonth%==08 SET logMonthtxt=AUG
if %logMonth%==09 SET logMonthtxt=SEP
if %logMonth%==10 SET logMonthtxt=OCT
if %logMonth%==11 SET logMonthtxt=NOV
if %logMonth%==12 SET logMonthtxt=DEC

Now, if today is January 23, 2017, we can get 23JAN2017 by:



Note: I couldn't use the code format for the examples below because %% causes issues with Dokuwiki

In this next example, the . represents the directory out batch file is in. FTPDownloads is a sub-directory of the current one. For example if our current directory c:\downloads then .\FTPDownloads would be the equivalent of c:\downloads\FTPDownloads.

Here we are looking into the sub-directory FTPDownloads and looking at all zip files and looping through them. %%f will return the full path to the zip file.

for %%f in (.\FTPDownloads\*.*) do (
    echo %%f

In this next example, given a zip file called, the delims=_ part tells the process that _ is our delimiter in the FILE NAME of %%f (%%~nf is what limits it to the file name) and to return everything BEFORE the FIRST _ as %%a.

The echo part of the commend appends (>> appends > creates new or overwrites) comma separated values to a log file.

for %%f in (.\FTPDownloads\*.zip) do (
    FOR /f "delims=_" %%a IN ("%%~nf") do (
        echo %%~nf,%date%,%time% >> "%source%\logs\%logYear%%logMonth%_ftplog.csv"

Delete Empty Folders

The code below will delete any empty folders in the FTPDownloads sub-directory

:: Delete empty folders
for /f "delims=" %%d in ('dir /s /b /ad "%source%\Process\Medpace\Archive" ^| sort /r') do rd "%%d" 2>nul

Schedule Batch to Run Invisible

You can easily schedule a batch file to run using Windows Task Schedule but if you want it to run and not interrupt your work you will want to run it invisible. To do that, you need to call it with another first. Here are the steps.

Copy the code below and save it into a file called invisible.vbs

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Next, open Windows Task Scheduler

  • Under Action, click Create Task
  • On the General tab, name your task something that will make sense to you later
  • Click the Triggers tab and click new. Adjust the times to be when you want the program to run
  • Click Actions and then click new
    • Next to Action select Start a program
    • Under Program script, enter wscript
    • In Add arguments enter: “C:\YourPath\invisible.vbs” “C:\YourPath\YourBatchFile.bat”
      • Be sure to enter the full path to both items and enclose them in quotes
    • Save the task and enter your password if prompted