Reputation:
I want to copy all the files of a folder into some other folders using batch script. Say, I have two folders named folder1 and folder2. these two folders are located in C:\Users\xyz . I want to copy the elements of another folder (say, folder3 which is located in C:\Users\abc\def) into these two folders. I have made the following script but nothing is copied. My sample batch file is as follows:
FOR /L %%A IN (1,1,2) DO (
xcopy /s C:\Users\abc\def\folder3 C:\Users\xyz\folder%%A
)
is there anything wrong in the batch file?
Upvotes: 1
Views: 1572
Reputation: 49167
I suggest using this command line in the batch file:
for /L %%A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%%A\" /C /G /H /I /K /R /Q /S /Y >nul
I enclosed both directory paths in double quotes in case of real paths contain 1 or more spaces or other special characters which require double quotes. The last paragraph on last help page output by running in a command prompt window cmd /?
outputs on which characters in a directory/file name double quotes are required around the complete directory/file name.
The target path ends with a backslash to make it clear for console application xcopy
that the target is a directory and not a file. Together with the redundant /I
the target directory is created if not existing already.
For details on the options used on xcopy
open a command prompt window and run xcopy /?
. This outputs the help for this console application in the command prompt window. On Windows running a command or console application with /?
as parameter outputs in general the help for the command/application.
Note: The copying from one user profile directory to another user profile directory requires local administrator privileges. Each user profile directory is by default protected for exclusive usage of the owning user. Therefore I suggest to open a command prompt window and execute in this window:
for /L %A in(1,1,2) do %SystemRoot%\System32\xcopy.exe "C:\Users\abc\def\folder3" "C:\Users\xyz\folder%A\" /C /G /H /I /K /R /S /Y
You can see if that works with just %A
as required on command line instead of %%A
as required in batch files and without /Q
(quiet copying) and without >nul
(redirection of success messages to device NUL to suppress them). Or when it does not work, you can see why it does not work as the error message can be viewed on running a command or a batch file from within a command prompt window instead of double clicking on a batch file because the console window keeps open.
Upvotes: 0
Reputation: 80193
xcopy /s C:\Users\abc\def\folder3\*.* C:\Users\xyz\folder%%A\
where *.*
is an appropriate filemask and the final \
in the destination name tells cmd
that the destination is a directory.
Upvotes: 1