Windows – DOS Command Lines Move files to folders named with previous month

command linetask schedulerwindows

I am trying to create a task in the Task Scheduler that will automatically move some pdf files into another folder that is based on the date in the pdf files which is for the previous month. The problem I am having is that I can't figure out how to get the command line to roll back to the previous month. I am using the same thing to create the folder to begin with, but the task is set to run at the end of what would be considered the previous month. So for example one task running in October to create the folder for current month 2010-10 and another run in November after the PDFs are compiled that will move them to the 2010-10 folder. Any ideas?

The date parameters I am using are "%DATE:~0,2%"
I have tried "%DATE:~0,2% -1" and "%DATE:~0,2%-1"
and the full text in the batch file is "move [-Y] *.pdf P:\CBO\Physician Monthly Reports\%DATE:~6,4%-%DATE:~0,2% -1"

Best Answer

Your character selection requires that your date format looks like "MM-DD-YYYY". This command retrieves the number of the current month and subtracts one and saves it in a variable called prevmonth. It also subtracts one from the year if the previous month is December:

set /a prevmonth=%DATE:~0,2% - 1
set yearprevmo=%DATE:~6,4%
if %prevmonth% equ 0 set /a yearprevmo=%yearprevmo%-1 & set prevmonth=12
move -Y *.pdf "P:\CBO\Physician Monthly Reports\%yearprevmo%-%prevmonth%"

Note the quotes since there are spaces in your directory name.

Related Question