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
copy %~dp0file.txt file.txt
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.
mainfolder/ file1.txt file2.txt insidefolder/ file3.txt file4.txt file5.txt
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 [source] [destination] /E
copying all contents of folder to another folder using batch file , xcopy.exe is the solution here. It's built into Windows. xcopy /s c:\Folder1 d:\Folder2. You can find more options at A batch file is a text file that contains a sequence of commands for a computer operating system. The batch script file has a bundle or package of a set of commands that is the reason it is called the batch file. The extension of the bat script file is .bat or .cmd.
How to Copy a Folder with Batch File: 5 Steps (with Pictures), Have you ever tried to copy an entire folder with a Windows batch file? method will show you how to copy a folder from one location to another with a batch file. BAT extension. Delete Files That Cannot Be Deleted. This batch command is used for copying files from one location to the other. Syntax Copy [source] [destination] The files will be copied from source to destination location. Example. The following example shows the different variants of the copy command. @echo off cd Rem Copies lists.txt to the present working directory.
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
Check out the documentation here.
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.
Xcopy - Copy files and folders - Windows CMD, Copy files and/or directory trees to another folder. XCOPY is similar to the COPY command except that it has additional switches to specify both the source and The batch script, also known as batch file, actually refers to a list of several commands; whenever you double-click the file, the commands will be executed. If you want to copy files or folders from one folder to another automatically, creating a batch script is a nice choice.
For Folder Copy You can Use
robocopy C:\Source D:\Destination /E
For File Copy
copy D:\Sourcefile.txt D:\backup\Destinationfile.txt /Y
Delete file in some folder last modify date more than some day
forfiles -p "D:\FolderPath" -s -m *.[Filetype eg-->.txt] -d -[Numberof dates] -c "cmd /c del @PATH"
And you can Shedule task in windows perform this task automatically in specific time.
Batch file to copy folder structure, This batch file would copy all folders in the source directory to the destination directory - the files themselves would not be copied. For example, say there is a Suppose it normally takes 80 seconds to copy this file; this might well be the case on a 100 Mbit connection. Then the total time becomes 80 + 800 = 880 seconds (almost 15 minutes). The bandwidth used is 8000 Mbit / 880 sec = 9.1 Mbit/s. This leaves more than 90 Mbit/s of bandwidth for other processes to use.
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.,
Invalid path Access denied
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.
======= case 1 ================== "k:\software\dir_time-date\" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_1\" options = /E /F /G /H /K /O /R /V /X /Y Result: Invalid path Result: 0 File(s) copied ======= case 2 ================== "k:\software\dir_time-date\*" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_2\" options = /E /F /G /H /K /O /R /V /X /Y Result: Access denied Result: 0 File(s) copied ======= case 3 ================== "k:\software\dir_time-date" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_3\" options = /E /F /G /H /K /O /R /V /X /Y Result: Access denied Result: 0 File(s) copied ======= case 4 ================== "k:\software\dir_time-date\" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_4\" options = /E /F /G /H /K /R /V /Y Result: Invalid path Result: 0 File(s) copied ======= case 5 ================== "k:\software\dir_time-date\" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_5\" options = /E /F /G /H /K /O /R /S /X /Y Result: Invalid path Result: 0 File(s) copied ======= case 6 ================== "k:\software\dir_time-date" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_6\" options = /E /F /G /H /I /K /O /R /S /X /Y Result: Access denied Result: 0 File(s) copied ======= case 7 ================== "k:\software\dir_time-date" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_7" options = /E /F /G /H /I /K /R /S /Y Result: 20 File(s) copied ======= case 8 ================== "k:\software\dir_time-date" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_8" options = /E /F /G /H /I /K /O /R /S /X /Y Result: Access denied Result: 0 File(s) copied ======= case 9 ================== "k:\software\dir_time-date" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_9" options = /I /S Result: 20 File(s) copied ======= case 10 ================== "k:\software\dir_time-date" "k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_10" options = /E /I /S Result: 20 File(s) copied
Commmand line command to copy entire directory (including , For example, to copy all of the contents of the C:\tools directory to the new I ran into one when I had to copy folders from C:\Windows\Assembly\GAC_MSIL. result.log will be created automatically in the folder with batch.bat – spirit Jul 3 '16 at 8:34
Copy directory from command line, Windows has two command line utilities to copy files/directories from command line. Copy command can be used to copy files from one folder to another folder. Select the multiple folder(s) that you want to copy files to. Optionally, you can simply copy a set of files from different location to one folder location, great for testing applications. Ability
COPY command - Copy files or directories, @file, A text file containing the names of the source files, one per line (see @file The following command copies 3 specific files from the current directory to the \MYDIR BAT files from the E:\MYDIR directory to the root directory of drive A:. Save the Notepad file with the BAT file extension, and run it. The file will be copied to all sub-folders. Example. This is what the code looks like after I edited it to copy a file named MyFile.txt located at C:\Users\fatiw\Desktop\Test to all sub-folders under C:\Users\fatiw\Desktop\Newfolder.
How to copy a directory or folder, Once in the directory, use the xcopy command to copy another directory's subdirectories and contents Hi, I have a file with no extension called simply, D:\file I want to copy this to multiple locations, eg e:\file , f:\file sometimes there will already be an older version, so I would like it to overwrite the file. Is this possible with a bat file? I tried a simple copy command in a bat file, but it wouldn't copy at all. thanks.