- How to copy a directory structure but only include certain files (using windows batch files)
- 15 Answers 15
- Batch file to copy files from one folder to another folder
- 9 Answers 9
- copying all contents of folder to another folder using batch file?
- 12 Answers 12
- batch/bat to copy folder and content at once
- 5 Answers 5
How to copy a directory structure but only include certain files (using windows batch files)
As the title says, how can I recursively copy a directory structure but only include some files. E.g given the following directory structure:
The files data.zip and info.txt can appear everywhere in the directory structure. How can I copy the full directory structure, but only include files named data.zip and info.txt (all other files should be ignored)?
The resulting directory structure should look like this:
15 Answers 15
You don’t mention if it has to be batch only, but if you can use ROBOCOPY , try this:
EDIT: Changed the /S parameter to /E to include empty folders.
An alternate solution that copies one file at a time and does not require ROBOCOPY:
The outer for statement generates any possible path combination of subdirectory in SOURCE_DIR and name in FILENAMES_TO_COPY . For each existing file xcopy is invoked. FILE_INTERMEDIATE_DIR holds the file’s subdirectory path within SOURCE_DIR which needs to be created in DEST_DIR .
try piping output of find (ie. the file path) into cpio
cpio checks timestamp on target files — so its safe and fast.
remove -v for faster op, once you get used to it.
If Powershell is an option, you can do this:
The main disadvantage is it copies all folders, even if they will end up being empty because no files match the filter you specify. So you could end up with a tree full of empty folders, in addition to the few folders that have the files you want.
Thanks To Previous Answers. 🙂
This script named «r4k4copy.cmd»:
It accepts variable of «Source», «Destination», and «FileName». It also can only copying specified type of files or selective filenames.
Any improvement are welcome. 🙂
To copy all text files to G: and preserve directory structure:
With find and cp only:
Similar to Paulius’ solution, but the files you don’t care about are not copied then deleted:
That’s only two simple commands, but I wouldn’t recommend this, unless the files that you DON’T need to copy are small. That’s because this will copy ALL files and then remove the files that are not needed in the copy.
Sure, the second command is kind of long, but it works!
Also, this approach doesn’t require you to download and install any third party tools (Windows 2000+ BATCH has enough commands for this).
Under Linux and other UNIX systems, using the tar command would do this easily.
Then you’d cwd to the target and:
Of course you could pipe the output from the first tar into the 2nd, but seeing it work in steps is easier to understand and explain. I’m missing the necessary cd /to/new/path/ in the following command — I just don’t recall how to do it now. Someone else can add it, hopefully.
Tar (gnutar) is available on Windows too, but I’d probably use the xcopy method myself on that platform.
EDIT: If you want to preserve the empty folders (which, on rereading your post, you seem to) use /E instead of /S.
Using WinRAR command line interface, you can copy the file names and/or file types to an archive. Then you can extract that archive to whatever location you like. This preserves the original file structure.
I needed to add missing album picture files to my mobile phone without having to recopy the music itself. Fortunately the directory structure was the same on my computer and mobile!
- C:\Downloads\music.rar = Archive to be created
- X:\music\ = Folder containing music files
- Folder.jpg = Filename I wanted to copy
This created an archive with all the Folder.jpg files in the proper subdirectories.
This technique can be used to copy file types as well. If the files all had different names, you could choose to extract all files to a single directory. Additional command line parameters can archive multiple file types.
For those using Altap Salamander (2 panels file manager) : in the Options of the Copy popup, just specify the file names or masks. Easy.
I am fine with regular expressions, lazy and averse to installs, so I created a batch file that creates the directory and copies with vanilla DOS commands. Seems laborious but quicker for me than working out robocopy.
- Create your list of source files with complete paths, including drive letter if nec, in a text file.
- Switch on regular expressions in your text editor.
- Add double quotes round each line in case of spaces — search string (.*) replace string «\1» , and click replace all
- Create two lines per file — one to create the directory, one to copy the file (qqq will be replaced with destination path) — search string (.*) replace string md qqq\1\nxcopy \1 qqq\1\n and click replace all
- Remove the filename from the destination paths – search \\([^\\^»]+)»\n replace \\»\n
- Replace in the destination path (in this example A:\src and B:\dest ). Turn OFF regular expressions, search qqq»A:\src\ replace B:\dest\ and click replace all.
md will create nested directories. copy would probably behave identically to xcopy in this example. You might want to add /Y to xcopy to suppress overwrite confirms. You end up with a batch file like so:
repeated for every file in your original list. Tested on Win7.
Batch file to copy files from one folder to another folder
I have a storage folder on a network in which all users will store their active data on a server. Now that server is going to be replaced by a new one due to place problem so I need to copy sub folders files from the old server storage folder to new server storage folder. I have below ex:
from \Oldeserver\storage\data & files to \New server\storage\data & files.
9 Answers 9
xcopy.exe is definitely your friend here. It’s built into Windows, so its cost is nothing.
Just xcopy /s c:\source d:\target
You’d probably want to tweak a few things; some of the options we also add include these:
- /s/e — recursive copy, including copying empty directories.
- /v — add this to verify the copy against the original. slower, but for the paranoid.
- /h — copy system and hidden files.
- /k — copy read-only attributes along with files. otherwise, all files become read-write.
- /x — if you care about permissions, you might want /o or /x .
- /y — don’t prompt before overwriting existing files.
- /z — if you think the copy might fail and you want to restart it, use this. It places a marker on each file as it copies, so you can rerun the xcopy command to pick up from where it left off.
If you think the xcopy might fail partway through (like when you are copying over a flaky network connection), or that you have to stop it and want to continue it later, you can use xcopy /s/z c:\source d:\target .
copying all contents of folder to another folder using batch file?
I have a folder in C:\Folder1
I want to copy all the contents of Folder1 to another location, D:\Folder2
How do I do this using a batch file?
12 Answers 12
xcopy.exe is the solution here. It’s built into Windows.
If you have robocopy,
if you want remove the message that tells if the destination is a file or folder you just add a slash:
xcopy /s c:\Folder1 d:\Folder2\
I see a lot of answers suggesting the use of xcopy. But this is unnecessary. As the question clearly mentions that the author wants THE CONTENT IN THE FOLDER not the folder itself to be copied in this case we can -:
Thats all xcopy can be used for if any subdirectory exists in C:\Folder1
RoboCopy did not work for me, and there are some good solutions here, but none explained the XCopy switches and what they do. Also you need quotes in case your path has spaces in it.
xcopy /i /e «C:\temp\folder 1» «C:\temp\folder 2»
Here is the documentation from Microsoft:
On my PC, xcopy and robocopy need also the path to them, i.e. C:\Windows\System32\xcopy.exe
That’s why I use simply «copy»: copy /y . \Folder1\File.txt . \Folder2\
This is how it is done! Simple, right?
Here’s a solution with robocopy which copies the content of Folder1 into Folder2 going trough all subdirectories and automatically overwriting the files with the same name:
/COPYALL copies all file information
/E copies subdirectories including empty directories
/IS includes the same files
/IT includes modified files with the same name
Note: it can be necessary to run the command as administrator, because of the argument /COPYALL . If you can’t: just get rid of it.
FYI. if you use TortoiseSVN and you want to create a simple batch file to xcopy (or directory mirror) entire repositories into a «safe» location on a periodic basis, then this is the specific code that you might want to use. It copies over the hidden directories/files, maintains read-only attributes, and all subdirectories and best of all, doesn’t prompt for input. Just make sure that you assign folder1 (safe repo) and folder2 (usable repo) correctly.
And, that’s it folks!
Add to your scheduled tasks and never look back.
I have written a .bat file to copy and paste file to a temporary folder and make it zip and transfer into a smb mount point, Hope this would help,
batch/bat to copy folder and content at once
I’m writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this
If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.
5 Answers 5
if you have xcopy , you can use the /E param, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories
xcopy is deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.
robocopy has several advantages:
- copy paths exceeding 259 characters
- multithreaded copying
I suspect that the xcopy command is the magic bullet you’re looking for.
It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copy command.
If your batch file only needs to run on Windows Vista or later, you can use robocopy instead, which is an even more powerful tool than xcopy , and is now built into the operating system. It’s documentation is available here.
I’ve been interested in the original question here and related ones.
For an answer, this week I did some experiments with XCOPY.
To help answer the original question, here I post the results of my experiments.
I did the experiments on Windows 7 64 bit Professional SP1 with the copy of XCOPY that came with the operating system.
For the experiments, I wrote some code in the scripting language Open Object Rexx and the editor macro language Kexx with the text editor KEdit.
XCOPY was called from the Rexx code. The Kexx code edited the screen output of XCOPY to focus on the crucial results.
The experiments all had to do with using XCOPY to copy one directory with several files and subdirectories.
The experiments consisted of 10 cases. Each case adjusted the arguments to XCOPY and called XCOPY once. All 10 cases were attempting to do the same copying operation.
Here are the main results:
(1) Of the 10 cases, only three did copying. The other 7 cases right away, just from processing the arguments to XCOPY, gave error messages, e.g.,
with no files copied.
Of the three cases that did copying, they all did the same copying, that is, gave the same results.
(2) If want to copy a directory X and all the files and directories in directory X, in the hierarchical file system tree rooted at directory X, then apparently XCOPY — and this appears to be much of the original question — just will NOT do that.
One consequence is that if using XCOPY to copy directory X and its contents, then CAN copy the contents but CANNOT copy the directory X itself; thus, lose the time-date stamp on directory X, its archive bit, data on ownership, attributes, etc.
Of course if directory X is a subdirectory of directory Y, an XCOPY of Y will copy all of the contents of directory Y WITH directory X. So in this way can get a copy of directory X. However, the copy of directory X will have its time-date stamp of the time of the run of XCOPY and NOT the time-date stamp of the original directory X.
This change in time-date stamps can be awkward for a copy of a directory with a lot of downloaded Web pages: The HTML file of the Web page will have its original time-date stamp, but the corresponding subdirectory for files used by the HTML file will have the time-date stamp of the run of XCOPY. So, when sorting the copy on time date stamps, all the subdirectories, the HTML files and the corresponding subdirectories, e.g.,
can appear far apart in the sort on time-date.
Hierarchical file systems go way back, IIRC to Multics at MIT in 1969, and since then lots of people have recognized the two cases, given a directory X, (i) copy directory X and all its contents and (ii) copy all the contents of X but not directory X itself. Well, if only from the experiments, XCOPY does only (ii).
So, the results of the 10 cases are below. For each case, in the results the first three lines have the first three arguments to XCOPY. So, the first line has the tree name of the directory to be copied, the ‘source’; the second line has the tree name of the directory to get the copies, the ‘destination’, and the third line has the options for XCOPY. The remaining 1-2 lines have the results of the run of XCOPY.
One big point about the options is that options /X and /O result in result
To see this, compare case 8 with the other cases that were the same, did not have /X and /O, but did copy.
These experiments have me better understand XCOPY and contribute an answer to the original question.