File Renaming and copying to a specific direc

September 4, 2009 at 13:46:11
Specs: Windows XP
Hi

I have a program that generates a number of PDF files with an individual's name contained in the filename, e.g employee file for john smith week 20.pdf. There would be several hundred separate files created each week.

What I want to be able to do is to transfer each file into a sub directory for each employee, creating the directory where it does not exist, and copy the file to another directory with the filename reduced to the employees name, i.e. john smith (all files in the second copy would be transferred to a single directory).

Forgive me if the answer is relatively simple but I am a novice at the compilation of code.

Thank you in anticipation


See More: File Renaming and copying to a specific direc

Report •


#1
September 4, 2009 at 16:56:17
Give some concrete examples of the employees PDF names. Do they follow some pattern?


Report •

#2
September 6, 2009 at 13:50:02
Thank you for your reply

The file names all have the same pattern for example

Employee record for J Smith (001) for week 20.pdf
Employee record for B Brown (012) for week 20.pdf
Employee record for C Church (027) for week 20.pdf

With a new file being created for each week

There is a directory D:\Employee Records where these files are initially saved.

Within this directory there would be a sub directory for each employee which would be named in accordance with the naming convention used for the initial pdf file.

There are two separate actions that need to be undertaken for each file:

1. A copy of the file needs to be made and saved in another directory, say D:\Employee Reports with the filename reduced to the employee's name, i.e.

J Smith (001).pdf
B Brown (012).pdf
C Church (027).pdf

2. All of the files in C:\Employee Records then need to be moved into the subdirectory for the individual employee, i.e.

From C:\Employee Records\Employee record for J Smith (001) for week 20.pdf

To C:\Employee Records\J Smith\Employee record for J Smith (001) for week 20.pdf

The number after the employee's name is essentially a unique employee number.

I hope this clarifies what I am trying to achieve

Regards

Rodger


Report •

#3
September 6, 2009 at 16:07:25
Rodger, I think is that.
I keep the employee number when creating employee directory because there may be more than one employee with the same name. Change it if you want.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /b empl*.pdf') do (
set file=%%a
call :copymove
)
goto :eof
:copymove
for /f "tokens=4-6 delims= " %%b in ("!file!") do (
copy /y "!file!" "Copied Files\%%b %%c %%d.pdf"
if not exist "%%b %%c %%d\" md "%%b %%c %%d"
move /y "!file!" "%%b %%c %%d\"
goto :eof
)
:: the bat has to be in the same folder as PDF's.


Report •

Related Solutions

#4
September 7, 2009 at 13:16:19
Ricardo

Thank you so much for your help, I set up a test on my local machine and whilst the files 'cut and paste' to the individual emploee directory (and create the directories) perfectly the part with the shortened file names (transferring the file with an abbreviated name) does not appear to work.

As you will have guessed I am new to this (and my age is not in my favour - too old) so if I could beg your indulgence - am I being stupid? I feel this is something I should have been able to figure out for myself so I apologise, but I have been looking at it for more than 1 hour to no avail.

Would you mind reviewing the code that you sent to me?

Thank you in grateful appreciation

Regards

Rodger


Report •

#5
September 7, 2009 at 14:11:25
Rodger, probably you have confused the string "Copied Files" in the batch as being a command. It is a subdirectory you should have created inside the PDF's folder.

So I will add a new line to the bat. It will create this subdirectory next time you run it (and recreate it for the case it got accidentaly deleted in the future).

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%a in ('dir /b empl*.pdf') do (
set file=%%a
call :copymove
)
goto :eof
:copymove
for /f "tokens=4-6 delims= " %%b in ("!file!") do (
if not exist "Copied Files\" md "Copied Files"
copy /y "!file!" "Copied Files\%%b %%c %%d.pdf"
if not exist "%%b %%c %%d\" md "%%b %%c %%d"
move /y "!file!" "%%b %%c %%d\"
goto :eof
)
:: the bat has to be in the same folder as PDF's.

--------
I think is this. If not, let me know. I am practicing to achieve skill in bats and I don't get bored if helping you would take me some more time of practice.


Report •

#6
September 8, 2009 at 13:21:07
Ricardo

You are a star! ...it works perfectly... you are correct, I was confused by the string "Copied Files"

Could I possibly ask for an additional refinement?

Would it be possible to delete all the files in the 'Copied Files' directory before the files are transferred? The files from this directory are being emailed and I had thought at the start (to avoid old files being sent repeatedly if an employee left) that we would need to delete the files manually.

If you are looking to develop your skills I may have another task, if you could help. If you are willing to assist do I need to post another problem or can we continue this thread.

Again, my sincere thanks for what you have done to date

Regards

Rodger


Report •


Ask Question