user2840573
user2840573

Reputation: 11

Batch file to move PDF files based on date, create folder if necessary

I'm relatively new to batch files. I can do VERY simple ones, however this next one is throwing me for a loop.

We have a program that generates PDF quotes for customers that saves in the following way...

FirstnameLastname_Estimate_MMDDYYYY-###

For example if I had a customer "John Doe" and we gave him an estimate (#239th in our system) on Christmas Eve this year it would look like this.

JohnDoe_Estimate_12242013-239.pdf

Right now it lives in a folder "C:\Estimates". What I'd like the batch file to do is look at all of the files in the "C:\Estimates" folder and automatically move the files sorted by year. So all of the 2012 estimates are put into a folder "C:\Estimates\2012", 2013 in "C:\Estimates\2013" etc.

Some of the batch files on here get me close, but no cigar. Any help would be great. Thanks!

Upvotes: 1

Views: 977

Answers (1)

foxidrive
foxidrive

Reputation: 41244

Launch this in the C:\Estimates folder. Filenames should not have _ or - in the client name.

@echo off
setlocal disabledelayedexpansion
for %%a in (*.pdf) do (
   for /f "tokens=3 delims=_-" %%b in ("%%a") do (
    set variable=%%b
       setlocal enabledelayedexpansion
         set variable=!variable:~-4!
         md !variable! 2>nul
         move "*!variable!-*.pdf" !variable! >nul
       endlocal
  )
)

Upvotes: 2

Related Questions