Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Helpful Script: Shows updated/changed files, then backs them up

Status
Not open for further replies.

GeneralDzur

Technical User
Jan 10, 2005
204
US
Reason for Writing
I store everything in a folder called 'root', because I hate My Documents. Anyway, it's at C:\root and contains everything I use, nicely organized for easy back up.

I got tired of looking through each individual folder, trying to remember which copy was newer, which copy was updated, etc... so I wrote a little batch file to do it for me. It requires ROBOCOPY, a command-line .exe available as part of the Server 2003 Resouce Kit, available here from Microsoft's site, or here as a stand-alone .exe.

What It Does
1) It searches a Destination directory and Source directory for similar files
2) Shows you a list of NEW, UPDATED, or OBSOLETE files that it found and asks you if it's ok to update them
3a) If any of the files in the source are NEW or UPDATED, it copies them to the Destination, overwriting the older version.
3b) If any of the files in the Destination DO NOT EXIST in the Source, it deletes them from the Destination.
3c) It saves a report in a text file for you.

What You Need To Do
1) Drop a copy of "ROBOCOPY.exe" in c:\windows\system32
2) Replace [Source] and [Destination] with the folders/files you want to copy. If you want to ignore files with a certain name or extension (*.txt, chicken, etc) put each string in it's own set of quotes. I underlined the first set for you.

Hopefully this saves you some time or money. If you like it , let me know! :) There's a list of what the switches do (/v, /i, etc) below the code.
Code:
@echo off
color 81
cls
:: ###THIS ASKS YOU IF YOU WANT TO START SEARCHING###
echo --------------------------------------------------
echo --                                              --
echo --- Ready to find updated files under [b][Source][/b]  ---
echo --                                              --
echo --------------------------------------------------
pause
cls
echo.
:: ###BUILD THE FILE LIST, CREATE THE LOG FILE - DON'T COPY ANYTHING YET###
::
@robocopy [b][Source] [Destination][/b] /E /PURGE /XD "[u]Filename to ignore[/u]" /L > c:\robo_temp.txt
::
@robocopy [b][Source] [Destination][/b] /E /PURGE /XD "[u]Filename to ignore[/u]" /L > "[u]Path and name of logfile[/u]"
findstr /I "newer *extra lonely same new dir ended older dirs total files bytes ------" c:\robo_temp.txt
echo.
echo.
echo LEGEND
echo --Status-----Meaning-----------------------Result--------------------
echo / *EXTRA     Doesn't exist in *source*     DELETED from source      /
echo / Newer      Newer time stamp              overwrites older copy    /
echo / lonely     New file                      copied                   /
echo / same       Unchanged                     not copied               /
echo ---------------------------------------------------------------------
echo.
echo.
echo.
echo Double-check EVERY FILE - All obsolete stuff will be ERASED!
echo.
echo Are you ready?
echo.
echo.
pause
:: ###PERFORM THE COPY AND SHOW YOU THE RESULTS###
robocopy [b][Source] [Destination][/b] /E /PURGE /XD "[u]Filename to ignore[/u]" "[u]Path to logfile[/u]"
cls
:: Remove the temp file
del %SYSTEMDRIVE%\robo_temp.txt
echo.
echo Ok, all done. Finished at %TIME%
echo A full list of files that needed 
echo updating is at [b][Location of log file][/b]
pause

Switches Used
--robocopy.exe--
/V Verbose logging (more details)
/L List files but don't copy/touch them
/PURGE Delete files that no longer exist in Source
/E Copy all directories (including sub-dirs)
/LOG+: Stick a logfile [here], add+ new stuff to the end
/XD "eat" Exclude any file or directory containing "eat"

--findstr.exe--
/I Search is NOT case-sensitive
/V Ignore any file with "this" as part of it's name
 
I'd never messed with that SyncToy before...it's pretty cool, plus a slick GUI.

Oh well...the .bat is simpler, and can be customized *way* more than an .exe

 
A .bat can be customized *way* more than an .exe?
I am sorry, but this is absolutely untrue.

While clever things can be done with .bat/.cmd files, I can with an .exe do things you never even dreamed where possible with the OS.

Beware of generalizations, no matter how truthful they seem generaly speaking.


____________________________
Users Helping Users
 
I think you misunderstood me b.

A .bat *can* be customized way more than an already-compiled .exe - I.E. SyncToy.

Obviously you can dream up anything, code & compile, but a .bat gives you more options over something already coded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top