copy files created or modified today with robocopy - batch-file

I'm trying to create a batch file in Win7 that will copy any files that have been created or modified today and copy them to a destination with a similar directory structure. This is what I have so far:
set today="20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MINAGE:%today%
I know that /e copies empty directories and /xf excludes all files, but I'm not sure if that helps me. The code above seems to copy all files regardless of date, so I'm a little lost here.

Assigning quotes to your variables is not a best practice and will cause problems with some commands if you try to quote the variable later on. Regardless that was not your problem. Your problem is you need to use the /MAXAGE option. Reading the help file you should see this:
/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.`
So your code should be:
set "today=20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MAXAGE:%today%
Going to assume you thought the options were for INCLUDE.

robocopy's /MINAGE//MAXAGE options regard the full date and time, so specifying something like /MAXAGE:1 filters for files that have been modified within the last 24 hours.
If you want to process files which have been modified today only, hence regarding the date but not the time, you could use forfiles and its '/D' option, like this:
set "DEST=D:\backup\temp"
forfiles /P "C:\temp" /D +0 /C "cmd /C if #isdir==FALSE for %%Z in (#relpath) do #(2> nul md 0x22%DEST%\%%~Z\..0x22 & copy #relpath 0x22%DEST%\%%~Z0x22)"

Related

how to copy only modified files using LogFile.txt?

when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.

BAT File to find most recent files based on filename

I need to be able to create a bat script to do 3 things:
Search for multiple specific filenames in a directory.
Find the most recently generated version based on each filename specified.
Copy that most file to a new dir.
I am very new to coding in general, so any assistance would be much appreciated.
So far all I have been able to do is figure out how to copy files from one location to another using the below:
xcopy /s c:\source\differentfilename1.csv d:\target\
xcopy /s c:\source\differentfilename2.txt d:\target
xcopy /s c:\source\differentfilename3.html d:\target
So far I have tried the following and its not copying the files over:
ECHO
CD D:\Data\
MKDIR D:\Data\CopyFilesHere
for /R %file in (Filename1.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename2.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename3.*) DO XCOPY "%file" D:\Data\CopyFilesHere
I have since noted there are subfolders I need to search through also.

Excluding folders from %% parameter in Windows command line

I would like to modify the following code to better target a set of folders.
I have the following folder structure:
C:\Root\Dir1\Data, C:\Root\Dir2\Data, ...
D:\Root\Dir1\Data, C:\Root\Dir2\Data, ...
The C: contains working copies, and the D: contains backup copies to be restored on-demand. With the help of #Marged, I utilize the following batch file:
for /d %%g in ("C:\Root\*") do rd /s /q "%%g\Data"
xcopy /s /q "D:\Root" "C:\Root"
pause
I now need a way to specify %%g to exclude folders in C:\Root\*. In example, I want line 1 to delete C:\Root\Dir1\Data and C:\Root\Dir2\Data but not C:\Root\Dir3\Data. It is required to make the exclusions explicit, as the set of inclusions change. I also need line 2 to better target folders. However, I believe that can be accomplished with XCOPY's /EXCLUDE:"D:\Root\Dir3" switch.
Thank you very much for reading and in advance for any discussion! :)
... do if "%%~g" neq "C:\Root\Dir3" rd ...? – Stephan

Using robocopy with source and destination as variables

Completely new to scripting, tried to find a solution through searching the web but I'm stumped, so asking for help please!
I'm trying to use robocopy with a variable as both the source and destination, different one for each, but I just can't get the right syntax.
The hard code which I can get to work fine is:
robocopy C:\Users\me\Documents\a C:\Users\me\Documents\b
But I can't get it to work with variables. I've tried, after reading around, what seems like all variations of the below with " and % in/out, with and without Set, and none of them have worked.
Set src="C:\Users\me\Documents\a"
Set dest="C:\Users\me\Documents\b"
robocopy %src% %dest%
Hope that's a clear explanation of what I'm trying to do, if not ask for clarification.
Thanks in advance for any help.
set "src=C:\Users\me\Documents\a"
set "dest=C:\Users\me\Documents\b"
robocopy "%src%" "%dest%"
Nothing bad with your sintax. This way is "more robust" or more standard or more habitual, ...
BUT robocopy is not copy not xcopy. You are asking robocopy to copy from source to target changed or new files. If there are no changes, robocopy will not copy anything. If you have tried and it worked, .... if no changes, no file copy.
AND you have not asked robocopy to copy subdirectories. So, if there are no files in source directory, nothing will be copied.
I have found Robocopy is touchy to the point of being arbitrary about syntax. I have found a similar problem to yours:
This code works:
Set Today=%DATE:~0,3%
Robocopy "G:\folder A" "U:\%Today%\folder A" ^
/S /XJD /R:25 /W:4 /NP /copyall ^
/LOG:"U:\%Today%\FolderALog.txt"
IF ERRORLEVEL 8 goto Badend
This (nicely structured) code doesn't work
Set Today=%DATE:~0,3%
Set source="G:\folder A"
Set target="U:\%Today%\folder A"
Set Logname="U:\%Today%\FolderALog.txt"
Echo Source is %Source%
Echo Target is %Target%
Echo logfile named %Logname%
Pause
Robocopy %source% %target% ^
/S /XJD /R:25 /W:4 /NP /copyall ^
/LOG:%Logname%
Pause
However, in this 2nd example, take the 1st continuation out of the command line and it works:
Set Today=%DATE:~0,3%
Set source="G:\folder A"
Set target="U:\%Today%\folder A"
Set Logname="U:\%Today%\FolderALog.txt"
Echo Source is %Source%
Echo Target is %Target%
Echo logfile named %Logname%
Pause
Robocopy %source% %target% /S /XJD /R:25 /W:4 /NP /copyall ^
/LOG:%Logname%
Pause
I've been using the caret (^) as a continuation character in batch command jobs ever since the DOS days, but in this instance the parser tries to concatenate it with the previous variable and the job dies because the system thinks I'm trying to name a folder "U:\%Today%\folder A ^". So goes it -- you keep trying things until something works.
Troubleshooting techniques: Doing Echos of newly defined variables then pausing allows you to check for typos and misplaced quote marks. The pause at the end gives you ample time to read the error code, should there be one. Another thing I ran into once was inadvertently inserting an unprintable character in place of a space in a path enclosed with quotes.
RoboCopy is very powerful and well worth occasional tinkering with touchy syntax.
Looks like an old question, but I ran into this issue myself today and solved it by using double slashes for the set command:
Set src="C:\\Users\\me\\Documents\\a"
Set dest="C:\\Users\\me\\Documents\\b"
robocopy %src% %dest%
Try this way
Set src=C:\Users\me\Documents\a
Set dest=C:\Users\me\Documents\b
robocopy %src% %dest% /E
/E - for copying of Subfolder including empty subfolder
If you are trying to use a .bat the code will be like this:
Set src="C:\Users\me\Documents\a"
Set dest="C:\Users\me\Documents\b"
robocopy.exe %src% %dest%
You forgot to put the .exe
dude try the XCOPY command it will work for sure!
example:
xcopy "C:\Users\me\Documents\a" "C:\Users\me\Documents\b"
it will ask is the destination is a folder or a file, so once u chose folder (dir) it will copy!
hope it helps! :D

How to copy all files created between two dates using Command prompt?

I have written code in window batch file (eq. getFiles.bat ) that get all files within select date range.
eq.
xcopy /S /D:01-10-2011 *.* C:\todaysFiles
But I want get all files in between two dates including From date and To date.
file extension is .cmd or .bat
If you're on Vista/Win7/WinServer2008 you can use robocopy like so:
robocopy c:\source c:\destination *.* /MAXAGE:20101231 /MINAGE:20111001
On XP, I'm not sure if there are built-in solutions short of using Powershell and the like.
The title of this question is slightly misleading. Based on the questioner's xcopy example, I believe the questioner wants to know "How to copy all files created...", not how to get all files (which I equate with list).
The questioner also states "file extension is .cmd or .bat". This could mean that the questioner wants to copy only files with these extensions but I think it means the questioner would like a batch script solution.
To use the following solution, open a command prompt and chdir to the folder where the files to be copied are located. Then enter the commands listed below, changing the SET values as appropriate.
SET DESTINATION=C:\destination
SET DATE_FROM=01/01/2005
SET DATE_TO=01/01/2007
> nul forfiles /S /D +%DATE_FROM% /C "cmd /C if #isdir==FALSE 2> nul forfiles /M #file /D -%DATE_TO% && > con ( echo #path && copy /V #path %DESTINATION% )"
Note: this will copy files in subfolders as well as files in the top-level folder.
The SET values could be hard-coded directly into the > nul forfiles... line, meaning only one line is required, but for clarity I've used variable substitution.
A caveat is that it is based on date modified (original question asked for date created)
Credit to aschipfl (https://stackoverflow.com/a/36585535/1754517) for providing the inspiration for my answer.
You can also use the forfiles command. It can search recursively in all subfolders /s for files created in the selected root folder /p <Path>, execute commands on the selected files /c "<Command>", apply masks to search /m <SearchMask>, between a date range /d [{+|-}][{<Date>|<Days>}]
more info here, and here
Here's a batch for viewing files by creation date by year. Easily changed to cmd prompt by removing the extra percentage symbols.
for %%a in (2011 2012 2013 2014 2015 2016 2017) do (
for /f %%i in ('xxcopy i:\podcasts\*.* /LL /ZS /Q /FC /DA:%%a-01-01 /DB:%%a-12-31 ^| find /c /v ""') do echo %%a: %%i
)

Resources