I was wondering if there is a quick and dirty command that can copy all files in a directory and it's subdirectory to another folder. The destination folder will have no subdirectories just all the source files.
Also as an added bit of fun, in case there is a file name conflict, not to overwrite but to rename the destination file with something unique, maybe append _1 to the filename?
This will copy the files and prompt if there is a filename conflict.
The third party tool XXcopy has the ability to flatten a directory tree and handle filename conflicts.
#echo off
for /r "d:\folder" %%a in (*) do copy "%%a" "E:\target folder"
To copy for instance all files from the current folder and its sub directories to the parent folder of the current folder you can use:
for /r . %a in (*) do copy %a ..
Related
for /r "C:\Users\bui\Desktop\Annotation Traing Package\test1" %i in (*03.json) do copy "%i" "C:\Users\bui\Desktop\Annotation Traing Package\test2"
i want to copy all 03.json files from test1 to test2 but in test1 folder, there are subfolders, and in those subfolders also have 03.json files. With for loop i cant loop through subfolders and copy 03.json files in it, but i also want to copy those subfolders from test1 to test2 with those 03.jon files in it. is there possible way to do it?
i tried using xcopy to copy those subfolders first then use for loop but no use, bc they copy all files to "C:\Users\bui\Desktop\Annotation Traing Package\test2"
RoBoCopy is the best method to do this IMHO.
Robocopy "C:\Users\bui\Desktop\Annotation Traing Package\test1" "C:\Users\bui\Desktop\Annotation Traing Package\test2" *03.json /E /S /B
The Robocopy utility is made to copy directory trees and matching files and has been included in windows since Windows 7 released in 2009.
Note I used /E assuming you would want the folders which are empty assuming you may have things like log folders which contain no "03.json" files
If you do not want to create empty directories when they do not contain "03.json" files, then simply omit the /E option
I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)
I want to copy all files within subfolders to the parent folder. But want to create a new folder in it by the name "copied" in whichever folder i have copied my batch file. I tried this command.
FOR /R %%# IN (*.*) DO XCOPY "%%#"
It does the job but it keeps asking to rewrite files. Also wasn't sure how to create the folder name in which i have copied my batch file into.
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\
Does somebody know a way to move a folder from one path to another with the "parent" folder ?
For example, all these folders:
Contain a folder named "Win" see below:
i want to copy the folder "Win" with the parent folder ( the numbers ) and move it to a new location example -> "C:\Storage\migration"
there are over 200 folders, so right now i am manually creating a folder named these numbers and copy & pasting the Win folder into it, it is very time consuming and not a very good long term solution :(
Maybe something could be done with a BAT. script or like ?
for /d %%a in ("d:\BaswareRay\OES\*") do (
xcopy "%%~fa\win\*" "c:\storage\migration\%%~nxa\win\" /y /s /e
echo rmdir /s /q "%%~fa\win"
)
For each folder under the indicated source, recursively copy its contents to the target folder and remove the source folder.
For each folder, the for command replaceable parameter %%a will hold a reference to the folder being processed. Using this reference, %%~fa will return the full path of the folder and %%~nxa the name and extension of the folder.
The deletion of source folder is only echoed to console. If the output is correct, remove the echo that precedes the rmdir command