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,
@echo off
if not exist "C:\Temp Backup\" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP"
if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
xcopy /s/e/q "C:\Source" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\TELIUM"
"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_Log_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
NET USE \\IP\IPC$ /u:IP\username password
ROBOCOPY "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" "\\IP\Backup Folder" /z /MIR /unilog+:"C:\backup_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log"
NET USE \\172.20.10.103\IPC$ /D
RMDIR /S /Q "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
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,
@echo off
if not exist "C:\Temp Backup\" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP"
if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
xcopy /s/e/q "C:\Source" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\TELIUM"
"C:\Program Files (x86)\WinRAR\WinRAR.exe" a "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_Log_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
NET USE \\IP\IPC$ /u:IP\username password
ROBOCOPY "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" "\\IP\Backup Folder" /z /MIR /unilog+:"C:\backup_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log"
NET USE \\172.20.10.103\IPC$ /D
RMDIR /S /Q "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
I have two folders src
and dest
:
src
--new.txt
--style.css
dest
--text.txt
--install.bat
I would like to copy all files inside src into dest. I have tried copy src dest
but that destroys everything that is inside dest.
Is they a way to copy all files/folders inside src into dest ?
how the dest folder should be after the copy:
dest
--text.txt
--new.txt
--style.css
--install.bat
Kevin Panko
7,35622 gold badges44 silver badges53 bronze badges
asked Feb 6, 2014 at 16:59
xcopy /s src\*.* dest
doesn’t work for you?
If you specify only the folder, it replaces the entire folder for you. When you specify the items inside the folder (with *.*
), it copies the actual files instead of the folder.
Example below:
C:\Users\User>xcopy /s test\*.* test2
test\1.txt
test\2.txt
2 File(s) copied
C:\Users\User>dir test2
Volume in drive C has no label.
Volume Serial Number is 3018-ED8A
Directory of C:\Users\User\test2
06/02/2014 09:38 AM <DIR> .
06/02/2014 09:38 AM <DIR> ..
06/02/2014 09:37 AM 5 1.txt
06/02/2014 09:37 AM 5 2.txt
06/02/2014 09:37 AM 5 3.txt
answered Feb 6, 2014 at 17:06
Canadian LukeCanadian Luke
24.3k39 gold badges118 silver badges171 bronze badges
7
I finally used
robocopy src\ dest /E
which is able to copy even files with long paths.
answered Feb 6, 2014 at 19:53
edi9999edi9999
1712 silver badges13 bronze badges
I think You should use xcopy command. It copies all file structure within the directory:
xcopy src dest /s /e
This two parameters instruct it to copy all files directories within src including empty ones
answered Feb 6, 2014 at 17:10
4
copy scr dest
should be enough altough it will not copy subdirectories
if you have same file in the destination folder you should have a message like this one:
C:\>copy tmp tmp2
tmp\bifish.txt
tmp\mal.log
Overwrite tmp2\mal.log? (Yes/No/All): no
tmp\png_create_201401.sql
tmp\png_data_201401.sql
3 file(s) copied.
answered Feb 6, 2014 at 17:12
Fred CloseFred Close
5013 silver badges4 bronze badges
1
You must log in to answer this question.
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.
In this tutorial, we will learn how to copy files and folders in the Windows Command prompt.
We are going to look at two cmd commands: Copy and Xcopy.
Note that the copy
command has some limitations compared to the xcopy
. For example, to copy directories or hidden files, you have to use the xcopy
command.
Copy Command
On Windows, we can use the copy
command to copy one or more files from one location to another:
copy C:\data\sales.doc C:\backup
The preceding command will copy sales.doc
from C:\data\
to C:\backup
.
Use the /y
switch to overwrite duplicate files without confirmation:
copy /y C:\data\sales.doc C:\backup
We can also save a file to a different name. For example, the following command saves file1.tx
t as file2.txt
in the same directory:
copy file1.txt file2.txt
You can also use wildcards to copy multiple files:
copy /y C:\data\* C:\backup
copy /y C:\data\*.doc C:\backup
The first command copies all files in the C:\data\
directory to C:\backup
. The second command copies all files with a .doc
extension to the C:\backup
.
We can also combine several files into one:
copy file1+file2 file3
copy error* C:\backup\report.txt
In the first line, file1 and file2 are combined to make one file named file3. In the second line, all files whose names start with «error» are copied to the C:\backup
, as a single file called report.txt.
You can get a list of all available options with the copy /?
command.
Xcopy Command
The xcopy
command offers more features. For example, with xcopy
, we can copy directories and subdirectories, as well as hidden files.
Command Options
/Y | Prompt before overwriting an existing file. |
/H | Copy hidden files/system files. |
/S | Copy directories and subdirectories. Empty directories are not included by default (use /e for that). |
/E | Include empty directories. |
/I | Create the destination folder if it does not exist. Use this option when you want to copy the parent folder itself. |
/T | Copy directory tree without files. Empty directories are not included by default. Use /e option to include empty folders. |
/P | Prompt for confirmation before creating each file. |
/Q | Quiet mode. |
/exclude | Specify a text file that contains a list of files to exclude. See the examples. |
/Z | Resume mode. Use this option when copying files over a network. |
/D:m-d-y | Copies files changed on or after the specified date. |
Examples of Using the Xcopy Command
Copy sales.doc from the current directory to C:\backup
:
xcopy sales.doc C:\backup
Copy C:\data\accounts
(all files including subdirectories) to C:\backup
:
xcopy /s /e /h /i /y C:\data\accounts C:\backup\accounts
In the following example (without /I
switch), the contents of the folder are copied but not the folder itself:
xcopy /s /e /h /y C:\data\accounts C:\backup\
Copy the directory structure of C:\OneDrive
to the backup directory:
xcopy /s /e /t /y C:\OneDrive C:\backup\
You can use wildcard characters to match patterns. The following command copies all files with a .jpg
extension:
xcopy /s /h /y C:\data\*.jpg C:\backup
Using for
loop to copy multiple files:
for %i in (sales.doc, products.doc) do xcopy /y %i C:\backup
Excluding files with xcopy
With the /exclude
, we can provide a text file that contains items we want to exclude.
xcopy /s /e /h /y /exclude:C:\Users\user1\files-to-exclude.txt C:\data\ C:\backup\
The files-to-exclude.txt
may look like the following:
.doc
sales*
In this example, we exclude items with the .doc
extension and files whose name starts with sales.
You can get a list of all available options with the xcopy /?
command.
Xcopy command
Xcopy
is a built in command on Windows OS which has advanced features than the basic Copy command. The additional features Xcopy has are listed below.
- Xcopy can copy directories
- Xcopy can copy all files including subdirectories recursively and can replicate the source directory structure as is.
- Xcopy can exclude files based on file name or extension
- Xcopy can help identify updated files(based on archive attribute or based on a given cutoff date), so it’s useful for incremental backup needs.
Xcopy command to copy files.
Xcopy /I Source_Directory Destination_directory
This would copy only the immediate files in the source directory to the destination. It would not copy files from sub directories. Adding /I switch will avoid the question to the user ‘Does the destination directory specify a file name or directory name on the target‘.
Copy a directory including files and sub directories recursively.
Xcopy /S /I /E Source_Directory Destination_directory
For example to copy all the files from D:\data\documents to the folder E:\Newfolder\documents we need to run the below command.
Xcopy /S /I /E D:\data\documents E:\Newfolder\documents
This command creates the folder E:\Newfolder\documents if it already does not exist. It also creates the same directory/file structure in the destination folder. If the destination folder already exists you do not need to add /I switch. If the folder does not exist and you do not specify /I you will be prompted to confirm if the destination is a filer or folder. This would cause issues if you want to run unattended or automated copying of files.
Xcopy – Exclude files
If you want to copy a directory to another location but want to exclude some files then you can use /EXCLUDE switch with Xcopy command. You need to put the list of the files to be excluded in a file and then specify this file with the /EXCLUDE switch.
Xcopy /I Sourcedir Destdir /EXCLUDE:filename
Example:
Copy the directory D:\docs to E:\newdocs\ excluding all pdf and mp3 files:
C:\>type 1.txt .pdf .mp3 C:\>Xcopy D:\docs E:\newdocs /EXCLUDE:1.txt
You can also specify the full names of the files. Each file name/pattern should be in a separate line.
Copy files including hidden and system files.
The above command excludes hidden and system files from copying. If you want to these files also then you need to add /H option to your command. Then the command would be
Xcopy /S /I /E /H D:\data\documents E:\Newfolder\documents
/E option causes to copy empty sub directories to the destination folder.
If you add /H option the the command would also copy hidden and system files to the destination folder.
If you are trying to overwrite an existing file then add the option /Y so that you will not be prompted for confirmation for overwriting the file.
Copy files based on archive attribute
If you want to copy only the files that have archive attribute set, you can use /A and /M switches. Archive attribute indicates whether the file has been modified since the time the attribute was reset.
The command for this is:
Xcopy /A /I /S source_directory destination_directory.
The above command keeps the archive attribute set; It does not reset the attribute.
If you want to reset the archive attribute, you can use /M switch instead of /A. The command for this case would be:
Xcopy /M /I /S source_directory destination_directory
The commands shown above can be used from batch files also. This command is available in Windows 7, XP, Vista, Server 2003 and Server 2008 editions.
Copy files based on modified date
Xcopy /D:dd-mm-yy /I sourcedir destinationdir
Example: To copy all the files in the directory ‘E:data’ that are modified on or after 1 st February 2011 to the folder ‘E:\backup‘
Xcopy /D:01-02-11 /I E:\data E:\backup
Supported versions
Xcopy is available in all newer versions of Windows – Windows 10, 7 and 8. Particularly, I’ve verified it to be working on Windows 10.
Related Posts:
Backup files using xcopy command