Page 1 of 1

Copy files to their relevant directory

Posted: 02 Apr 2009 01:27
by AnthonyCSC
Hi Dos Gurus,

I need help to copy files to their relevant directories.
The following files reside in one directory say for example C:\Test\workdir
20090402_000010.csv
20081201_000042.csv
20090301_000081.csv
20090301_000024.csv
20090328_000012.csv
and so on...

The first part of filename is date and the second part holds reference to a directory name where it should be copied into.
i.e. 000010, 000042, 000081 etc.,
If the file name is 20090402_000010.csv then that file should be copied to a directory called C:\Test\000010\indir
and similarly if the filename is 20081201_000042.csv then this will have to go to C:\Test\000042\indir
and so on...

The other possible directories are
C:\Test\000012\indir
C:\Test\000081\indir
C:\Test\000024\indir
C:\Test\000030\indir etc.,


Please advise how do I write a program to copy the files.
Thanks for your valuable time and help.

Posted: 02 Apr 2009 10:20
by RElliott63
You could try something like:

Code: Select all

For /F "Tokens=1,2,3* Delims=_." %%F In ('Dir /B \Test\WorkDir\*.csv') Do (
    Echo Copying %%F CSV file to %%G
    Copy /v %%F_%%G.%%H \Test\%%G\indir
)

Posted: 06 Apr 2009 02:54
by AnthonyCSC
RElliott63,

Thank you very much for answering my question and for sharing your excellent dos skills amongst us.