- batch/bat to copy folder and content at once
- 5 Answers 5
- Batch script to copy files listed in text file from a folder and its subfolders to a new location
- 1 Answer 1
- How to copy a directory structure but only include certain files (using windows batch files)
- 15 Answers 15
- Copy a .bat off a USB and copy it to the Startup folder script — WinXP
- 3 Answers 3
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.
Batch script to copy files listed in text file from a folder and its subfolders to a new location
I would like a batch file to copy all of the files listed in a text file from one destination to another. The source destination may have multiple subfolders and I would like the batch to search each subfolder for the file name.
I do not want copy the folders themselves, only the files.
I have the following code but it isn’t recognizing the (%file_list%)
I also want the file to write a text file called ‘notcopied.txt’ so I can see if any required files were not in the source folder. I want that file written to the destination folder.
source folder contains
notcopied.txt will then show
Thanks for any insight.
1 Answer 1
First, you need to enable extended command line
so you can use the extended FOR loop. Second, you need to enable delayed expansion of environment variables
so you can check if searched file was found later.
Here is the result:
ENABLEDELAYEDEXPANSION is needed here for variable %found% , without it %found% will always be false . Furthermore, to expand environment at runtime you must use !found! syntax.
You can call it like
and the results will be writen to file result.log
UPDATE: here is a version of bacth without variable found and with suggestions from @aschipfl:
This version can handle files with a space symbol in the filename.
But both of the solutions have same restriction: sourcefolder can’t contain any spaces in the name. For some reason command WHERE /R «%src%» «%%f» doesn’t working, while WHERE /R %src% «%%f» working just as expected.
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.
Copy a .bat off a USB and copy it to the Startup folder script — WinXP
I’m a Mac/iPhone dev so I don’t know very much about Windows scripting. The point is I have to install a startup app on many computers, so I’d like to have a USB stick with two .bat files:
- would be the actual «app»
- would be the script that would copy the 1st.bat off my USB to the Windows startup folder.
How can I do that?
the name of my usb is «USB» and the name of my startup app is «startup.bat». How I already said, I’m extremely lame in Windows programing, and I need it acutely 😉
3 Answers 3
Try the following script. This will cause the application to run whenever the current user logs in. Without administrative privilages, you won’t be able to do it for all users in one go.
Each line does the following:
- Turn off command echoing, making the script look cleaner to the end user.
- Set the current directory to wherever this script is located.
- Set the Startup folder’s path as expected in Windows Vista or later.
- If this folder exists, jump to the copying stage.
- Set the Startup folder’s path as expected in Windows 2000 or later.
- If this folder exists, jump to the copying stage.
- Report that the Startup folder can’t be found.
- Exit the batch script.
- A label that can be jumped to.
- Copy «MyApp» from the current folder (USB) to the Startup folder.
0dp is code for «this script’s drive and path». If you script is «E:\MyInstaller\Install.bat», then %
dp0 is «E:\MyInstaller». Dir /D is «change drive and path». – Hand-E-Food Oct 25 ’11 at 6:18
I don’t want to take credit for Hand-E-Food’s answer, but I figured out why his code didn’t work and I can’t reply to his answer, so here it is. Instead of using quotes around the %StartupFolder% variable in the Copy line, use them around the path for Set StartupFolder . Therefore, the code would be as follows.
The only reason I figured this out is because it wasn’t doing anything for me either, so I tried removing the quotes around %StartupFolder% and that resulted in an error message that it couldn’t find the folder, but at least I knew it was doing something at the end. Once I figured out that it was looking for the wrong folder because it thought the folder name stopped at the first space in its name, I simply added in the quotes and voila!