Wednesday, September 11, 2019

pdf - Batch Script to move and rename if existing

I have a folder that contains thousands of .pdfs each file named by a 5 or 6 digit account number. I have a script that will create a folder of the same account number and move the file into it.


echo ON
Title Move files Routine
setlocal enabledelayedexpansion
pushd D:\test1\source\
for /f "tokens=*" %%1 in ('dir /a-d /b D:\test1\source\*.pdf') do (
set filename=%%1&set dirname=!filename:~0,6!
if not exist D:\test1\source\!dirname! (md D:\test1\source\!dirname!)
move %%1 D:\test1\source\!dirname!\
)
PAUSE

However, I need to run this script daily as new filed are added to the source directory and if the script moves a file to a directory that already has a file of the same name, it will overwrite it instead of renaming it. How can I tweak my script to address this problem. My options appear to be



  1. append to existing file (this seems to require additional software)

  2. Rename (e.g. 123456.pdf to 123456_001.pdf)

  3. Add date to filename (e.g. 123456.pdf to 123456_MMMMddyyyy.pdf)


I am fine with any of these options, I just don't know how to put that in the script. Any help is much appreciated.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...