Batch-file assistance - folder watch, copy, rename
#1
Posted 26 October 2011 - 11:44 AM
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 directory
3. But when it copies to the backup directory, rename the copied file with the appended time and date
So, if C:\main\filename.txt changes, copy to C:\backup\filename_DATE_TIME.txt
The 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
#2
Posted 26 October 2011 - 01:02 PM
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:
http://www.msfn.org/...ename-in-batch/
and:
http://www.robvander....com/ntcall.php
Try quickly this batch example:
@ECHO OFF
FOR /F %%A IN ('DIR /B C:\main\*.*' ) DO (
ECHO %%A
ECHO %%~A
ECHO %%~dA
ECHO %%~pA
ECHO %%~nA
ECHO %%~xA
ECHO %%~dpnxA
ECHO %%~tA
ECHO %%~zA
ECHO %%~aA
ECHO.
PAUSE
)
What do you get as output?
jaclaz
#3
Posted 26 October 2011 - 01:15 PM
jaclaz, on 26 October 2011 - 01:02 PM, said:
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
#4
Posted 26 October 2011 - 01:17 PM
jaclaz, on 26 October 2011 - 01:02 PM, said:
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
Attached File(s)
-
batch01.gif (19.89K)
Number of downloads: 14
This post has been edited by Indy: 26 October 2011 - 01:22 PM
#5
Posted 26 October 2011 - 01:35 PM
Indy, on 26 October 2011 - 11:44 AM, said:
Indy, on 26 October 2011 - 11:44 AM, said:
The 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
#6
Posted 26 October 2011 - 01:43 PM
jaclaz, on 26 October 2011 - 01:35 PM, said:
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
#7
Posted 26 October 2011 - 01:50 PM
Indy, on 26 October 2011 - 01:43 PM, said:
Maybe you are asking too much for a batch.
You may use SCHTASKS to make it run periodically:
http://ss64.com/nt/schtasks.html
About the "silently", you will need anyway a "third party" program to hide the command console window.
Indy, on 26 October 2011 - 01:43 PM, said:
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
#8
Posted 26 October 2011 - 02:00 PM
jaclaz, on 26 October 2011 - 01:50 PM, said:
Indy, on 26 October 2011 - 01:43 PM, said:
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.
:-)
#9
Posted 26 October 2011 - 02:01 PM
Quote
You could use a VBS script to run the batch file without any cmd promt window showing
Example VBS
CreateObject("Wscript.Shell").Run _
("C:\Users\Gunsmokingman\Desktop\TestDemo.cmd"),0,False
Example Cmd
@Echo Off Echo Hello World > Test.txt
Will produce the text file without any cmd promt window showing
#10
Posted 26 October 2011 - 03:20 PM
If you really want something in batch (not plainly batch anyway), here is how i would do it:
@echo off set pathtools=%~dp0 set time_interval=15 set path_mon=c:\main set backupdir=c:\backup set /A sleep_time=%time_interval%*60 :loop for /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.lst for /f "delims=" %%i in (%pathtools%tmp_files_to_copy.lst) do (copy %%i %backupdir%\%%~ni_%now%.%%~xi) %pathtools%sleep.exe %sleep_time% goto loop
You'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).
#11
Posted 27 October 2011 - 02:42 AM
Actually, 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/
@gunsmokingman
VBS should be also "better" than batch to manage date/times ....
jaclaz
#12
Posted 27 October 2011 - 03:03 AM
#13
Posted 27 October 2011 - 03:54 AM
allen2, on 27 October 2011 - 03:03 AM, said:
Yep, it uses instead a "versioning" system, which is actually IMHO better (as the filenames remain unchanged):
http://evacopy.sourc...hots.html#LaRVe
but sure, that is just an example.
jaclaz
#14
Posted 27 October 2011 - 12:30 PM
jaclaz, on 27 October 2011 - 02:42 AM, said:
VBS should be also "better" than batch to manage date/times ....
jaclaz
That true but you could also add something like this to the code posted
to get the Date Time Formats from VBS
Example Pass Varibles From One Script Langauge To Another Script Langauge
Save As Demo_Date_Vbs.cmd
@Echo Off
CLS
Mode 55,7
COLOR F2
Title Pass Varibles Between 2 Script Langauge
Set Vbs="%Temp%\Test.vbs"
::Create VBS File With Date Information
Echo 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 Results
Echo Month Name = %mName%
Echo Day Name = %mDay%
Echo Full Date = %mDate%
Echo.
pause
Change Demo_Date_Vbs.cmd.txt to Demo_Date_Vbs.cmd to make active
Demo_Date_Vbs.cmd.txt (966bytes)
Number of downloads: 2
#15
Posted 27 October 2011 - 03:52 PM
jaclaz, on 27 October 2011 - 02:42 AM, said:
I 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.
#16
Posted 28 October 2011 - 02:17 AM
CoffeeFiend, on 27 October 2011 - 03:52 PM, said:
....
It would still be pretty simple to write such a tool in a number of languages.
I will quote - just in case - Mikhail Kalashnikov
http://en.wikipedia.org/wiki/AK-47
http://en.wikipedia....#Design_concept
Quote
jaclaz
#17
Posted 01 November 2011 - 02:17 PM
#18
Posted 01 November 2011 - 02:42 PM
http://www.technixup...folder-changes/
Particularly this one could do:
http://www.softpedia...FolderMon.shtml
jaclaz
This post has been edited by jaclaz: 01 November 2011 - 02:43 PM
- ← [Powershell] Get lines of text-file and fill up a variable
- Programming (C++, Delphi, VB/VBS, CMD/batch, etc.)
- Registry key to a text file in VB.NET →



Help
Back to top










