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?
Shift-3Connect With a Mentor Commented:
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

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.