Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Batch-file assistance - folder watch, copy, rename

- - - - -

  • Please log in to reply
17 replies to this topic

#1
Indy

Indy
  • Member
  • 7 posts
  • OS:XP Pro x86
  • Country: Country Flag
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


How to remove advertisement from MSFN

#2
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag
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:
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
Indy

Indy
  • Member
  • 7 posts
  • OS:XP Pro x86
  • Country: Country Flag

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

#4
Indy

Indy
  • Member
  • 7 posts
  • OS:XP Pro x86
  • Country: Country Flag

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

Attached Files


Edited by Indy, 26 October 2011 - 01:22 PM.


#5
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag

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

#6
Indy

Indy
  • Member
  • 7 posts
  • OS:XP Pro x86
  • Country: Country Flag

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

#7
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag

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

#8
Indy

Indy
  • Member
  • 7 posts
  • OS:XP Pro x86
  • Country: Country Flag


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

#9
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag

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


GunSmokingMan



#10
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
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).

#11
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag
@allen2
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
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,812 posts
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.

#13
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag

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.sourc...hots.html#LaRVe
but sure, that is just an example.

jaclaz

#14
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag

@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
Attached File  Demo_Date_Vbs.cmd.txt   966bytes   4 downloads


GunSmokingMan



#15
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • OS:Windows 7 x64
  • Country: Country Flag

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.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#16
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag

I think that's the best option too. Why bother if there's something pre-made that does the job reasonably well...
....
It would still be pretty simple to write such a tool in a number of languages.


I will quote - just in case - Mikhail Kalashnikov :w00t:
http://en.wikipedia.org/wiki/AK-47
http://en.wikipedia....#Design_concept

A lot of [Soviet Army soldiers] ask me how one can become a constructor, and how new weaponry is designed. These are very difficult questions. Each designer seems to have his own paths, his own successes and failures. But one thing is clear: before attempting to create something new, it is vital to have a good appreciation of everything that already exists in this field. I myself have had many experiences confirming this to be so.


;)

jaclaz

#17
Indy

Indy
  • Member
  • 7 posts
  • OS:XP Pro x86
  • Country: Country Flag
Just wanted to check in, and thank everyone for your input and direction. I did check out EVACopy, which is an excellent utility. It does ALMOST everything I need to do, but can only check once per minute of keyboard/mouse inactivity. You can technically set it for less (say, 0.1) but after the first attempt, it can't do it faster than 45 seconds due to the program's limitations. I chatted with the programmer, who has done a great job; the software should work nicely for most people who need such a solution. Unfortunately, when we're working on projects that need this auto-backup feature, we literally work steadily for hours without pausing long enough for the backup to kick in. So while EVACopy won't work for my needs (and I continue to investigate how to script this in some way), it truly is a neat program! I actually plan to use it for other purposes at home, so I'll definitely get some use out of it.

#18
jaclaz

jaclaz

    The Finder

  • Developer
  • 13,985 posts
  • OS:none specified
  • Country: Country Flag
Check if any of these fit:
http://www.technixup...folder-changes/


Particularly this one could do:
http://www.softpedia...FolderMon.shtml

jaclaz

Edited by jaclaz, 01 November 2011 - 02:43 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN