Click here to Skip to main content
15,868,419 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I want to create a batch file
and this batch file function is like that

1. Run batch file
2. Looping a particular folder (C:\folderA)
3. find all the .txt inside it and get .txt modified date time
4. compare the modified date and time with the current date and time
5. if .txt modified date and time is earlier 3 seconds
6. move those .txt into another folder (C:folderB)

i crack my head.. still cant get any idea..:S
i only manage to write
VB
for %%d in (C:\A) do DIR %%d *.*
pause
Posted
Comments
G4mm4R4y 12-Nov-12 22:39pm    
Im very interested in this question. I do not think there is a way to get the date and time. Ill try to search the internet but if you dont hear back from me its probably impossible. I check through cmd commands from time to time and never see a command like that. I know this doesnt help much but i thought you should know
_______________________________________________________________________________
Remember "help"!

1 solution

I realize this is pretty old, but in case anyone is still interested...

I think the "hard" part of this is coming up with the seconds on the modified time. It turns out that forfiles will do this, but because forfiles requires internal commands to be launched in a separate cmd shell it's a bit sticky getting the values back.

This will write the information into a .csv file:
forfiles  /m *.txt /c "cmd /c for /r %%i in (@file) do @echo @file, @ftime, @fdate >> txt_files.csv"


Given a .csv file listing all the .txt files along with modified dates and times, the rest should follow pretty easily. The file can be read with a for loop, and the dates and times compared to %date% and %time% to determine if the file should be moved. (That part is more tedious than tricky: if the 3-second cutoff causes the seconds to roll over a minute boundary then both the seconds and minutes will need to be adjusted, and repeat if the adjusted minute boundary rolls over the hour boundary or the adjusted hour boundary over a day boundary or the adjusted day boundary rolls over a month...you get the idea.)

I don't see any way to force @time to use a 24-hour clock, so the AM/PM in the .csv file will need to be handled as well.

Also, %time% will have a leading blank on any hour before 10 AM, so using something like the following will fix that:
C#
set curtime=%time:~0,2%%time:~3,2%%time:~6,2%
set curtime=%curtime: =0%
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900