Link to home
Start Free TrialLog in
Avatar of failed
failed

asked on

Copy files based on file name script

Hi Experts

I am looking for a script to run under Windows XP/Server 2003 which will copy and move files to a location determined by their name and alter their file extension.

Basically It has to be triggered by a .bat file but I can call a VBScript etc from within the batch

example:
in c:\testing\
12345678_ordrsp.128475
12345678_ordrsp.178548
56789101_ordrsp.787955

Two variables are:

%customer%
%messagetype%

These are as follows in example:
12345678 and 56789101 are the %customer%, these are always the first characters before the first underscore

ordrsp is the %messagetype% - these are always the 6 characters after the first _


Step 1
Files should end up with the . changed to _ and having .xml at the end
12345678_ordrsp_128475.xml
12345678_ordrsp_178548.xml
56789101_ordrsp_787955.xml

Step 2
Copy the files to c:\testing\backup\%customer%\%messagetype%

Step 3
Move the files to c:\testing\outbound\%customer%\%messagetype%

Not essential but if the folders with the values behind %customer% and %messagetype% did not exist then it would be useful the script would create them.

HELP :)

ps. To get the examples uploaded I had to rename them .bmp as EE wont allow the extension types .128475 etc



testing.zip
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this batch script...

CopyFiles.cmd has to be stored in a different path (not in c:\testing)!!!
@echo off
set BasePath=C:\TESTING
FOR /F "tokens=1,2,3 delims=_." %%A IN ('DIR %BasePath% /B /A-D') DO (
  if not exist %BasePath%\backup\%%A md %BasePath%\backup\%%A
  if not exist %BasePath%\backup\%%A\%%B md %BasePath%\backup\%%A\%%B 
  copy c:\testing\%%A_%%B.%%C c:\testing\backup\%%A\%%B\%%A_%%B_%%C.xml
)

Open in new window

CopyFiles.cmd
Avatar of Bill Prew
Bill Prew