PDA

View Full Version : Batch file to copy dynamically made files


Hardeep_S
04-21-2005, 07:10 PM
Hi. I have folder which are made dynamically and i wanted to copy the contents in them to another destination. The folder themselves only have one .txt file in them so that will be easy. However, to get into the right folder is what has me confused. The folders are named like....

2005-04-18
2005-04-19
2005-04-20

and the folder i want to open is the last one but the date on it is yesterday's. So i was thinking maybe to treat those as strings or something and sort them somehow. I have never programmed a batch file other than real simple ones so i have no clue how to do this.

the command i'm gonna issue in cmd is...

copy "c:\dated logs\%that last folder%\*.txt" "c:\Program Files\MySQL\MySQL Server 4.1\data\syslog"

Can someone please help me!

Unit
04-21-2005, 07:26 PM
This should do what you want. But is what you want is what you need?
This will not work if two logs are written before you get a chance to run this. This will always get the last log and copy to syslog folder.

@echo off
dir /b /ad /o-d>c:\tempdeep.dat
set /ptempvar01=tempvar01<c:\tempdeep.dat
copy "c:\dated logs\%tempvar01%\*.txt" "c:\Program Files\MySQL\MySQL Server 4.1\data\syslog"
del c:\tempdeep.dat
set tempvar01=

Let us know how it goes.

Hardeep_S
04-21-2005, 07:54 PM
Thx for the quick reply

it does not seem to work. when i run the batch file, the cmd window flashes for like half a second and nothing gets copied.

also, there is only one text file in each of the folders at any one time.

Unit
04-21-2005, 08:05 PM
hmm, remove the "@echo off" and paste the output here. Run it from command line so that you can capture the output.

To open a command window go to start->run and type cmd

Edit: add this to the beginning of the file
cd "c:\dated logs\"
or change
dir /b /ad /o-d>c:\tempdeep.dat
to
dir /b /ad /o-d "c:\dated logs\">c:\tempdeep.dat

Hardeep_S
04-21-2005, 08:37 PM
hi, i tried this ..

c:
cd "c:\dated logs\"
dir /b /ad /o-d>c:\tempdeep.dat
set /ptempvar01=tempvar01<c:\tempdeep.dat
copy "c:\dated logs\%tempvar01%\*.txt" "c:\Program Files\MySQL\MySQL Server 4.1\data\syslog"
del c:\tempdeep.dat
set tempvar01=

and it did copy the file but it copied the the file from teh oldest folder, not the newest one.

i got folders named from 2005-04-07 to 2005-04-20 right now and i want to copy the file from teh 2004-04-20 one, but this code copied from the 2005-04-07 one

Hardeep_S
04-21-2005, 08:50 PM
here's the ouput



C:\Documents and Settings\hsanghera\Desktop>c:

C:\Documents and Settings\hsanghera\Desktop>cd "c:\dated logs\"

C:\Dated logs>dir /b /ad /o-d 1>c:\tempdeep.dat

C:\Dated logs>set /ptempvar01=tempvar01 0<c:\tempdeep.dat
tempvar01
C:\Dated logs>copy "c:\dated logs\2005-04-07\*.txt" "c:\Program Files\MySQL\MySQ
L Server 4.1\data\syslog"
c:\dated logs\2005-04-07\SyslogCatchAll-2005-04-07.txt
1 file(s) copied.

C:\Dated logs>del c:\tempdeep.dat

C:\Dated logs>set tempvar01=

C:\Dated logs>

Hardeep_S
04-21-2005, 10:44 PM
anyone??

Unit
04-22-2005, 01:49 AM
did you make the folders?
The batch file was trying to get the latest folder based on the time stamp on the folder, not the name. If you want to sort by name, look at the various sort keys for dir command.

You can get a list by typing dir /? at command prompt.