windows batch copy * but omit a single folder - windows

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.

Related

Xcopy with excluding folders (sub-directories)

I want to copy files and folders in a directory to another folder excluding sub-folders with files contains it, as for example I have a large number of files for node_modules directory which like 100Mb with 50K+ files, that I don't need to copy.
I tried using xcopy like this :
xcopy . c:\inetpub\CIVEBuildCentral\UI\. /Y /S /EXCLUDE:CIVE\UI\elist.txt
and elist.txt contains :
\node_modules\
But no luck, and its really annoying syntax and I don't see its optimal to check-in such a useless file for this case.
Any idea how to solve this?
Well, after searching I found a similar question in StackOverflow but was not so helpful for my case:
Xcopy Command excluding files and folders ( its marked as duplicated but actually it's not really duplicated, it is the different case even if answer seemed the same way, and found no answer for my case anyway)
But I found that if you using Windows 7 or later, you can use robocopy instead, found its so powerful tool compared to old man xcopy, and no need to dirty work for exceptions, the command to achieve what I need replaced xcopy with :
robocopy . c:\inetpub\CIVEBuildCentral\UI\. /IS /S /XD node_modules
For full documentation for it, you can see this link: http://ss64.com/nt/robocopy.html
Solved my issue, and the output of the result was so nice, clear, and well formatted.

Need a batch file to copy new and changed files

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.

Windows batch command to move all folders in a directory with exceptions

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\

Incremetal backups of directories with a batch/shell script

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.

xcopy /exclude issue

I am trying to run xcopy that copies files excluding .obj, etc.
What I am seeing is that Microsoft.Practices.ObjectBuilder.dll is not copied when my excludes.txt file contains .obj as an extension. When .obj is removed, I Microsoft.Practices.ObjectBuilder.dll is copied correctly. This does not happen to other dlls though.
Does anyone have any idea why this would happen?
Thanks!
Lenik
Yeah, xcopy is dumb like that.
Do this:
dir /b *.obj >excludes.txt
xcopy * /exclude:excludes.txt targetdir
although this will still have the problem sometimes.
If you had a file called practices.obj, for example, it wouldn't copy that, but it would also fail to copy your Microsoft.Practices.ObjectBuilder.dll
A handy trick is if you specify /s on dir, you get recursion and the full path, then if you specify the source directory fully on the xcopy, the excludes will have to match from the beginning:
dir /s /b *.obj >excludes.txt
xcopy c:\sourcedir\* /exclude:excludes.txt \targetdir
Now Microsoft.Practices.ObjectBuilder.dll would only fail to copy if you happen to have a Microsoft.Practices.obj file in the same directory. Get it?
I guess because the substring .obj is found in the name Microsoft.Practices**.Obj**ectBuilder.dll and since windows is not case sensitive, it will exclude it.
XCOPY is deprecated now anyway, so I doubt things are going to get fixed. Take a look at ROBOCOPY - it's built into Vista, and comes in the resource kit for 2003 and XP.
The answer is what you could obtain by typing:
xcopy /?
Namely:
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.

Resources