Xcopy or robocopy batch file - xcopy

I'm trying to create a batch file that takes latest .exes between dates format DDMMYYYY from a network drive and give it to all PCs with overwrite and skip if a pc is turned off.
Tried it with 1 file without date on 1 pc and succeeded but struggling with multiple.

Related

Batch file to archive important files into a zip file in Windows 7

I have some files which I would like to be able to archive daily into a zip file that has the date in the filename.
The files to be archived are in one folder, let's call them a, b and c.
I would like them to be zipped into a file with the name archiveYYYYMMDD.zip into a second (different) folder where YYYYMMDD is the current date. I'm struggling to come up with a suitable batch file.
I'm running Windows 7 x64 Ultimate. I have a scheduling program which would run the batch file at a preset time every day.
Thanks
Alan
This can be done with shareware archiver WinRAR with a single command line:
"%ProgramFiles%\WinRAR\WinRAR.exe" a -afzip -agYYYYMMDD -cfg- -ed -ep1 -ibck -inul -m5 -r -y -- "Path to Backup Folder\Backup_.zip" "Path to Folder to Backup\"
This single command line can be executed directly as scheduled task. There is no need for a batch file.
The help of WinRAR opened by starting WinRAR and clicking in menu Help on menu item Help topics explains under Contents menu item Command line mode the command line syntax, the command a and the used switches.

How to archive files older than 7 days with creating one archive for all files with same date?

I am looking for someone who can help me make a scheduled task to automatically move log files into RAR archives.
It does not have to be a batch file solution, if you have other ideas please share.
I got the basic code for it. This is the batch file code I have so far:
"C:\Program Files\WinRAR\rar.exe" a -ag -ms "D:\tet\Web3811\Web3811\LogsBackup\" #backup.txt
That line in the batch file runs RAR to create an archive with all files in the folder specified in list file backup.txt containing:
D:\tet\Web3811\Web3811\log
The RAR archive is created in D:\tet\Web3811\Web3811\LogsBackup\ with yyyy-mm-dd.rar as file name.
I need help with:
The RAR archives should have date in format dd-mm-yyyy in name instead of yyyy-mm-dd.
Only log files should be archived which are older than 7 days according to last modification date in comparison to current date whereby time does not matter, just date. All files with a date and time before 27-07-2014 00:00:00 should be added to the RAR archives if current date and time is 02-08-2014 12:30:00.
Each RAR archive to create should contain only files with same last modification date.
All archived log files should be deleted once the RAR compression is completed without errors.
The reason for being a batch file is the requirement of being executable as scheduled task.
An example for third requirement:
The folder contains 5 log files with following last modification dates:
Oldest.log 23-07-2014 02:20:54
AlsoOld.log 23-07-2014 23:52:26
Sample1.log 25-07-2014 09:08:46
Sample2.log 25-07-2014 12:59:02
Newest.log 26-07-2014 18:32:48
The scheduled task needs to create 3 archives with following names and files:
23-07-2014_Logs.rar with Oldest.log and AlsoOld.log.
25-07-2014_Logs.rar with Sample1.log and Sample2.log.
26-07-2014_Logs.rar with just Newest.log.
No log file was created on 24-07-2014 and therefore also no RAR archive to create for this day.
I suggest to use in the batch file:
"C:\Program Files\WinRAR\rar.exe" mf -ac -ao -agDD-MM-YYYY-NN -ep1 -idq -m5 -to7d -y "D:\tet\Web3811\Web3811\LogsBackup\Logs_" #backup.txt
Above command moves all files older than 7 days according to last modification date into a RAR archive with name starting with Logs_ and current date in requested format and an additional incrementing number starting with number 1 after a hyphen in case of running this command line several times on one day.
Only files with archive attribute are moved into an archive. The archive attribute is cleared after archiving a file even if deletion is not possible for example when another application has opened the file with a write lock. RAR does not delete files on which reading and compressing the data failed at all (read lock).
See text file Rar.txt in program files folder of WinRAR for a description of all the switches used in this command line.
After some requirements have been explained better, here is a batch file to create the archive files as finally requested.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Define the directories to use for backup task.
set "LogDirectory=D:\tet\Web3811\Web3811\log"
set "BakDirectory=D:\tet\Web3811\Web3811\LogsBackup"
rem Get all file names in log directory into a list file sorted by last
rem modification date with oldest file at top and newest at bottom.
rem Note: /S is important to get the file names with complete path.
dir "%LogDirectory%\*" /A-D /B /OD /S /TW 1>"%BakDirectory%\LogFiles.lst" 2>nul
rem Jump to clean up stage if no file found in the log directory.
if errorlevel 1 goto :CleanUp
rem Delete list file for all files with same day if file exists
rem for example from a previous execution of this batch file
rem which was terminated manually by a user during execution.
if exist "%BakDirectory%\DayFiles.lst" del "%BakDirectory%\DayFiles.lst"
set LastDate=none
for /F "usebackq delims=" %%F in ( "%BakDirectory%\LogFiles.lst" ) do (
set FileTime=%%~tF
rem Get just file date from file time in format DD-MM-YYYY.
rem The file time string format depends on date and time
rem format definition in Windows language settings.
rem Therefore the line below must be adapted if date format
rem is whether DD.MM.YYYY nor DD-MM-YYYY nor DD/MM/YYYY.
set FileDate=!FileTime:~0,2!-!FileTime:~3,2!-!FileTime:~6,4!
rem Is the last modification date of this file different
rem to last modification date of the previous file?
if not "!FileDate!"=="!LastDate!" (
rem Nothing to archive on first difference.
if not "!LastDate!"=="none" call :ArchiveLogs
rem Exit loop if RAR has not archived any file which means
rem all other files are modified within the last 7 days.
if "!LastDate!"=="ExitLoop" goto CleanUp
rem Start creating a new list.
set LastDate=!FileDate!
)
rem Append name of this file with path to current day list.
echo %%F>>"%BakDirectory%\DayFiles.lst"
)
rem Jump to clean up stage if no list file with files to archive.
if not exist "%BakDirectory%\DayFiles.lst" goto CleanUp
rem Otherwise with no log file created or modified within
rem the last 7 days, but at least one older file exists
rem nevertheless, archive all those files in list file.
call :ArchiveLogs
:CleanUp
del "%BakDirectory%\LogFiles.lst"
endlocal
goto :EOF
:ArchiveLogs
rem Move all files in the list file older than 7 days without
rem path using best compression into a RAR archive with last
rem modification date of archived file(s) in RAR file name.
"C:\Program Files\WinRAR\Rar.exe" mf -ep1 -idq -m5 -to7d -y "%BakDirectory%\!LastDate!_Logs.rar" "#%BakDirectory%\DayFiles.lst"
rem Exit FOR loop above if no file archived because
rem no file in the list file is older than 7 days.
if errorlevel 10 set LastDate=ExitLoop
del "%BakDirectory%\DayFiles.lst"
I first thought, it is not possible to do this without coding a small console application to create the file lists per date and ignore files not modified within the last 7 days. But then I had an idea on how to solve this main problem using just a batch file and RAR as it can be seen above.
It is best to run this batch file with a scheduled task short after midnight as RAR takes also current time into account for "older than 7 days" and not just the date.
But it would be no problem if batch file is executed for example at 18:00 and there are log files created respectively modified at 23:00. In this case log files with last modification date before 18:00 and with a date exactly before 7 days in comparison to current date are moved first into a RAR archive, and on next day the other log files last modified after 18:00 from same date are moved also to the RAR archive for this date.
Example with batch task executed always at 18:00 and what happens.
There are the log files
FirstSundayAugust2014_1.log 03/08/2014 15:23
FirstSundayAugust2014_2.log 03/08/2014 23:48
and the scheduled task runs on Sunday, 10th August 2014 at 18:00.
The batch file moves FirstSundayAugust2014_1.log into RAR archive 03-08-2014_Logs.rar, but the other log file FirstSundayAugust2014_2.log also from last Sunday remains in the directory.
On Monday, 11th August 2014 at 18:00 the batch file moves also FirstSundayAugust2014_2.log into the RAR archive 03-08-2014_Logs.rar and this archive contains now both log files created respectively last modified on first Sunday in August 2014.
One more note:
RAR file names with date in format DD-MM-YYYY are not really good in my point of view. Better would be YYYY-MM-DD as this results in *.rar files where those RAR files listed alphabetically according to file name in Windows Explorer would result in same list as when those RAR files are listed according to file date/time in Windows Explorer.
To get RAR files with format YYYY-MM-DD for the date in file name the line
set FileDate=!FileTime:~0,2!-!FileTime:~3,2!-!FileTime:~6,4!
needs to be modified to
set FileDate=!FileTime:~6,4!-!FileTime:~3,2!-!FileTime:~0,2!

robocopy monitor source, save versions

Is it possible to use the robocopy command with the monitor source switch to copy files with a new file name when they change?
I use the command below but would like to explore leave it running for several hours and capturing changes. In its current state the command overwrites changes in the destination folder.
report.txt can have several changes (say at 10:00 and 3:00) I would like to have each version saved as report_1000.txt and report_0300.txt.
robocopy \\temp\output c:\users\eric\desktop\robocopy\ report.txt /mon:1 /r:4000

ROBOCOPY puts the date 1/1/1980 on some copied files

I am having an intermittent issue with ROBOCOPY copying files with an incorrect date.
I am using ROBOCOPY to copy backup files from a local folder to a remote fileshare as a part of having a remote backup solution. The script is scheduled through task manager to run daily. Here is pseudo code:
ROBOCOPY E:\LocalFolder \\RemoteServer\FileShare\Folder *.bak
Most of the files copy with the correct file date, hovever one or two files sometimes will have the date of 1/1/1980. This presents a major issue with managing the backups in the fileshare because the dates are crucial to its management.
What might causes this?
What can be done to prevent this behavior?
I was having a similar issue. After some searching, I found reference to a behavior of Robocopy where it sets the modified date to 1/1/1980 until after a transfer is completed. [source]
What was really strange in my case was, if I watched the directory during the copy, I would see the file with the correct date appear, then AFTER it was complete the date would change to 1/1/1980. After some experimentation, I removed the /B switch I had been using and the dates seemed to be left alone.

Batch File to move files with the same string of characters into another folder with that string name

I am using Windows 7 Enterprise and have approximately 20 files per day for the last 365 days that I need to sort.
All of the files are in the same directory. Each file name also contains the date of the file's creation. The date is in the format MM-DD-YYY and starts at the 29th character of the file name. The files have the .csv extension.
I need to create a batch file to move all of the files with the same date into their own folder and onto another drive on my computer.
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.csv) do (
set fileName=%%~a
set datePart=!fileName:~28,9!
if not exist "D:\!datePart!" md "D:\!datePart!"
move "%%a" "D:\!datePart!"
)
This script extracts the date part of each file name and uses it as the name of the folder to move the file to. If the folder doesn't exist, it is created, then the file is moved to it.
As written, the script iterates over the .csv files in the current directory. This is specified by the mask in the for loop: *.csv. You can change the mask to include a specific path to process, like C:\path\to\*.csv.
The target drive is also hard-coded and assumed to be D:. Change the corresponding entries of the script if you need to use a different drive.
Detail information on every command used in this script can be obtained by calling the command's built-in help from the command prompt, using either of the below syntaxes:
command /?
help command