|
Answer» Hi, I'm new to DOS and want to MAKE a batch file that can copy all files within a given time span. What i`m trying to do is something like this:-
Loop(All files in folder){ IF(file.lastdatemodified <= x hours){ copyfiles to another folder } }
Can somebody help me?What you want would be difficult in a batch file. It is easy to copy files modified on or after a specified DATE ... would DATE be good enough for you, or does it need to be by hours?
If it needs to be done by time, you would need to get current time, then manually subtract the specified number of hours (TAKING into account the change of day, and figuring out how to count backwards for the number of days specific to each month ... and you should probably check for years and leap years) then compare the calculated date to the date of each file. Major pain to script for batch probably 100 lines of code or more ... but I bet Sidewinder (or others) could do all that in about 5 lines of VB if that would work for you.What time span? This little blurb will run in Windows:
Code: [Select]Set FSO = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("folderspec") 'No trailing backslash Set fc = f.Files For Each fs in fc If DateDiff("h", fs.DateLastModified, Now) <= 7 Then 'Set for 7 Hours fso.CopyFile fs, "c:\anotherfolderspec\", True 'Keep trailing backslash End If Next
As written, the code ages the file modified date based on the current date/time and checks for an arbitrary 7 hour timespan. Change folderspec and anotherfolderspec as needed (keep the quotes). Change the number of hours as needed.
Save the script with a vbs extension and run from the COMMAND line as cscript scriptname.vbs.
Happy Computing 8-)
PS. GuruGary is right about date/time calcs in batch. You'd have to jump thru hoops to get the job done. But it sure is fun to watch. Thank guys!
Wow it very hard to working with time in DOS.. huh. I do get what GuruGary say. Actually I need this code for copy logs files recently created in 6 hours and it need to be schedule every 6 hours. I intent to use BATCH files as to schedule using Windows scheduler because my boss told say so.
Anyway guys, your help is very much appreciate!! My problem SOLVED
|