Xcopy certain file names only - windows

I've never used the Xcopy feature before and I was wondering if it would be possible to use xcopy to copy only certain files within a directory tree.
For example, suppose I have the following documents:
\servername\generateddocuments\2014\20141231\GUID1.doc
\servername\generateddocuments\2014\20141231\GUID2.doc
\servername\generateddocuments\2015\20150101\GUID3.doc
\servername\generateddocuments\2015\20150101\GUID4.doc
Now, suppose I have a spreadsheet that tells me which .doc files I need to copy:
GUID1.doc
GUID3.doc
Is there a way to base the xcopy on the spreadsheet(or txt document) so I don't copy the files I don't need?

I don't think xcopy can read files to include from a file. But you can create a batch file that does this:
for /F "tokens=*" %%A in (documents.txt) do (
copy %%A x:\targetfolder\
)

Try typing HELP XCOPY at a command prompt and look at the /EXCLUDE parameter. The documentation isn't quite correct, you can put a list of files in a single file, one file name per line, and they will be excluded from the xcopy.

Related

Windows batch copy files from subfolders to one folder

I had tried to make batch script that copies all *.tif files located in D:\images(random named subfolders here) to d:\all.
xcopy D:\Downloads\*.TIF D:\temp\ /s
works, but it copies with all folder tree. I tried to use other keys, but its dont works.
Thanks for help!
FOR is your friend. Read HELP FOR on the /R option and the %~nx variable substitution; and then try this very simple code.
pushd d:\downloads
for /r %%a in (*.tif) do (
echo COPY "%%a" "d:\temp\%%~nxa"
)
popd
watch carefully the results and then remove the ECHO command.
You will have to refine the code to cope with errors, duplicate names, edge cases, names with reserved characters, race conditions, cosmic events...
Searched files using windows file explorer for e.g. *.gif , I got files in search window, used Edit=>Select All , copy and then pasted to desired folder. This copied all the gif files in all sub directories to single folder.
For large number of files, it sometimes hangs/not responding, but otherwise works ok.
pushd D:\Source
for /r %%a in (*.?*) do (
MOVE "%%a" "D:\Destination folder\%%~nxa"
)
popd
You can also use the XXCOPY freeware. Works like XCOPY, but when you use a /SG parameter, it flattens the sub-directories. See how to use it here.

Windows batch file - Concatenate all files in subdirectories

Need to concatenate all of the javascript files in a directory and all of its sub-directories into one file.
Right now I have a very simple command in a batch file that concatenates all of the matching files in one directory into one file:
copy C:\javascripts\*.js concatenated.js
However, this only works for the one directory and not any of its sub-directories. How can I do this same thing and also include all of the matching files in the sub-directories?
Thanks so much!
From the command line you can use
for /r "c:\javascripts" %F in (*.js) do #type "%F" >>concatenated.js
You might want want to first delete any existing concatenated.js before you run the above command.
From a batch file the percents need to be doubled
#echo off
del concatenated.js
for /r "c:\javascripts" %%F in (*.js) do type "%%F" >>concatenated.js
EDIT
It is a bit more efficient to put parentheses around the entire statement and use a single overwrite redirection instead of append redirection with each iteration. It also eliminates the need to delete the file in the beginning.
>concat.js (for /r "c:\javascripts" %F in (*.js) do #type "%F")
or from batch
#echo off
>concat.js (for /r "c:\javascripts" %%F in (*.js) do type "%%F")
I'm not aware of an approach to do that from a batch file, but you could use a tool like minify
http://code.google.com/p/minify/
to both combine JavaScript files and also minify them (e.g. by removing comments and unnecessary white space).
There are quite a few similar tools for a variety of programming environments.

converting xcopy to robocopy

Tried to use xcopy and excluding a folder and all its subfolders.
C:\Merged\org\a>xcopy /I /E /Y C:\Merged\org\*.* C:\Merged\dest /exclude:"C:\Mer
ged\org\a\*.*"
Can't read file: "C:\Merged\org\a\*.*"
I guess I cannot exclude folder, but only file with xcopy.
So I think of moving to robocopy, but I'm not sure which flags to use.
What is the equivalent robocopy to my above xcopy ?
Well, you can. Read up on XCOPY /?:
/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.
The problem is you specify the path you want to exclude to /EXCLUDE when you should specify the name of a file, which contains the "paths" to exclude (it is actually more flexible see above). The error message you get even hints at that.
Create a file, e.g. C:\ignore.txt, that contains the line "C:\Merged\orga\a\", then invoke XCOPY as follows:
xcopy.exe C:\Merged\org\*.* C:\Merged\dest /exclude:C:\ignore.txt /I /E /Y
If you want to use robocopy nevertheless, you can do so like:
robocopy.exe c:\merged\org c:\merged\dest /xd c:\merged\org\a /IS /E
I has been wrote seemless wrappers from xcopy command line to the robocopy. You can find it from here: https://github.com/andry81/contools.
The scripts:
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_dir.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_file.bat

Replacing a file into multiple folders/subdirectories

Is there a way in command prompt to take one file and copy it into another folder and its subdirectories based on its name?
I have an image named 5.jpg that has been put in a sub-folder that is in every folder in a directory.
I want to do a search inside of the folder (with the old image) and its sub-folders and replace all of the results with the new image.
Probably there is one more (simpler) way.
Use the replace command:
replace C:\SourceFile.Txt C:\Some_Root_Folder_Which_Contains_Multiple_SubFolders /s
As the command itself says it just replaces the file, which already existed in sub-directories.
I'm not sure if I understood you completely. The following code will search for all occurences of 5.jpg in subfolders of C:\MyPath\ and replaces them with C:\NewImage\5.jpg. I did test it, so it should work.
FOR with parameter /R will help you here:
FOR /R C:\MyPath\ %%I IN (5.jpg) DO COPY /Y C:\NewImage\5.jpg %%~fI
If you want more information about what FOR /R does and what %%~fI means, have a look at FOR /? | more which gives nice explanations about the new Windows cmd possibilities that are used here.
To do this work with several files, both paths are needed enclosed in quotes:
replace "C:\*.Txt" "C:\Some_Root_Folder_Which_Contains_Multiple_SubFolders" /s
The asterisk to make changes on all files with the ".txt" prefix.

DOS command to replace all instances of <filename>.config

i have an edited version of a config file specific for my machine.
i have the same config file in multiple different directories in my development folder.
i want to, in a single bat file, replace all instances of this file with my edited one.
So in pusedo code:
Take C:\edited.config and copy to C:\Projects\ /s wherever original.config is found
i want the final file to have the name of original.config, not edited.config
so i am guessing i need some combination of a FOR, a rename and copy or something like that
is this easier to do in Powershell?
can anybody help?
Thanks
I blogged about this a little bit ago at http://jamesewelch.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/
I think your solution will look something similar to (below is untested but used to show general idea)
for /f %%a IN ('dir /b *.config') do copy c:\master.config %%a
There's probably a switch there on the copy to suppress file overwrite warnings, but I don't remember what the switch is. This will copy your master.config and overwrite your local file (variable of %%a).
I'm amazed what DOS batch file experts make work. Since I'm not one of them, I take an approach that's pragmatic for me. It might work for you as well.
Get a list of destination folders
C:
Cd\
Dir original.config /s > original.bat
Edit original.bat in your favorite text editor (I like Notepad++)
Search for "original.config" and replace with "" (empty string)
Insert the text "Xcopy C:\edited.config " at the front of each line
Proof-read the result to be sure it's what you want. If you're not sure put an "Echo " in front of each line for a dry run.
Run the batch file.
#echo off
C:
cd \Projects
FOR /F "tokens=*" %%G IN ('DIR /B /S original.config') DO xcopy /y c:\edited.config %%G

Resources