Windows copy command copies only 1 file - windows

In a folder, I have 4 files, for example:
install.bat
program.exe
service.exe
uninstall.bat
I am interested to know why the following command copies all bat files to C:\Temp
copy *.bat C:\Temp
But this command only copies the first file, ignoring the rest?
copy *.bat+*.exe C:\Temp
It sees all the files because the output lists them, but only one is copied.
install.bat
program.exe
service.exe
uninstall.bat
1 file(s) copied.
Of course I can use two commands, one for each extensions, but why does it copy only one file, and how to specify multiple sources at once?
EDIT
The documentation of the command (i.e. copy /?) is the following:
Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.
C:\Users\yanickrochon>copy /?
Copies one or more files to another location.
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]
source Specifies the file or files to be copied.
/A Indicates an ASCII text file.
/B Indicates a binary file.
/D Allow the destination file to be created decrypted
destination Specifies the directory and/or filename for the new file(s).
/V Verifies that new files are written correctly.
/N Uses short filename, if available, when copying a file with a
non-8dot3 name.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/L If the source is a symbolic link, copy the link to the target
instead of the actual file the source link points to.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.
To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).
This question is about the follow part:
COPY source + source destination
If the command accepts multiple sources, how does it work?

#for %e in (bat exe) do (#XCOPY "C:\source\*.%e" C:\tmp /C /S /I /F /H)
Just put this to cmd and you should be fine.

Related

Batch Script to copy files from a Text file

I need help with below code as it is executing in root folder only whereas I want the code to look for files within sub folders as well.
for /F "tokens=*" %%A in (documents.txt) do (
copy %%A E:\Destination\
)
I suggest to use this command line in the batch file to copy all files with duplicating directory structure from source to destination directory.
for /F "eol=| delims=" %%I in (documentation.txt) do %SystemRoot%\System32\robocopy.exe "%~dp0." "E:\Destination" "%%~I" /S /NDL /NFL /NJH /NJS
It is assumed that the file documentation.txt contains a list of file names without path.
The command FOR reads one line after the other from file documentation.txt with skipping empty lines. The end of line character is modified from default ; to | using option eol=| to be able copying also files of which name starts unusually with a semicolon. No file name can contain a vertical bar anywhere. The line splitting behavior on spaces/tabs is disabled by using option delims= which defines in this case an empty list of string delimiters. Therefore file names with one or more space even at beginning of file name read from file are assigned unmodified to loop variable I. The option tokens=* removes leading spaces/tabs from the lines read from text file. A file name can begin with one or more spaces although such file names are unusual.
FOR runs for each file name the executable ROBOCOPY with directory of the batch file as source folder path and E:\Destination as destination folder path. ROBOCOPY interprets a \ left of one more \ or " as escape character. For that reason the source and destination folder paths should never end with a backslash as this would result in " being interpreted not as end of folder path, but everything up to next " in command line. For that reason . is appended to %~dp0 because of %~dp0 always expands to batch file folder path ending with a backslash. The dot at end of batch file folder path references the current folder of batch file folder. In other words with batch file stored in C:\Temp the batch file folder can be referenced with C:\Temp\ as done with %~dp0 but not possible with ROBOCOPY or with C:\Temp\. as done with %~dp0. or with just C:\Temp or with C:\Temp\\ as done with %~dp0\ which would also work with ROBOCOPY. See the Microsoft documentation about Naming Files, Paths, and Namespaces for details.
Remove %~dp0 to use current folder as source folder instead of batch file folder.
The ROBOCOPY option /S results in searching in source folder and all its subfolders for the file and copy each found file to destination folder with duplicating the source folder structure in destination folder.
The other ROBOCOPY options are just for not printing list of created directories, list of copied files, header and summary.
Here is an alternative command line for this task copying all files from source directory tree into destination directory without creating subdirectories. So all copied files are finally in specified destination directory.
for /F "eol=| delims=" %%I in (documentation.txt) do for /F "delims=" %%J in ('dir "%~dp0%%~I" /A-D-H /B /S 2^>nul') do copy /B /Y "%%J" "E:\Destination\" >nul
The inner FOR starts for each file name assigned to loop variable I of outer FOR one more command process in background with %ComSpec% /c and the DIR command line appended as additional arguments. So executed is for each file name in documentation.txt with Windows installed into C:\Windows for example:
C:\Windows\System32\cmd.exe /c dir "C:\Batch File Path\Current File Name.ext" /A-D-H /B /S 2>nul
The command DIR executed by second cmd.exe in background searches
in directory of the batch file
and all its subdirectories because of option /S
only for non-hidden files because of option /A-D-H (attribute not directory and not hidden)
with the specified file name
and outputs in bare format because of option /B
just the names of the found files with full path because of option /S.
It is possible that DIR cannot find a file matching these criteria at all in which case it would output an error message to handle STDERR of the background command process. This error message is suppressed by redirecting it with 2>nul to device NUL.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The inner FOR captures everything written to handle STDOUT of started background command process and processes this output line by line after started cmd.exe terminated itself after finishing execution of DIR.
The inner FOR assigns each full qualified file name to specified loop variable J without any modification because of option delims= and runs next the command COPY to copy that file as binary file to destination directory with automatically overwriting an existing file in destination directory with same file name. The success message output by COPY to handle STDOUT is redirected with >nul to device NUL to suppress it. An error message would be output by COPY. An error occurs if destination directory does not exist, or the destination directory is write-protected, or an existing file with same name is write-protected due to a read-only attribute or file permissions, or source file is opened by an application with sharing read access denied, or an existing destination file is opened by an application with sharing write access denied.
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.
call /? ... for an explanation of %~dp0 referencing drive and path of argument 0 which is the full qualified path of the batch file currently processed by cmd.exe.
copy /?
dir /?
for /?
robocopy /?

I want to copy a directory excluding one file

I have used the xcopy command with /EXCLUDE switch but for that i have to make another file that contains the list of files to be excluded.
Below is the xcopy command i am using...
xcopy /EXCLUDE:C:\AA\excludedfiles.txt C:\AA d:\Models\Broker\NB\MOTNB0056
/S /E
where excludedfiles.txt contains the name of file that i want to exclude.
C:\AA is source and d:\Models\Broker\NB\MOTNB0056 is destination.
However i don't want to make extra file(excludedfiles.txt) for it. Suggest a command that exclude a file by giving just its path.
XCOPY is deprecated. It has been replaced by ROBOCOPY.
Open a command prompt window an run robocopy /? for help on command ROBOCOPY. In comparison to help of XCOPY output on running xcopy /? it has the option /XF to exclude one or more files specified after this switch and it is even possible to use wildcards.
So the command you might use is:
%SystemRoot%\System32\robocopy.exe C:\AA D:\Models\Broker\NB\MOTNB0056 /E /XF C:\AA\FileToExclude.ext
Some additional notes:
/S means copying with subdirectories, but without empty directories.
/E means copying with subdirectories with including empty directories.
It does not make sense to specify both on the command line.
It is advisable to specify target directory with backslash at end. This makes it clear for ROBOCOPY as well as for XCOPY that the target string specifies a directory and not a file. This is important in case of just a single file is copied as you can read in answer on batch file asks for file or folder. Both commands create the directory tree to target directory if this is necessary on having target directory specified with \ at end.
See Microsoft's documentation on Windows Commands and SS64.com - A-Z index of the Windows CMD command line on searching for a command for a specific file operation from command line or batch file.

Batch script to read and extract details from a text file

I need to copy a few files and folders to their respective destinations using a Windows batch script.
All the files and folders I am supposed to copy, are kept within a folder, SOURCE.
Example:
folder: C:\X\Y\Z\SOURCE\A
file : C:\X\Y\Z\SOURCE\A.txt
file : C:\X\Y\Z\SOURCE\B.txt
folder: C:\X\Y\Z\SOURCE\ZZZ
The destination paths of all the above are provided as text file contents, destination.txt.
Content of destination.txt:
C:\FinalDestination\D\A\...
C:\FinalDestination\N\A.txt
C:\FinalDestination\C\B.txt
C:\FinalDestination\U\ZZZ\...
Where three dots at the end signifies a directory, otherwise it's a file.
What I need to do in the above scenario is:
Copy folder A from SOURCE to C:\FinalDestination\D\
Copy file A.txt from SOURCE to C:\FinalDestination\N\
Copy file B.txt from SOURCE to C:\FinalDestination\C\
Copy folder ZZZ from SOURCE to C:\FinalDestination\U\
I don't know how to do it as I am pretty new to Windows command line.
I know XCopy is a command which can work for me, xcopy source destination, but I don't know how to extract the source and destination details.
Using an unchanged destination.txt and your supplied data, the following may help:
#Echo Off
Set "sD=C:\X\Y\Z\SOURCE"
Set "sF=destination.txt"
For /F "UseBackQ Delims=" %%A In ("%sF%"
) Do For %%B In ("%%~fA.") Do echo=XCopy/IY "%sD%\%%~nxB" "%%~dpA." 2>Nul
Pause
You need only modify the content of the variables at lines 3 and 4
Note:
I have currently made it so that nothing is copied, just the commands output to your screen. If you are happy with the output remove echo= from line 7 and delete the content of line, 8

copy entire folder on postbuild to a specific folder

Lets say ,
I have xyz.dll in the debug folder, along with resource files folders , de, da ,etc.
F be the project name .
Folder Structure as mentioned below
d:A\B\C\D\E\F\bin\debug
I have to copy the dll and resource file folders from debug folder to C1 folder .
Folder structure as shown below
d:A\B\C1.
available macros are
TargetDir : d:A\B\C\D\E\F\bin\debug
ProjectDir: d:A\B\C\D\E\F\
I tried this but its not working
xcopy $(TargetDir ) $(ProjectDir)........\c1 /R /Y
This copies only the dll not the resources folder .
Any suggestions on how I should do this ?
Use the /S switch to xcopy to include subdirectories recursively
/? is your friend..
C:\>xcopy /?
Copies files and directory trees.
XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
[/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B] [/J]
[/EXCLUDE:file1[+file2][+file3]...]
source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set,
doesn't change the attribute.
/M Copies only files with the archive attribute set,
turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
/P Prompts you before creating each destination file.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/V Verifies the size of each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/G Allows the copying of encrypted files to destination that does
not support encryption.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attributes.
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/B Copies the Symbolic Link itself versus the target of the link.
/J Copies using unbuffered I/O. Recommended for very large files.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
finally I found a way ,
xcopy $(TargetDir).$(ProjectDir)..\..\..\C1 /R /Y /E .
There is an * operators surrounding the dot(.) after the $(TargetDir) ,As its not visible when i post it , I'm mentioning it here .

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

Resources