batch file-count the number of files copied over - batch-file

I'm using xcopy to copy over a folder to another, and I use /d command to copy files from last modified. And I want to log the number of files copied every time. How do I do that?

Call xcopy two times: first with the /l option and redirect the output to a file xcopy /l [...] >list.log and second the usual command without /l.

read HELP XCOPY and then use XCOPY /L /Q command to achieve what you want.
or alternatively, I would use ROBOCOPY http://en.wikipedia.org/wiki/Robocopy

Related

Copy multiple files in bat file

I want to copy multiple files using bat file. I have succeeded with this script.
xcopy /-y "C:\Users\Robin\Desktop\bat\test1.csv" "C:\Users\Robin\Desktop\"
xcopy /-y "C:\Users\Robin\Desktop\bat\test2.csv" "C:\Users\Robin\Desktop\"
and I tried to do it in one line, so I use this script
for %I in (C:\Users\Robin\Desktop\bat\test1.csv C:\Users\Robin\Desktop\bat\test2.csv) do copy %I C:\Users\Robin\Desktop\
however it is not working. Can anyone correct my code?
You need to escape the % with another %.
A single % is used to represent command line parameters.
for %%I in (C:\Users\Robin\Desktop\bat\test1.csv C:\Users\Robin\Desktop\bat\test2.csv) do copy %%I C:\Users\Robin\Desktop\
If you're curious to see what is happening, temporarily add pause to your batch file to see what it's doing before the window closes.
Your question says multiple files, but you show only two.
Here are a few examples for you.
You may just get away with:
Copy /-Y "%UserProfile%\Desktop\bat\test*.csv" "%UserProfile%\Desktop"
If you wanted to use a wildcard in a loop.
From a batch file:
#For %%A In ("%UserProfile%\Desktop\bat\test*.csv") Do #Copy /-Y "%%A" "%UserProfile%\Desktop"
Or from the Command prompt:
For %A In ("%UserProfile%\Desktop\bat\test*.csv") Do #Copy /-Y "%A" "%UserProfile%\Desktop"
In a loop with wildcards limiting it to specified names only.
From a batch file:
#For %%A In (1 2) Do #Copy /-Y "%UserProfile%\Desktop\bat\test%%A.csv" "%UserProfile%\Desktop"
Or from the Command prompt:
For %A In (1 2) Do #Copy /-Y "%UserProfile%\Desktop\bat\test%A.csv" "%UserProfile%\Desktop"
In all cases, you could still use XCopy but it isn't required since you're using none of its features. Additionally it was superseded by RoboCopy as far back as Windows Vista!
For information on the use of a command, enter its name at the Command prompt with the question mark option e.g. Copy /?

Xcopy is missing parameters and will not copy files

I am trying to make a .bat file that will copy another batch file to the windows start up directory. but XCOPY keeps saying it is missing parameters, can you have a look at my code and see where I'm going wrong.
xcopy "c:\Desktop\CHAOSCOPY.bat" *.* "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\"
xcopy "c:\Desktop\CHAOS V2.bat" *.* "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\"
xcopy "c:\Desktop\CHAOS.bat" *.* "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\"
xcopy syntax is
xcopy sourcedecription destinationdescription
(disregarding any switches)
What is your code supposed to do? Is "c:\Desktop\CHAOSCOPY.bat" a file you want to copy? If so, copy it to where? What has *.* got to do with the matter? It specifies all files with an extension. Do you want to copy all files with an extension from the current directory and also "c:\Desktop\CHAOSCOPY.bat" to the destination? If so, you need two separate xcopy commands. cmd cannot read your mind - it can't tell which of the three parameters is source and which destination.

Excluding a directory in for loop's search

I have the following line of code:
FOR /R D:\1_FOLDER %f IN (*.jpg,*.png) DO XCOPY %f D:\2_FOLDER /H /C
The first problem what i have, the command doesn't run, i get the error
Invalid numbers of parametres
The second problem in directory 1_FOLDER i have 2 more folders X_FOLDER and Y_FOLDER, i want the for loop to search only in X_FOLDER, and copy only files of the X_FOLDER.
I have the second problem same when i want to copy from C:\ , i want to exclude the Windows folder from for loop's search.
The error message comes from xcopy when you specify unquoted paths containing SPACE characters (or other token separators like ,, ;, =, TAB or non-break spaces (ASCII 0xFF)).
Anyway, actually I do not see any reason to use a for /R loop for the task at hand, because xcopy features an /EXCLUDE option. Here is an excerpt of the help test appearing when typing xcopy /?:
/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.
So for your example, create a text file (let us call it exclusions.lst) with the following content:
D:\1_FOLDER\Y_FOLDER\
Alternatively, if you want every subdirectory called Y_FOLDER in any location to be excluded, write:
\Y_FOLDER\
Since you have multiple file patterns (*.jpg and *.png) to be processed, you could provide an xcopy command line for each of them (let xcopy do the recursion by using the appropriate switches, rather than wrapping a for /R loop around):
xcopy /C /H /K /S /I /EXCLUDE:exclusions.lst "D:\1_FOLDER\*.jpg" "D:\2_FOLDER"
xcopy /C /H /K /S /I /EXCLUDE:exclusions.lst "D:\1_FOLDER\*.png" "D:\2_FOLDER"
Or you could list the file patterns in another text file (let us call it patterns.lst):
*.jpg
*.png
Then you could use a for /F loop to read that file and feed each pattern/line to an xcopy command:
for /F "eol=| delims=" %%P in (patterns.lst) do (
xcopy /C /H /K /S /I /EXCLUDE:exclusions.lst "D:\1_FOLDER\%%~nxP" "D:\2_FOLDER"
)
Do not forget to replace each %% by % if you want to try this in command prompt (cmd) directly.
Both text files exclusions.lst and patterns.lst are expected to be in the current working directory.
Note that all methods shown here do not copy any empty directories from the source.
If you need advanced options for specifying filters to exclude or include certain files, you might be interested in the robocopy command.

Copying a Folder and renaming it using command prompt

I am trying to copy a folder and paste it in the same directory it was copied from.
For example
C:\Test is the main directory which consists of a folder ACDM, I would like to copy ACDM in the same directory and rename the new folder to ACDM1 which will have all the same files as ACDM has
I would like to do it using command prompt
I tried the following
C:>Xcopy C:\Test C:\Test\ACDM1 /E /U
Cannot perform a cyclic copy
0 File(s) copied
which fails, not sure hoe to add REN command with XCOPY command.
Need help ASAP as i would want to create a batch file which will create a copy of an existing folder and rename it according to a name retrieved from a text file..
xcopy "C:\Test\ACDM\*.*" "C:\Test\ACDM1\" /s/h/e/k/f/c
for /f "delims=" %%a in (yourtextfilename) do xcopy "C:\Test\ACDM" "C:\Test\%%a\" /E
as a .bat file line. Directly from the prompt, change each %% to %
I've assumed (for lack of futher information) that your textfile contains just the one line
ACDM1
neither do you specify the textfilename tou want to use.
xcopy C:\Test\ACDM C:\Test\ACDM1\ /E /Q

How to overwrite existing files in batch?

The following command copies and moves a file but I also need it to overwrite the file it's replacing.
xcopy /s c:\mmyinbox\test.doc C:\myoutbox
Add /Y to the command line
You can use :
copy /b/v/y
See SS64 on COPY.
Add /y to the command line of xcopy:
Example:
xcopy /y c:\mmyinbox\test.doc C:\myoutbox
you need to simply add /Y
xcopy /s c:\mmyinbox\test.doc C:\myoutbox /Y
and if you're using path with spaces, try this
xcopy /s "c:\mmyinbox\test.doc" "C:\myoutbox" /Y
If the copy command is run from within a batch job you do not need to use the /Y switch: it will overwrite existing files.
For copying one file to another directory overwriting without any prompt i ended up using the simply COPY command:
copy /Y ".\mySourceFile.txt" "..\target\myDestinationFile.txt"
A command that would copy in any case
xcopy "path\source" "path\destination" /s/h/e/k/f/c/y
If destination file is read only use /y/r
xcopy /y/r source.txt dest.txt
Here's what worked for me to copy and overwrite a file from B:\ to Z:\ drive in a batch script.
echo F| XCOPY B:\utils\MyFile.txt Z:\Backup\CopyFile.txt /Y
The "/Y" parameter at the end overwrites the destination file, if it exists.
You can refer Windows command prompt help using following command : xcopy /?

Resources