It’s a sad day when a web developer like me has to resort to writing batch files. Since I had to write some today, I guess that makes this a sad day?
Anyway, so I don’t forget, here is a sample script with some notes so I don’t completely forget what I did. The script below takes two file paths as input parameters, then generates a file name using some date manipulation so that file may be moved from the first file path to the second:
rem Clear any previous commands cls rem Hide output from showing unless ECHO is used @echo off rem Name: MoveIt.Bat rem rem Purpose: To copy a file from one directory to another. rem The file name is dynamically created in this script. rem rem Variables: rem %1 = The 'from' file path rem %2 = The 'to' file path rem rem Example: rem rem C:\moveit.bat “C:\downloads\“ “C:\uploads\“ rem rem Create filename for last month's report PDF set day=%date:~-7,2% set month=%date:~-10,2% set year=%date:~-2,2% if %month:~-2,1% equ 0 set month=%month:~-1,1% if %month% lss 1 goto error if %month% gtr 12 goto error set /a month=%month%-1 if %month% equ 0 set month=12 if %month% lss 10 set month=0%month% set reportfile=SomeFileName %month%%year%.pdf set reportpath=%1%reportfile% rem Move the web report PDF from path %1 to %2 if not exist %reportpath% goto nofile move %reportpath% %2 rem Create a text file for DIP importing rem ">" Over-writes the file, ">>" Appends to the file set dipfile=%2WEBDIP.txt rem Reset the dates in case they were modified above set day=%date:~-10,2% set month=%date:~-7,2% set year=%date:~-4,4% set currentdate=%day%/%month%/%year% rem Write the DIP file directly in the %2 path echo BEGIN >> %dipfile% echo DOCTYPE: doctype >> %dipfile% echo DATE: %currentdate% >> %dipfile% echo PATH: %reportfile% >> %dipfile% echo END >> %dipfile% :error if errorlevel 4 goto lowmemory if errorlevel 2 goto abort if errorlevel 0 goto exit :nofile echo The requested PDF report (%reportfile%) was not found goto exit :lowmemory echo Insufficient memory to copy files or echo invalid drive or command-line syntax. goto exit :abort echo You pressed CTRL+C to end the copy operation. goto exit :exit EXIT rem Use ‘pause’ to prevent the DOS window from closing rem pause
Note that this is my first batch file. If it's the worst thing you've ever seen, don't just sit there, help me out!