Replacing a file into multiple folders/subdirectories - windows

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.

Related

Xcopy certain file names only

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.

Copying a file to multiple folders in the same directory

I have a file, lets call it EXAMPLE.DBF
The location of this file is C:\EXAMPLE.DBF
Now I would like to copy this file into multiple folders at another location.
These folders are dated Sub-directories so they are named 20140101 - 20141231 and their location would be in d:\bootd\hello\20140101 - 20141231
Not only that but a file named EXAMPLE.DBF already exists in the folders...so it will ask to Copy and Replace.
I will need c:\EXAMPLE to copy and replace the existing EXAMPLE.DBF in 365 folders (20140101-20141231) in a different location.
If anyone could help me out with this I will be truly grateful.
Directly from the prompt,
for /d %a in (d:\bootd\hello\2014*) do copy /y C:\EXAMPLE.DBF %a\
Will copy C:\EXAMPLE.DBF to each directory of d:\bootd\hello\ which matches the pattern 2014* (ie. simply starts 2014), replacing any existing example.dbf in that subdirectory, if that is what you want to do.
To suppress the 1 file(s) copied messae generate for each, simply append >nul to the above line.
Just a small addition to #Magoo's answer;
in case the destination folders have spaces in their names, use double quotes like this:
for /d %a in (d:\bootd\hello\2014*) do copy /y C:\EXAMPLE.DBF "%a\"
and as pointed out by #ian0411, in case source folder has spaces in its name, use double quotes like this:
for /d %a in (d:\bootd\hello\2014*) do copy /y "C:\EXAMPLE.DBF" %a\

Batch file to copy files

Need to have a batch file that will actually check if the file name has "abc" in its name and if its there , then it will initiate a copy.
So suppose folder /test has 5 files
apt.text
mar.text
may.text
Jan.text
abc_xyx1234.text
So since "abc" appears in string only in the last file , it should pick only last one.
I tried
#echo off
for /f %%a in (' dir /b /a-d ^| find /v /i "abc" ') copy "%%a"
pause
but it didn't work out.
You have no destination for your copy.
The find /v /i will find those filesnames that do not (/v) match abc
It's probably easier to use
copy *abc* destination
I think part of your problem is the following:
Even though forward slashes work sometimes, Windows' canonical path separator is the backslash \. Using forward slashes as path separators confuses programs which use the forward slash as parameter prefix, and most if not all of the Microsoft Windows tools do so, including FIND and FINDSTR.
Also, if you're talking about /folder - which should be \folder as explained above - you're talking about a directory in the root of the current drive. I'm not sure you really mean that.
Apart from that, Magoo's suggestion is still kind of valid, but it omits the path "problem".
So let's use absolute directories to clarify:
I'll assume your source directory is c:\work\source and contains the file set you've stated in your question. I'll assume your target directory is c:\stackoverflow\target. So you can do the following from anywhere on your system:
copy c:\work\source\*abc* c:\stackoverflow\target
If you're doing this from an arbitrary folder on drive c, you could omit the drives:
copy \work\source\*abc* \stackoverflow\target
And, if you're inside the source folder, you could use Magoo's answer:
copy *abc* \stackoverflow\target

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.

Copy a directory tree to a single directory at a command line

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.

Resources