Page 1 of 1

Need to copy files from a directory that the name changes

Posted: 13 Mar 2009 12:23
by MinorThreat
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

Posted: 13 Mar 2009 22:35
by RElliott63
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

Posted: 17 Mar 2009 10:57
by MinorThreat
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

Posted: 17 Mar 2009 11:11
by RElliott63
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

Posted: 17 Mar 2009 11:19
by RElliott63
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

This looks like it will work!

Posted: 17 Mar 2009 17:31
by MinorThreat
Thank you very much!