Comparing a portion or a file name to a directory name and then moving the file into the appropriate directory.

I am looking to compare a portion of a file name, to a directory name, and move the file that matches the directory name to that matching directory.  On top of that, I would also like the user to be able to append a user defined date onto any files it will be moving.
Here is a sample of what I am looking to accomplish using windows scripting (if possible):

Data files reside in \\server1\Share\

Sample File Names:  
(there will be hundreds of variations on the file names, possibly multiple files for one ###XXX)

Destination is \\server2\share\

Sample Directory Names (hundreds of directories):


I want the script to compare the first 6 characters of the file name in  \\server1\Share\, and then move it to the matching directory in \\server2\share\

At some point in this process, I think it may be easiest to do this while all the files are together on \\Server1\Share, I would like a user defined date to appended to the end of all file names.  
The final result would look something like this:

1. Prompt for the user to enter a date to append to all files at the end of the file name (before the extension)
2. Append UD Date and then compare the first 6 Characters of the file names on \\Server1\Share\ to existing directories on \\Server2\Share
3. Move the files to the appropriate directories
4. End Result:
(the '_' in between the file name and date is completely optional, just thought it looked cleaner)

If I could assign a billion points to this, I would.  Any help would be greatly appreciated.  I am familiar with windows scripting, I have used it quite a bit, but I dont use it enough to create something as complex (or so it seems) as this.

Thanks very much for your help in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paste the script below into a text file with a .cmd extension.  Customize the value of the source variable on line 4 with the location of the source directory.  Customize the value of the dest variable on line 5 with the location of the destination directory.  

Running the script will prompt for the date and then echo the move commands to be run.  Once you have tested it and are certain it will do what you intend, remove the word ECHO from line 13 to execute the commands.

@echo off
setlocal enabledelayedexpansion
set source=\\server1\Share
set dest=\\server2\share
set /P udate=Enter the date to append to file names: 
for /F "tokens=*" %%G in ('dir "%source%" /A:-D /B') do (
 set newname=%%~nG_%udate%%%~xG
 set folder=%dest%\!newname:~0,6!
 if not exist "!folder!" md "!folder!"
 ECHO move "%source%\%%G" "!folder!\!newname!"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JimNicholsonAuthor Commented:
Excellent, thank you so much.  Sorry it took so long for the reply, I was trying to break it.  Didn't happen, nice job and thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.