Jump to content

Batch-file assistance - folder watch, copy, rename


Recommended Posts

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 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

Link to comment
Share on other sites


I presume XP or later. :unsure:

The real problem is IMHO not to get the data, but rather to find out if the date/time has changed. :unsure:

When you are using batch files for dates time there is a "random variable" that is your "international" date/time settings, :ph34r:

Check:

and:

http://www.robvanderwoude.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

Link to comment
Share on other sites

I presume XP or later. :unsure:

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

Link to comment
Share on other sites

I presume XP or later. :unsure:

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

post-335662-0-88124500-1319656634_thumb.

Edited by Indy
Link to comment
Share on other sites

1. Watch a specified directory.

So, if C:\main\filename.txt changes, copy to C:\backup\filename_DATE_TIME.txt

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.html

About 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

Link to comment
Share on other sites

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.

:-)

Link to comment
Share on other sites

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 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

Link to comment
Share on other sites

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 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).

Link to comment
Share on other sites

@gunsmokingman

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

Link to comment
Share on other sites

there are pre-made apps that can do that

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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...