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.
Related
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.
We have a file share that has a directory containing all of our build version direcotries named like this
WebApp_20140702.1 first number being date second number being the build count for that date
these are then contained in the following directory
\\server\share\product\
What i need to do from a virtual machine is create a batch file that can check the target location on my vm ie. c:\product\ see if it has the latest version from the network share either by comparing the file names or dates, then copy the new version and delete the old if necessary.
So far i can copy the folder over using xcopy but that's about the extent of my dos/batch file knowledge iv had a look around for a while but haven't been able to see anything that i can use
This is what i have so far, as you can see though i dont know how to do the comparison between the two directories as explained above.
xcopy "\\server\share\webapp" "c:\users\username\desktop\webapp" /E /K
I did try to use just /D at the end and just copying the directories from \\server\share\product\ that had a later date than the target but it ended up just copying the whole directory.
EDIT : to make my self clear
i need to find out if i have the latest sub directory but no matter what i do it always copies all the sub directories from \\server\share\product\
ie. the \\server\share\WebApp directory will have the following sub dirs
..\WebApp_20140628.1\
..\WebApp_20140628.2\
..\WebApp_20140703.1\
and my vm will have the directory
c:\product\WebApp_20140628.2\
Now i need to be able to go into the file share see that it has a more up to date subdirectory i need to copy that directory to my vm and then delete the older one from my vm so i would then have
c:\product\WebApp_20140703.1\
OK i eventually found another question that wanted to do a similar thing and the answer worked exactly as i wanted it
Question can be found here: How to get the most recent file using a batch script in windows
i had to use xcopy instead of the copy used in the answer for the above question. here is my solution as well in case anyone needs something similar
(z is the mapped version of the network share i talk about in my question)
pushd "z:\WebApp\"
for /f "tokens=*" %%a in ('dir/b /od') do set newest=%%a
xcopy /e /k "%newest%" "c:\product\"
popd
im not sure if i actually need the popd command as i believe it just goes back to the directory set in the pushd command
I'm using a robocopy batch script to move files from one server to another,
but I need it to delete the files on the original server after it is done
(which should happen when you use /move).
The copying works fine, but the files and folders aren't deleted afterwards.
Can anyone tell me what might be going wrong?
Command:
robocopy "\\Srv04\data\logs" "F:\Logs" /move /S /minage:8
thanks,
Gotcha:- Please note that using ROBOCOPY with the /MOVE command other than permission problems won’t remove source directories and files if those items already exists at the target because they will be skipped.
You can get around this by quoting a new target destination.
Question solved: It was a permission problem
Anyone know of a command line utility (or one that can run as a command line) that will collect all the .jpg files in a directory tree to a single folder, only copying files that change?
I started with Renamer, which is great for renaming files in their current directories, but fell short when I tried to mangle the path. This is probably because I don't know Renamer that well. I ended up creating a text file directory dump, then using a REGEX find / replace to create a batch file, but this is hardly efficient nor automated.
The REGEX:
(G:\DIR\DIR\)([0-9]+\)([0-9]+\)([0-9]+\)([0-9]+\)(p[0-9]+.jpg)
changed this
G:\DIR\DIR\00\00\00\00\p0000000000.jpg
to this
G:\DIR\DIR\p0000000000.jpg
(copy \1\2\3\4\5\6 \1\6) in the batch file.
I need to run the whole thing as a scheduled task without a real person logging in. Not really looking for a Zip file because I don't want to disturb the system processor, plus most of the files will not change from day to day. This is more of a file sync.
In a Windows command line you can do this:
for /R A %i IN (*.jpg) DO xcopy %i B /M /Y
Where A is the source directory and B is the destination directory. You need to have command extensions enabled, which I believe is the default.
A couple of notes from the comments:
If any of your paths could have spaces in you will need to add quotes around the second %i. This prevents the string being interpreted by the xcopy command as two separate parameters. You may need to do the same around A and B paths. Like this:
for /R "A" %%i IN (*.jpg) DO xcopy "%%i" "B" /M /Y
If you are putting this inside a .bat or .cmd file you will need to double the percentage like this.
for /R A %%i IN (*.jpg) DO xcopy %%i B /M /Y
The /M option on xcopy will only copy files with the Archive bit set and then unset this bit. This prevents the files being copied twice. If you have other processes that also alter this bit it may cause issues. It is also possible to use the /D option which compares the file's last modified time with that in the destination and only copies newer files.
I'm guessing you're on Windows from the path format.
I've not read the whole thing, but http://www.infionline.net/~wtnewton/batch/batguide.html#6a might help you.
The same page has dizzy.bat, (http://www.infionline.net/~wtnewton/batch/dizzy.bat) which should be trivial to edit to do what you want.
In a Unix environment I would use find or rsync (and maybe some features of the shell). Cygwin and MinGW come with find, maybe with rsync. You can also probably get a standalone port of find for Windows somewhere.
If the SOURCE shell variable is the directory containing subdirectories with files to copy, and the DEST shell variable is the directory to copy them to:
find $SOURCE -name \*.jpg -exec cp --update \{\} $DEST/ \;
find is by nature recursive. "-name \*.jpg" selects files that match that pattern. You can add additional conditions with -and. The --update option to the cp command (or -u) only bothers copying the file if changed or not yet copied. There are other options to cp that might be useful too.
If $SOURCE is the same as $DEST as in your DIR/DIR/ example, then find will also find the destination files (already copied), though this will be ok, cp will recognize that you are trying to copy the same file to itself and skip it, but if you want to avoid that wasted work you can use 'for' and 'if' (or something) to only run find on the subdirectories of DIR/DIR/.
You can also use rsync, which has options that can delete files from the destination directory if they have also been deleted from the source directory, and many other such variations.
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.