I need to copy files from one location to another location, but only copy new files, or files that have changed.
For instance, I have data in C:\Working which includes folders and files. The files are changed, and removed from there as they are no longer needed or a project is complete.
I need a batch file to move everything in that location to D:\Storage. Since some files may not have changed, I do not want to copy those, however, I do want to copy and replace files that have been modified.
I believe I can use Robocopy /E and that will give me the recursion that I need. I am not sure on how to check hashes or stamps to verify if the file has been changed. I know I can EXCLUDE files with the /XC, but I think that's the opposite of what I want.
Right now my file as as folows:
#echo off
pushd C:\Working
>nul Robocopy /E . D:\Storage
popd
Edit: I don't want to just copy everything because the working location may have upwards of 60GB in it, and only 1-2GB needs to be copied.
http://ss64.com/nt/robocopy.html
By default Robocopy will only copy a file if the source and destination have different time stamps or different file sizes.
Therefore, Robocopy . D:\Storage /E should be fine. You may want to add the /XO option depending on what you want.
Related
I have a folder with approximately 7000 subfolders. I am interested in copying 1500 of those subfolders and their file contents into a new folder.
The closest I have got is copying the subfolders file contents into a new Target folder. However, the file contents are not copied over within their resepective subfolder making the batch copy useless to me.
Here is what I have tried.
CD E:\Source_Folder
FOR /F "delims=" %%N in (List.txt) do COPY "%%N" "Target_Folder"
I have tried XCOPY, ROBOCOPY, as well and all give me the same output of individual files in my target folder. I am looking for the subfolders with their contents to be copied into my new target folder.
Any help would be much appreciated. Thanks!
Just a try: rsync in linux does have the option to take the file-list to be synced from a file. Maybe robocopy (which is the windows-pendant) has the same capability, then you need one robocopy x y z only.
-edit: robocopy can'r read from a file, but if you search for 'robocopy reading from list' you get a lot of short scripts.
How can I copy all files/folders in a directory using a batch file but omit one? I know I could always delete it after the fact but it is an extremely large folder and I don't want to waste the time copying it. There are numerous files in the location I want to copy so doing it one individually would be unrealistic. Thank yoU!
robocopy "source" "destination" files_to_copy.ext /e /xd "folder_to_exclude"
robocopy is an upgraded version of xcopy, thus, is more useful. You can check the complete usage by typing robocopy /?, or from the microsoft docs, or from ss64.
EDIT: Almost forgot, robocopy accepts wildcards, so you can type *.* to copy every files. The line I suggest already have the option /e, which will copy all subfolders, even the empty ones. /xf:file_to_exclude.ext can also be used to skip a specific file type, eg. /xf:"*.txt" to exclude all the .txt files.
xf is used to exclude files, xd to exclude folders.
i want to copy the WHOLE folder in a new subfolder. currently i am using:
(robocopy "C:/Test" "C:/Test/a" /E> output.log) ^& IF %ERRORLEVEL% LSS 8 SET ERRORLEVEL = 0
after running this, there is in "C:/Test/a" also the "/a" folder. i dont want that new folder.
The robocopy command seems to get confused when a full source directory tree is to be copied and the destination directory is located somewhere in the source directory tree. Therefore your command line results in a directory C:\Test\a\a that partially contains stuff from the source.
Try with the /L option (do not copy but list items that would be copied), so the output log shows exactly what you would expect. As soon as you actually do copy (so with /L removed), there are some more items listed in the log unexpectedly. This behaviour indicates that robocopy does not evaluate the whole source directory tree in advance, but during the actual copying operation.
The easiest way to avoid that is to exclude the destination directory from being (re-)copied:
robocopy "C:\Test" "C:\Test\a" /E /XD "C:\Test\a"
By the way, the correct Windows path separator is the \. Although robocopy also accepts /, you should stick to \, because many commands do misinterpret the /.
I am trying to write a Windows Batch file that will allow me to move all directories within a given source directory into a target directory that exists within that source directory.
Obviously my move command with need to only apply to directories and also exclude the target directory from being processed.
Is this possible with a Windows batch command?
Robocopy (present in recent versions of windows or downloadable from the WRK) can do this, just use the /xd switch to exclude the target directory from the copy;
robocopy c:\source\ c:\source\target\ *.* /E /XD c:\source\target\ /move
FOR /d %%i IN (*) DO IF NOT "%%i"=="target" move "%%i" target
That won't work - you'll get an error telling you the target directory is inside the source directory or so, even if you explicitly exclude the target directory. What you can do is move the directories to a temporary location which is not under the source, and then move them into the target.
BTW, using the move command won't let you specify folders to exclude. For that you can use xcopy, but note that it will copy the folders, as opposed to move them. If that matters, you can delete whatever you want afterwards, just make sure you don't delete the target dir, which is in the source dir...
Using robocopy included with Windows 7, I found the /XD option did not prevent the source folder from also being moved.
Solution:
SET MoveDirSource=\\Server\Folder
SET MoveDirDestination=Z:\Folder
FOR /D %%i IN ("%MoveDirSource%\*") DO ROBOCOPY /MOVE /E "%%i" "%MoveDirDestination%\%%~nxi"
This loops through the top level folders and runs robocopy for each.
NB: Robocopy mentioned above using the /move flag will copy the files and then delete them from the source folder rather than moving the files. This may be critical if moving large numbers of files from one location to another on the same disk (because move is virtually instantaneous, while copying is a much slower operation)
On windows batch:
FOR /d %%i IN (MySourceDirectory\*) DO move "%%i" MyTargetDirectory\%%~ni
The above command moves all directories found in MySourceDirectory (/d) to MyTargetDirectory using the original directory name
(~ni) Robocopy's move first does a copy, then delete, so it is slower.
This works for me:
move c:\fromDir\*.* c:\toDir\
I'm trying to create a script that will automatically backup a complete directory tree. I also want it to work for incremental backups. Basically, it wil work like this:
If file is in both source and destination and they are different, source file will be copied
If file is in both source and destination and they are the same, nothing will be copied
If file is only in the source, source file will be copied
If file is only in the destination, destination file will be deleted.
I'm still new to shell scripting and I'm not sure how I could implement this. Any ideas? Windows batch scripts would be better, but shell scripts that run on Cygwin are also fine.
Take a look at rsync tool - it was designed to minimize traffic during files synchronization.
Also, probably "cp" with "-u" argument will be useful:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
For windows batch scripting xcopy (native to windows) or robocopy (a free download in windows) both work extremely well.
An example xcopy script (in a .bat file):
#echo off
:: /L = list only.
:: /v=Verify, /y=No prompting /s=subdirs /z=Network mode (supports bad)
:: /i=Tells xcopy dest is folder /f=Display names /d=Copy only changed
echo Backing up projects...
xcopy e:\projects h:\projects /V /Y /S /Z /I /F /D
It will even support orphaned files (if you delete something from your source you no longer need a copy in the backup). Xcopy is typically fine for your needs until you deal with sync between NTFS and Fat32 file systems - the later only has a 2 second resolution and problems with daily savings time so you occasionally run into issues: (a) on time change day you might not get a backup of a changed file - depends on change-regularity of course or you might get a backup of all files even though none have changed (b) because of time resolution some files may backup even though they haven't changed.