How to overwrite existing files in batch? - batch-file

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 /?

Related

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.

Use batch script to copy directory

I want to copy a folder to the same directory and change its name. Let's say, if I have a folder "C:\Users\Desktop\A", how can I copy it, rename it as "B", and still put it at "C:\Users\Desktop"?
Thanks.
xcopy "C:\Users\Desktop\A" "C:\Users\Desktop\B" /e /i
From the command prompt
xcopy source target /E
/E recursivly copies files and directories.
So:
xcopy c:\users\Desktop\A c:\users\Desktop\B /E

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

batch file-count the number of files copied over

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

Directo copy batch file command

How can copy a folder(located in the location of batch file)containing another folders and files to another location using batch file...
i am trying using command below but not working..
xcopy /E /Y %~p\DagwoodLoki#hp.com\* C:\Loki
Please help me
I dont know what "%~p" does but i think that following should work
xcopy %~p\DagwoodLoki#hp.com\ C:\Loki /E /Y
the /E and /Y must be written later the source and the target.
Maybe this?
xcopy /E /Y "%~dp0\DagwoodLoki#hp.com\*" C:\Loki
%~dp0 is substituted with drive+path to the batch file, without quotes, if any.

Resources