Copying files based on text in argument

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
scuba
Posts: 1
Joined: 10 Jun 2009 07:59

Copying files based on text in argument

#1 Post by scuba » 10 Jun 2009 08:39

I have several text files that arrive in a folder for processing. once these files arrive, I need to use a batch file to identify certain information from the filename and copy that file to a specific folder named after that file.

Sample scenario:

1) "ABCD_1234_a1b2c3d4_AB1CD2.txt.gz" arrives in folder "C:\Drop Off\".
2) MoveFile.bat is called with the parameter: "C:\Drop Off\ABCD_1234_A1B2C3D4_AB1CD2.txt.gz"
3) Grab "A1B2C3D4" from the filename
4) Test for folderpath "C:\Test Data\A1B2C3D4\". If it doesn't exist, create it.
5) Test for the filename "C:\Drop Off\ABCD_1234_A1B2C3D4_AB1CD2.txt.gz". If exists, exit batch, else copy the file. I disregard the filedate & other attributes since files have a timestamp within the filename.


Any suggestions on how this would be accomplished is appreciated.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 10 Jun 2009 12:01

Assuming a standard filename convention of:

something_somethingelse_whatyouwant_somethingelseagain.txt.gz

then start with this:

Code: Select all

@echo off

for /f "tokens=3 delims=_" %%a in ("%~1") do set "dirname=%%a"

md "c:\test data\%dirname%" >nul 2>nul
if exist "c:\test data\%dirname%\%~nx1" goto :eof
copy "%~1" "c:\test data\%dirname%"

Post Reply