MSFN Forum: Batch-file assistance - folder watch, copy, rename - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Batch-file assistance - folder watch, copy, rename Rate Topic: -----

#1 User is offline   Indy 

  • Group: Members
  • Posts: 7
  • Joined: 26-October 11
  • OS:XP Pro x86
  • Country: Country Flag

Posted 26 October 2011 - 11:44 AM

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


#2 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 26 October 2011 - 01:02 PM

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 User is offline   Indy 

  • Group: Members
  • Posts: 7
  • Joined: 26-October 11
  • OS:XP Pro x86
  • Country: Country Flag

Posted 26 October 2011 - 01:15 PM

View Postjaclaz, on 26 October 2011 - 01:02 PM, said:

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 User is offline   Indy 

  • Group: Members
  • Posts: 7
  • Joined: 26-October 11
  • OS:XP Pro x86
  • Country: Country Flag

Posted 26 October 2011 - 01:17 PM

View Postjaclaz, on 26 October 2011 - 01:02 PM, said:

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 File(s)


This post has been edited by Indy: 26 October 2011 - 01:22 PM


#5 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 26 October 2011 - 01:35 PM

View PostIndy, on 26 October 2011 - 11:44 AM, said:

1. Watch a specified directory.


View PostIndy, on 26 October 2011 - 11:44 AM, said:

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 User is offline   Indy 

  • Group: Members
  • Posts: 7
  • Joined: 26-October 11
  • OS:XP Pro x86
  • Country: Country Flag

Posted 26 October 2011 - 01:43 PM

View Postjaclaz, on 26 October 2011 - 01:35 PM, said:

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 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 26 October 2011 - 01:50 PM

View PostIndy, on 26 October 2011 - 01:43 PM, said:

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.

View PostIndy, on 26 October 2011 - 01:43 PM, said:

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 User is offline   Indy 

  • Group: Members
  • Posts: 7
  • Joined: 26-October 11
  • OS:XP Pro x86
  • Country: Country Flag

Posted 26 October 2011 - 02:00 PM

View Postjaclaz, on 26 October 2011 - 01:50 PM, said:

View PostIndy, on 26 October 2011 - 01:43 PM, said:

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 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,356
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 26 October 2011 - 02:01 PM

Quote

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

#10 User is offline   allen2 

  • Not really Newbie
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,750
  • Joined: 13-January 06

Posted 26 October 2011 - 03:20 PM

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 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 27 October 2011 - 02:42 AM

@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 User is offline   allen2 

  • Not really Newbie
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,750
  • Joined: 13-January 06

Posted 27 October 2011 - 03:03 AM

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 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 27 October 2011 - 03:54 AM

View Postallen2, on 27 October 2011 - 03:03 AM, said:

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 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,356
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 27 October 2011 - 12:30 PM

View Postjaclaz, on 27 October 2011 - 02:42 AM, said:

@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)
Number of downloads: 2

#15 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 27 October 2011 - 03:52 PM

View Postjaclaz, on 27 October 2011 - 02:42 AM, said:

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.

#16 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 28 October 2011 - 02:17 AM

View PostCoffeeFiend, on 27 October 2011 - 03:52 PM, said:

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

Quote

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 User is offline   Indy 

  • Group: Members
  • Posts: 7
  • Joined: 26-October 11
  • OS:XP Pro x86
  • Country: Country Flag

Posted 01 November 2011 - 02:17 PM

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 User is offline   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 11,574
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 01 November 2011 - 02:42 PM

Check if any of these fit:
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


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy