Page 1 of 1

Copying files based on text in argument

Posted: 10 Jun 2009 08:39
by scuba
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.

Posted: 10 Jun 2009 12:01
by avery_larry
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%"