Copy all files from one directory to another windows

Windows copy command syntax and examples

Using copy command, we can copy files from one directory to another directory. This command is similar to the Linux cp command, but it does not match with the full functionality of cp. Windows copy command can be used to copy files only, we can’t copy directories.

The syntax and usecases of copy command are explained below with examples.

Copy the contents of a file to another file

Example: To copy a file from c:\data\file1.doc to D:\backup\file2.doc

If the destination file already exists you will be prompted for confirmation. To suppress this confirmation you can use /Y switch with copy command. This would be useful if you are executing copy command from a batch file.

If the destination file exists, the above command will overwrite the same without asking the user for confirmation.

Copy file to another directory

When we specify a directory path as the destination, the files will be copied with the same name. We can assign a different name by specifying the new name in the destination path. Example is shown below.

To copy the file 1.doc loated at c:\data\documents to the directory c:\data\newdocs

Copy files with white space in name

If the file name has white space within it, we can wrap up the name in double quotes.
Example: To copy file, my resume.doc to another folder

Copy multiple files

We can’t specify multiple file names in copy command. However, we can use wildcards to identify a group of files and then copy all of them in a single command.
For example, to copy all excel files from current folder to another folder F:\backup

To copy all files in current folder to another folder

Use of environment variables

We can use environment variables in the copy command to specify the path of the folders. Like USERPROFILE, SystemRoot, ProgramFiles, TEMP, WINDIR, APPDATA, HOMEPATH.

For example, to copy a file to a user’s documents folder

The above command copies the file to the My Documents folder of the current logged in user.

You may also want to read

Windows «copy» is funny. Type «copy 1 2» and the file «1» will be copied into a new file «2». Now separate them by a plus sign instead of a space (copy 1+2) and you’ll concatenate 1 and 2 and replace the old file «1» with the result of the concatenation!

Yes, we can concatenate two or more files using copy command. You need to separate the list of files using +. You can redirect the resultant data to a new file also.

Читайте также:  Как удалить все windows contacts

The above command will not alter the file 1. It creates a new file 3 with the concatenated data of 1 & 2. If no file name is provided it stores the result in the first file.

My Win7 cannot find a copy command, and when i run xcopy, a window flashes and exits.

I have the same problem. If you solved it, could you please explain how?

If you can not find your copy.exe file, you can download it to your windows directory or C:\ Directory depending the setting on your OS you should also be able to copy and run it from system32 or system folder.

how can i combine 2 .exe files and be able to use both after concatenation

I want to copy 2 different files(.exe,.config) from source to destination server of windows.
can you please help me on this command.

Hello i have a problem with my cmd windows 7.when i try to copy a command. Like help > file.pdf. i mean in extension pdf because i have this problem only with .pdf extension but not with .txt.So whe i execute the command. No problem. Then when i go to open the file.pdf ftom user destination the file.pdf doesn’t open say that is corrupted.please do help me .thanks

i have a file contain many lines as sources and another file has the same numbre of lines as destinations. i want to copy first line as source( c:/test/*.txt) to first line in destination ( d:/test2/), secend line ( c:/test/*.pdf) to second line in destination ( E:/test3/)……

Can I use the DOS/Windows “COPY” command in a BAT file to copy a file or a short string of text to computer memory and then paste (Ctrl +V) that string or file into a document?

Copy all files from one directory to another

I want to copy all file from folder1 to folder2 if folder 2 is empty.

For example: file1.txt , file2.pdf etc are present in folder1 , and there are two folders: folder1 and folder2 , and folder1 should send all files to folder2 if its empty.

1 Answer 1

When in doubt, read the documentation:

object.CopyFile ( source, destination[, overwrite] )

Arguments

source
Required. Character string file specification, which can include wildcard characters, for one or more files to be copied.

destination
Required. Character string destination where the file or files from source are to be copied. Wildcard characters are not allowed.

Remarks

Wildcard characters can only be used in the last path component of the source argument. For example, you can use:
[. ]
If source contains wildcard characters or destination ends with a path separator (\), it is assumed that destination is an existing folder in which to copy matching files. Otherwise, destination is assumed to be the name of a file to create.

and the code will copy everything from the source folder to the destination folder.

However, since you only want to copy when the destination folder is empty, you’ll want to check that first:

Читайте также:  Как установить принтер через терминал linux

If the destination must also not contain any folders you’ll need to check for the existence of subfolders as well.

Fastest way to copy files from one directory to another

I need to copy files from one directory to another, depending on the existence of the file name in a table of SQL database.

For this I use the following code:

Is there any way that this can be done in less time? It takes 1 hour for do that, for 20.876 records.

4 Answers 4

Since your i/o subsystem is almost certainly the botteneck here, using the parallel task library is probably about as good as it gets:

File.Copy is as fast as it gets. You must keep in mind that you depend on the file transfer speed dictated by your hardware and at 20000 files, the latency for data access also comes into play. If you are doing this on a HDD, you could see a big improvement after switching to SSD or some other fast medium.

For this case alone, most likely the hardware is your bottleneck.

EDIT: I consider keeping the connection to the database open for such a long time as a bad practice. I suggest you fetch all the needed data in some in-memory cache (array, list, whatever) and then iterate through that as you copy the files. A db connection is a precious resource and on applications that must handle high concurrency (but not only), releasing the connection fast is a must.

Allow me to make a guess — Mmmmm. No. There is no way to do it faster.

How come I am so confident? Because file copying requires talking to disk and that is a horribly slow operation. Even further, if you try to go for multi-threading, the results will go slower instead of faster because the ‘mechanical’ operation of moving the head over the disk isn’t sequential anymore, which may have been earlier by chance.

So yeah, try going to SSDs if you aren’t yet using them, otherwise you are getting the best already.

Below here is something for us to put into perspective what does slow mean in disk writing when compared to caches. If cache access is taking 10 min., it implies that it takes 2 years to read from disk. All the accesses are shown in the image below. Clearly when your code will execute, the bottleneck will be disk writes. The best you can do it to let the disk writes stay sequential.

How to copy all files from one directory to another only if file doesn’t exist in destination subfolders?

I have a directory with files directly in it C:\test1 .
I want to copy all these files to C:\test2 only if the file does not exist in any subdirectory in C:\test2 .

Need to write a Windows batch file which can do this. Any help is appreciated.

I have tried using the following, but it does not searches recursively in destination directory:

Читайте также:  Windows проверить запущен ли процесс

nxF» copy . , try > nul dir /S /B /A:-D «C:\test2\%%

nxF» || copy . . – aschipfl Oct 11 ’18 at 16:47

2 Answers 2

If you wanted to push ahead into PowerShell, this code might help. When you are satisfied the the files will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.

Ok, I admit that I do not have a test1 and test2 directory for testing, but this will probably get you started.

If you save the script above as Copy-IfNone.ps1 , then you can run it from a cmd shell using:

Examples make it easier for everyone to understand the requirements for a task.
So let me start with an example.

Source directory C:\test1 contains following files:

Destination directory tree C:\test2 contains following directories and files:

  • Subfolder1
    • Test2.txt
  • Subfolder2
    • Subfolder3
      • Test3.txt

For this example just file Test1.txt should be copied to directory C:\test2 because Test2.txt and Test3.txt exist already in subfolders of C:\test2 .

So the directory tree C:\test2 should look as follows after batch file execution:

  • Subfolder1
    • Test2.txt
  • Subfolder2
    • Subfolder3
      • Test3.txt
  • Test1.txt

This can be achieved with:

It is also possible to do that with a single command line:

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

copy all files and folders from one drive to another drive using DOS (command prompt)

i want to copy all files and folders from one drive to another drive using MS-DOS. How to do it?

I am trying xcopy I:\*.* N:\ But it copies only files, not folders. So how to copy all files and folders both?

5 Answers 5

Use xcopy /s I:\*.* N:\

This is should do.

Use the above command. It will definitely work.

In this command data will be copied from c:\ to D:\, even folders and system files as well. Here’s what the flags do:

  • /h copies hidden and system files also
  • /i if destination does not exist and copying more than one file, assume that destination must be a directory
  • /c continue copying even if error occurs
  • /k copies attributes
  • /e copies directories and subdirectories, including empty ones
  • /r overwrites read-only files
  • /y suppress prompting to confirm whether you want to overwrite a file

Use robocopy . Robocopy is shipped by default on Windows Vista and newer, and is considered the replacement for xcopy . ( xcopy has some significant limitations, including the fact that it can’t handle paths longer than 256 characters, even if the filesystem can).

Note that using /purge on the root directory of the volume will cause Robocopy to apply the requested operation on files inside the System Volume Information directory. Run robocopy /? for help. Also note that you probably want to open the command prompt as an administrator to be able to copy system files. To speed things up, use /b instead of /zb .

Оцените статью