Need to copy files from a directory that the name changes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MinorThreat
Posts: 3
Joined: 13 Mar 2009 12:09

Need to copy files from a directory that the name changes

#1 Post by MinorThreat » 13 Mar 2009 12:23

So here is what happens, once a month we have an automated query that spits out 3 large .txt files into a directory that says 01 Jan, 02 February etc and so on.

I need to copy each of these files to another directory on another server and rename them. The issue i'm having is trying to set something up that will be able to find the correct directory each month. Since the directory is going to change to each time, how would I go about doing this? For example

Copy \\batch\prod\int\?monthnumber?First3lettersofmonth?\file.txt C:\test\

Is there a way to change the directory to the most recently modified directory?

Thank you in advance

-MT

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

#2 Post by RElliott63 » 13 Mar 2009 22:35

If the files are going to be copied out of these directories each month and renamed, then why not just do a:

DIR /b/s \\Batch\Prod\Int\File*.txt

This will identify all files named File1.txt, File2.txt, File3.txt, etc, regardless of what month it is or what folder (01Jan, 02Feb, 03Mar, etc) . If you perform this with a:

For /F "Delims=" %%F in ('DIR /b/s \\Batch\Prod\Int\File*.txt ') do (

Then you would be able to trim off the current path to the files found, ie:

Set "p=%%~pF"
Set "f=%%~nxF"

That way you can identify where to copy the file from and the file name you are copying.

*Note* you will have to enable extensions for this to work

Then, once you have the files copy/move/rename them to where ever you want them.

I guess if you needed CURRENT month, you could get that from the current date and separate it to pick and choose which folders to specifically look at based on the \\Batch\Prod\Int\%CurMth%* type scenario.

-Rick

MinorThreat
Posts: 3
Joined: 13 Mar 2009 12:09

#3 Post by MinorThreat » 17 Mar 2009 10:57

Rick,

The problem is that each time the same file names get copied but we need the most up to date file. So in other words the main folder contains the folder of the month 01 Jan, 02 Feb, 03 March. Each of these folders contain the files Load1, Load2, Load3. So if I copy all of them over then which ones are going to be the correct most up to date ones and which are going to be over written?

Also I can't use the current month because the files are published a month behind. I.E. The Jan folder is published in Feb.

Thank You very much for you help though!

-Ncik

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

#4 Post by RElliott63 » 17 Mar 2009 11:11

Assuming that you know the names of the files (Load1, Load2, Load3) and this same file name is in the different folders (01-Jan, 02-Feb, 03-Mar, etc) then --

Dir /B /S /O-D would give you a directory sorted by date (Newest first)

Something like:

\03-Mar\Load1
\03-Mar\Load2
\03-Mar\Load3
\02-Feb\Load1
\02-Feb\Load2
\02-Feb\Load3
\01-Jan\Load1
\01-Jan\Load2
\01-Jan\Load3

Processing this list in order looking for the first LOAD1, LOAD2 and LOAD3 files would give you the "latest" versions of the files (based on file date). As a further comparison, you could compare the date with prior month, but I think that if you're running this on the 1st of the following month, you should pick up the files you're looking for.

-Rick

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

#5 Post by RElliott63 » 17 Mar 2009 11:19

Assuming that you know the names of the files (Load1, Load2, Load3) and this same file name is in the different folders (01-Jan, 02-Feb, 03-Mar, etc) then --

Dir /B /S /O-D would give you a directory sorted by date (Newest first)

Something like:

\03-Mar\Load1
\03-Mar\Load2
\03-Mar\Load3
\02-Feb\Load1
\02-Feb\Load2
\02-Feb\Load3
\01-Jan\Load1
\01-Jan\Load2
\01-Jan\Load3

Processing this list in order looking for the first LOAD1, LOAD2 and LOAD3 files would give you the "latest" versions of the files (based on file date). As a further comparison, you could compare the date with prior month, but I think that if you're running this on the 1st of the following month, you should pick up the files you're looking for.

-Rick

MinorThreat
Posts: 3
Joined: 13 Mar 2009 12:09

This looks like it will work!

#6 Post by MinorThreat » 17 Mar 2009 17:31

Thank you very much!

Post Reply