Indy Posted October 26, 2011 Posted October 26, 2011 I know this is a long shot, but I'm hoping someone out there might be able to direct me.I'd like a batch file that will do the following:1. Watch a specified directory.2. When any file in the directory changes -- as in a new file is created or a file is updated -- copy it to a different directory3. But when it copies to the backup directory, rename the copied file with the appended time and dateSo, if C:\main\filename.txt changes, copy to C:\backup\filename_DATE_TIME.txtThe thing is, it would have to poll the directory to account for any changes -- creations or alterations -- and back it up.This can be quick and dirty... it can merely check a folder every X minutes and copy any files newer than current date/time minus those X minutes. But it would have to keep multiple backups any files that change in the backup folder.Ultimately, I'd love to get fancy, and limit the backups to, say, the last five versions of the file, and delete any older than that. Yes, I know, now I'm just getting greedy. But if I can learn how to watch a folder for file changes and copy changed files with new names, I'd be plenty satisfied. I just can't figure out how to poll through all files in a directory, much less rename each copied file on the fly.Any ideas?I'm an old programmer from way back and code from time to time, but haven't done batch-file programming to any extent for about 20 years. So I'm on the cusp of digging up a complete, advanced, batch-file reference just to write this one file. If anyone can help, I'd really appreciate it.Best,-Indy
jaclaz Posted October 26, 2011 Posted October 26, 2011 I presume XP or later. The real problem is IMHO not to get the data, but rather to find out if the date/time has changed. When you are using batch files for dates time there is a "random variable" that is your "international" date/time settings, Check:and:http://www.robvanderwoude.com/ntcall.phpTry quickly this batch example:@ECHO OFFFOR /F %%A IN ('DIR /B C:\main\*.*' ) DO (ECHO %%AECHO %%~AECHO %%~dAECHO %%~pAECHO %%~nAECHO %%~xAECHO %%~dpnxAECHO %%~tAECHO %%~zAECHO %%~aAECHO.PAUSE)What do you get as output?jaclaz
Indy Posted October 26, 2011 Author Posted October 26, 2011 I presume XP or later. Sorry, you are quite right, XP. However, we're going to Windows 7 machines soon.I tried your batch without the ECHO OFF so you can see everything, and it looks like the attached. It's complaining about not finding the specified file. Note there are four files in the directory.I'll delve into those links and see if I can learn something!Best,-Indy
Indy Posted October 26, 2011 Author Posted October 26, 2011 (edited) I presume XP or later. Sorry, you are quite right, XP. However, we're going to Windows 7 machines soon.It appears to poll through all files in the folder properly. Ignore the attached image; I goofed when I ran it originally.So I could eliminate PAUSE and just let it cycle through repeatedly?I'll delve into those links and see if I can learn something!Best,-Indy Edited October 26, 2011 by Indy
jaclaz Posted October 26, 2011 Posted October 26, 2011 1. Watch a specified directory.So, if C:\main\filename.txt changes, copy to C:\backup\filename_DATE_TIME.txtThe snippet is "localized" to "C:\main\".Change the line:FOR /F %%A IN ('DIR /B C:\main\*.*' ) DO (to:FOR /F %%A IN ('DIR /B *.*' ) DO (if you want to try runnning it on the same directory the batch is.jaclaz
Indy Posted October 26, 2011 Author Posted October 26, 2011 The snippet is "localized" to "C:\main\".Yep -- I just created the folder, and it retested perfectly. It polls through each file and reports it as existing.So my next question: How can I have the batch file then pause, without using PAUSE, and wait a few minutes before checking again? Obviously, my aim is to have this run silently in the background, watching the folder and quietly copying backups if needed.Then, how will it know to then copy any files that are new or changed?Best,-Indy
jaclaz Posted October 26, 2011 Posted October 26, 2011 So my next question: How can I have the batch file then pause, without using PAUSE, and wait a few minutes before checking again? Obviously, my aim is to have this run silently in the background, watching the folder and quietly copying backups if needed.Maybe you are asking too much for a batch.You may use SCHTASKS to make it run periodically:http://ss64.com/nt/schtasks.htmlAbout the "silently", you will need anyway a "third party" program to hide the command console window.Then, how will it know to then copy any files that are new or changed?That is a good question.Easier would probably be writing a log at each run, then re-parse the log and compare it to the current "source" DIR contents.jaclaz
Indy Posted October 26, 2011 Author Posted October 26, 2011 So my next question: How can I have the batch file then pause, without using PAUSE, and wait a few minutes before checking again? Obviously, my aim is to have this run silently in the background, watching the folder and quietly copying backups if needed.Maybe you are asking too much for a batch.Yes, I knew this would be adventurous from the outset. I don't really need the batch to run invisibly -- just without need for user interaction. I'm thinking my "quick and dirty" example would make more sense... every 15 minutes, check the folder, and copy backups based on dates/times that have changed in the last 15 minutes.It has literally been 20 years since I did any of this. Time to check out those links and maybe find a good book. or two.:-)
gunsmokingman Posted October 26, 2011 Posted October 26, 2011 About the "silently", you will need anyway a "third party" program to hide the command console window. You could use a VBS script to run the batch file without any cmd promt window showingExample VBSCreateObject("Wscript.Shell").Run _("C:\Users\Gunsmokingman\Desktop\TestDemo.cmd"),0,FalseExample Cmd@Echo OffEcho Hello World > Test.txtWill produce the text file without any cmd promt window showing
allen2 Posted October 26, 2011 Posted October 26, 2011 If the files were stored on a server os like windows 2003/2008/2008R2, you could have used the shadow copies to make the backups for you every 15min (the interval between two shadow copies might depend a lot on the volume usage). If you really want something in batch (not plainly batch anyway), here is how i would do it:@echo offset pathtools=%~dp0set time_interval=15set path_mon=c:\mainset backupdir=c:\backupset /A sleep_time=%time_interval%*60:loopfor /f "usebackq" %%i in (`%pathtools%date.exe +%%Y%%m%%d_%%k%%M%%S`) do (set now=%%i)%pathtools%find.exe %path_mon% -type f -cmin -%time_interval% > %pathtools%tmp_files_to_copy.lstfor /f "delims=" %%i in (%pathtools%tmp_files_to_copy.lst) do (copy %%i %backupdir%\%%~ni_%now%.%%~xi)%pathtools%sleep.exe %sleep_time%goto loopYou'll need the unixtools find.exe, date.exe and sleep.exe stored in the same folder with the batch (you'll need to modify the pathtools value).As for the silent need, i would run this as a service with srvany.exe et instsrv.exe (from the windows 2003 reskit).
jaclaz Posted October 27, 2011 Posted October 27, 2011 @allen2Actually, if we are going "external" (i.e. it is possible to download and/or "install" something) there are pre-made apps that can do that, many of which freeware.Just as an example, this may do:http://evacopy.sourceforge.net/@gunsmokingmanVBS should be also "better" than batch to manage date/times ....jaclaz
allen2 Posted October 27, 2011 Posted October 27, 2011 Evacopy seems to only copy to date/time folder and not being able to append date/time to the file name's. But of course there might be other tools that would do it.
jaclaz Posted October 27, 2011 Posted October 27, 2011 Evacopy seems to only copy to date/time folder and not being able to append date/time to the file name's.Yep, it uses instead a "versioning" system, which is actually IMHO better (as the filenames remain unchanged):http://evacopy.sourceforge.net/30-screenshots.html#LaRVebut sure, that is just an example.jaclaz
gunsmokingman Posted October 27, 2011 Posted October 27, 2011 @gunsmokingmanVBS should be also "better" than batch to manage date/times ....jaclazThat true but you could also add something like this to the code postedto get the Date Time Formats from VBSExample Pass Varibles From One Script Langauge To Another Script LangaugeSave As Demo_Date_Vbs.cmd@Echo OffCLSMode 55,7COLOR F2Title Pass Varibles Between 2 Script LangaugeSet Vbs="%Temp%\Test.vbs"::Create VBS File With Date InformationEcho On Error Resume Next > %Vbs% Echo Dim Act :Set Act = Createobject("Wscript.Shell") >> %Vbs% Echo Dim Fso :Set Fso = Createobject("Scripting.FileSystemObject") >> %Vbs% Echo Dim Ts >> %Vbs% Echo Set Ts = Fso.CreateTextFile(Act.ExpandEnvironmentStrings("%Temp%\Temp.cmd")) >> %Vbs% Echo Ts.WriteLine "Set mName=" ^& MonthName(Month(Now),False) >> %Vbs% Echo Ts.WriteLine "Set mDay=" ^& WeekdayName(Weekday(Now),False) >> %Vbs% Echo Ts.WriteLine "Set mDate=" ^& "%%mName%%" ^& "," ^& "%%mDay%%" ^&_ >> %Vbs% Echo " " ^& Day(Date) ^& "," ^& Year(Date) >> %Vbs%Echo Ts.Close >> %Vbs%Call "%Vbs%"Call "%Temp%\Temp.cmd"Del "%Vbs%"Del "%Temp%\Temp.cmd"Echo.Echo Cmd To VBS To Cmd ResultsEcho Month Name = %mName% Echo Day Name = %mDay%Echo Full Date = %mDate%Echo.pauseChange Demo_Date_Vbs.cmd.txt to Demo_Date_Vbs.cmd to make activeDemo_Date_Vbs.cmd.txt
CoffeeFiend Posted October 27, 2011 Posted October 27, 2011 there are pre-made apps that can do thatI think that's the best option too. Why bother if there's something pre-made that does the job reasonably well...But if you were to write such an app, there would be several key decisions to make such as: relying on datestamps / archive attribute / file sizes to see if something's changed vs hashing all files to see if they've changed (better test but a lot heavier, especially with large files). Or watching for changes "live" using a FileSystemWatcher (quicker, knows instantly when something is updated) vs comparing the files each time the tool is run (doesn't have to be ran "live") and so on. It would still be pretty simple to write such a tool in a number of languages.
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now