Copy files to their relevant directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AnthonyCSC
Posts: 4
Joined: 01 Apr 2009 10:47

Copy files to their relevant directory

#1 Post by AnthonyCSC » 02 Apr 2009 01:27

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.

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 02 Apr 2009 10:20

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
)

AnthonyCSC
Posts: 4
Joined: 01 Apr 2009 10:47

#3 Post by AnthonyCSC » 06 Apr 2009 02:54

RElliott63,

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

Post Reply