Xcopy complete operation successfully, but the copy does not occur - windows

i'm having a weird problem about copying with Xcopy.
I'm using Windows Server 2008 R2. There is a Batch file (.CMD) - in fact, 2 Batches - which executes many operations. Among them there are 2 Xcopy.
The first Xcopy completes the copy successfully. Then some operations of batch are executed and...the second Xcopy is executed after some time. It shows a successfully status, like "300 files copied" but...the files are not copied !
THE strange behaviour: i put a pause command into the Batch and, after this second weird Xcopy, i run THE SAME command at the Prompt and..it works !!!
I cannot explain this, so i'm asking for help here. The full command is:
xcopy /s /e /h /r /i /y E: D:
As a matter of curiosity/information, the other Xcopy (which worked!) is:
xcopy /s /e /h /r /i /y E: D:
Or....the same command !!! Each one runs in a separated Batch file - the first Batch "calls" the second one.
E: is CD-ROM, so after the first Xcopy, the first Batch asks for another Disc and calls the second Batch, which (supposed) copies entire CD content to D: (HDD).
I'm sorry if the question is silly and i'm not realizing how simple is to correct the erratic behaviour of Xcopy. Thanks in advance...

Thanks very much Bali C !
But i found the problem: the correct syntax is
xcopy /s /e /h /r /i /y E:\ D:\
You can see that the backslashes made all the difference ! The files were being copied to another directory, in fact, the current working directory. With backslashes the copy is fine.
But i appreciate your suggestion...

To copy the entire contents try using
xcopy /e /h /r /i /y /t E:\*.* D:\
I left out the /s as it contradicts the /e switch, I doubt it will be the source of the problem but it's worth a shot. I used the \t switch to copy the directory structure of the source.
I have also used wildcards *.* to copy the contents, rather than just the drive letter, some things work at cmd prompt but not in batch, but try using this.
Another option would be to use robocopy.

Related

Trying to learn batch script, what does this do: /letter?

Here's the batch file:
#echo off
xcopy c:\testsource c:\testbackup /m /e /y
So I know that "xcopy c:\testsource c:\testbackup" copies the files in the testsource directory and pastes them in the testbackup directory, but what does the "/m /e /y" do? Can you explain what each individual part of this section of code does?
Also, would you mind telling me where I could look up explanations of syntax like this on my own in the future so I don't have to keep asking simple questions like this (a command for command prompt, maybe online database of commands and explanations, just something more efficient than google).
Basically, in batch you first enter the command to be excuted, and then supply the arguments. Each command can be seen as an own small programm.
you are calling the command xcopy and supply those five arguments to it.
A documentation can be found here: https://technet.microsoft.com/de-ch/library/cc771254.aspx?f=255&MSPPError=-2147217396
you can also type in help COMMAND, for example help xcopy, to see a full list of arguments which can be passed to the command
when in doubt, use the mighty /? behind most commands to get more information and extra switches. i.e: xcopy /? additionally, information on the /m /e and /y switches:
xcopy /M Copies only files with the archive attribute set,
turns off the archive attribute. xcopy /e Copies directories and subdirectories, including empty ones. and xcopy /y Suppresses prompting to confirm you want to overwrite an
existing destination file.
hope that helped!

xcopy returns error "Invalid number of parameters" when exclude parameter is set

Issuing:
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y
works as expected. However:
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:"Y:\...\exclude.txt"
returns error:
Invalid number of parameters
Which also occurs when path names (containing spaces) are not enclosed by quotation marks. This however, is not the case. Paths (edited for readability) all correspond correctly. Syntax (as per Product Documentation - Xcopy) is also correct. Concerning OS is Windows XP Professional x32 SP3.
Why is second cmd returning error and how is it to be solved? I am not looking for alternatives to xcopy (robocopy etc.).
XCOPY is an old command harking back to the days of DOS. It looks like the /EXCLUDE option was never updated to support long file names. Ugh :-(
If you remove the quotes, then the text after the space is interpreted as an additional parameter, and you get the "Invalid number of parameters" error. If you keep the quotes, then it treats the quotes as part of the path, and reports it cannot find the file.
I believe you have three possible solutions:
1) Use the short 8.3 folder names in your path.
Of course this cannot work if your volume has short names disabled.
2) Use the SUBST command to create a drive alias for your troublesome path.
subst Q: "Y:\path with spaces"
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:Q:exclude.txt
subst Q: /d
This could be a problem if you don't know a drive letter that is free.
3) (my favorite) Simply PUSHD do the troublesome path and run the command from there :-)
pushd "Y:\path with spaces"
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:exclude.txt
popd
See https://sevenx7x.wordpress.com/2009/01/02/xcopy-with-exclude-option-shows-cant-read-file/ and http://forums.majorgeeks.com/showthread.php?t=54300 for more information.
/EXCLUDE:file switch will not exclude the file specified. As per xcopy command reference:
/exclude:FileName1[+[FileName2][+[FileName3](…)] Specifies a list of
files. At least one file must be specified.
Each file will contain search strings with each string on a separate line in the file. 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.
It took me some time to get this right as well (I had the same errors), but ultimately, this format worked for me. As with all things DOS, absolute precision is critical, so feel free to copy and paste the below.
xcopy /t /e "C:\Users\username\Your Folder" "C:\Users\user\Your Folder"

xcopy syntax? (batch)

#echo off
xcopy /s/z/i Q:\U1210.exe C:\Users\jalozinski\Desktop\
START C:\Users\jalozinski\Desktop\U1210.exe
So this purpose of this code is to copy U1210.exe from the Q:\ drive to the desktop, then start the newly copied .exe. But for some reason it copies random folders and files from the Q:\ drive, and I don't know why. I have a feeling it has to do with the /s/z/i (i was trying these so it may be oen of them) or something to do with the filepath of the source. I feel like if I close the filepath it won't fix anything though.
This is batch, by the way. :I
Lets look at what you have for the XCOPY:
/S = Copies directories and subdirectories except empty ones.
/Z = Copies networked files in restartable mode.
/I = If destination does not exist and copying more than one file, assumes that destination must be a directory.
First I would try w/o the /S, because you don't need directories.
I would include /R (Overwrites read-only files.)
I would also include /Y (Suppresses prompting to confirm you want to overwrite an existing destination file.)
This is what I got working the way I think you want it:
#ECHO OFF
set source=Q:\U1210.exe
set dest=C:\Users\jalozinski\desktop\
xcopy %source% %dest% /Z /R /Y
start %dest%\U1210.exe
exit
#echo off
xcopy Q:\U1210.exe C:\Users\jalozinski\Desktop\
START C:\Users\jalozinski\Desktop\U1210.exe
that should be all you need
/s is copying all the subfolders
/i is assuming it is a directory (when in doubt)
/z is a preventitive measure if you have a very slow computer
so you should not need any of those commands

batch/bat to copy folder and content at once

I'm writing a batch script that does a copy. I want to script it to copy an entire folder. When I want to copy a single file, I do this
copy %~dp0file.txt file.txt
If I have a folder with this structure, is there a command to copy this entire folder with its contents all at once while preserving the exact structure.
mainfolder/
file1.txt
file2.txt
insidefolder/
file3.txt
file4.txt
file5.txt
if you have xcopy, you can use the /E param, which will copy directories and subdirectories and the files within them, including maintaining the directory structure for empty directories
xcopy [source] [destination] /E
The old way:
xcopy [source] [destination] /E
xcopy is deprecated. Robocopy replaces Xcopy. It comes with Windows 8, 8.1 and 10.
robocopy [source] [destination] /E
robocopy has several advantages:
copy paths exceeding 259 characters
multithreaded copying
More details here.
I suspect that the xcopy command is the magic bullet you're looking for.
It can copy files, directories, and even entire drives while preserving the original directory hierarchy. There are also a handful of additional options available, compared to the basic copy command.
Check out the documentation here.
If your batch file only needs to run on Windows Vista or later, you can use robocopy instead, which is an even more powerful tool than xcopy, and is now built into the operating system. It's documentation is available here.
For Folder Copy You can Use
robocopy C:\Source D:\Destination /E
For File Copy
copy D:\Sourcefile.txt D:\backup\Destinationfile.txt /Y
Delete file in some folder last modify date more than some day
forfiles -p "D:\FolderPath" -s -m *.[Filetype eg-->.txt] -d -[Numberof dates] -c "cmd /c del #PATH"
And you can Shedule task in windows perform this task automatically in specific time.
I've been interested in the original
question here and related ones.
For an answer, this week I did some
experiments with XCOPY.
To help answer the original question, here
I post the results of my experiments.
I did the experiments on Windows 7 64 bit
Professional SP1 with the copy of XCOPY
that came with the operating system.
For the experiments, I wrote some code in
the scripting language Open Object Rexx
and the editor macro language Kexx with
the text editor KEdit.
XCOPY was called from the Rexx code. The
Kexx code edited the screen output of
XCOPY to focus on the crucial results.
The experiments all had to do with using
XCOPY to copy one directory with several
files and subdirectories.
The experiments consisted of 10 cases.
Each case adjusted the arguments to XCOPY
and called XCOPY once. All 10 cases were
attempting to do the same copying
operation.
Here are the main results:
(1) Of the 10 cases, only three did
copying. The other 7 cases right away,
just from processing the arguments to
XCOPY, gave error messages, e.g.,
Invalid path
Access denied
with no files copied.
Of the three cases that did copying, they
all did the same copying, that is, gave
the same results.
(2) If want to copy a directory X and all
the files and directories in directory X,
in the hierarchical file system tree
rooted at directory X, then apparently
XCOPY -- and this appears to be much of
the original question -- just will NOT do
that.
One consequence is that if using XCOPY to
copy directory X and its contents, then
CAN copy the contents but CANNOT copy the
directory X itself; thus, lose the
time-date stamp on directory X, its
archive bit, data on ownership,
attributes, etc.
Of course if directory X is a subdirectory
of directory Y, an XCOPY of Y will copy
all of the contents of directory Y WITH
directory X. So in this way can get a copy
of directory X. However, the copy of
directory X will have its time-date stamp
of the time of the run of XCOPY and NOT
the time-date stamp of the original
directory X.
This change in time-date stamps can be
awkward for a copy of a directory with a
lot of downloaded Web pages: The HTML
file of the Web page will have its
original time-date stamp, but the
corresponding subdirectory for files used
by the HTML file will have the time-date
stamp of the run of XCOPY. So, when
sorting the copy on time date stamps, all
the subdirectories, the HTML files and the
corresponding subdirectories, e.g.,
x.htm
x_files
can appear far apart in the sort on
time-date.
Hierarchical file systems go way back,
IIRC to Multics at MIT in 1969, and since
then lots of people have recognized the
two cases, given a directory X, (i) copy
directory X and all its contents and (ii)
copy all the contents of X but not
directory X itself. Well, if only from
the experiments, XCOPY does only (ii).
So, the results of the 10 cases are below.
For each case, in the results the first
three lines have the first three arguments
to XCOPY. So, the first line has the tree
name of the directory to be copied, the
'source'; the second line has the tree
name of the directory to get the copies,
the 'destination', and the third line has
the options for XCOPY. The remaining 1-2
lines have the results of the run of
XCOPY.
One big point about the options is that
options /X and /O result in result
Access denied
To see this, compare case 8 with the other
cases that were the same, did not have /X
and /O, but did copy.
These experiments have me better
understand XCOPY and contribute an answer
to the original question.
======= case 1 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_1\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 2 ==================
"k:\software\dir_time-date\*"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_2\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 3 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_3\"
options = /E /F /G /H /K /O /R /V /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 4 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_4\"
options = /E /F /G /H /K /R /V /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 5 ==================
"k:\software\dir_time-date\"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_5\"
options = /E /F /G /H /K /O /R /S /X /Y
Result: Invalid path
Result: 0 File(s) copied
======= case 6 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_6\"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 7 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_7"
options = /E /F /G /H /I /K /R /S /Y
Result: 20 File(s) copied
======= case 8 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_8"
options = /E /F /G /H /I /K /O /R /S /X /Y
Result: Access denied
Result: 0 File(s) copied
======= case 9 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_9"
options = /I /S
Result: 20 File(s) copied
======= case 10 ==================
"k:\software\dir_time-date"
"k:\software\xcopy002_test\xcopy002_test_dirs\output_sub_dir_10"
options = /E /I /S
Result: 20 File(s) copied

How can I move the contents of one directory tree into another?

I have a directory which contains files and a number of levels of subdirectories:
C:\Source
I would like to move the contents of C:\Source into:
C:\Destination
Requirements:
All files and all subdirectories
within C:\SourceData must be moved
I will be running the command in a
batch file
I can't use Powershell or
any other scripting languages
Attempt 0
XCOPY /E "C:\Source" "C:\Destination"
This works perfectly, but it copies instead of moves. I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time.
Attempt 1
MOVE "C:\Source" "C:\Destination"
This moves the entire C:\Source directory into C:\Destination so I end up with:
C:\Destination\Source
Attempt 2
With some help from this question and accepted answer I came up with:
for /r "C:\Source" %%x in (*) do move "%%x" "C:\Destination"
This moves the files within C:\Source but not the subdirectories or their contents. Note that I used %%x instead of %x as I'm using it in a batch file.
Using FOR seems promising but I'm not sure I've used the right syntax? Have I missed something?
Attempt 3
As suggested by Nick D, I tried rename:
RENAME "C:\Source" Destination
For the example scenario I gave this works fine. Unfortunately my real Destination directory is at a different level to the Source directory and this doesn't seem to be supported:
C:\>REN /?
Renames a file or files.
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
I get "The syntax of the command is incorrect." errors if I try to specify a more complex destination path, for example:
RENAME "C:\Source" "C:\MyOtherDirectory\Destination"
RENAME "C:\Source" "MyOtherDirectory\Destination"
Undoubtedly use robocopy. It is a simple but brilliantly useful tool.
robocopy /move /e sourcedir destdir
This will move all the files and folders, including empty ones, deleting each original file after it has moved it.
If you don't have robocopy installed you can download it by itself or as part of a Microsoft resource kit.
Update:
Adjusted code to a. check whether folders already exist at the destination, in which case move files in that folder over (and continue traversing the source directory structure), otherwise move the folder wholesale.
At the end of the script the source folder is removed altogether to eliminate these folders which have had their files moved over to an already existent folder at the destination (meaning these folders have been emptied but not deleted at the source).
Additionally we check whether a folder is both empty and already exists at the destination in which case we do nothing (and leave the source folder to be deleted to the last line of the script). Not doing this results in "The filename, directory name, or volume label syntax is incorrect." errors.
Phew! Please let me know how you get on with this! I have tested this and it seems to be working well.
for /d /r "c:\source" %%i in (*) do if exist "c:\destination\%%~ni" (dir "%%i" | find "0 File(s)" > NUL & if errorlevel 1 move /y "%%i\*.*" "c:\destination\%%~ni") else (move /y "%%i" "c:\destination")
move /y c:\source\*.* c:\destination
rd /s /q c:\source
In response to ufukgun's answer:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
/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.
/i - If destination does not exist and copying more than one file, assumes that destination must be a directory.
/y - Suppresses prompting to confirm you want to overwrite an existing destination file.
As stated in another answer, using xcopy may be not as efficient as it would be a native move command that only changes pointers in the filesystem.
Since XCOPY works, you could use XCOPY and DELETE, it's a workaround, but should work?
on Vista use
robocopy source destination /MIR
/MIR .. mirror a complete directory tree (also deletes files in the destination)
else
xcopy
Of course you have to delete the source afterwards :)
I have a directory which contains
files and a number of levels of
subdirectories:
C:\Source
I would like to move the contents of
C:\Source into:
C:\Destination
Maybe I'm missing something, but can't you just rename the folder?
As sent on twitter:
Try combining attempt 3 with attempt 1. Rename to get the destination folder correct, then move "Destination" to the correct place.
#echo on
set SOURCE=C:\Source
set DESTINATION=C:\Destination
xcopy %SOURCE%\* %DESTINATION%\* /s /e /i /Y
PAUSE
i use batch file like this...
or simply call:
xcopy C:\Source\* C:\Destination\* /s /e /i /Y
I use a utility called xxcopy. It can move files and have many useful options, you can use it like this :
xxcopy C:\Source C:\Destination /E /RC
the options :
/E to copy everything even empty folders
/RC to remove every source file after each successful copy
you can download free copy of xxcopy for personal use from :
http://www.xxcopy.com/xcpydnld.htm
Maybe a little off-topic, but still usefull:
Some people suggest to just copy + delete the source files, but
moving files is not the same as copying + deleting!
When using the (x)copy function, you allocate new space on the same volume on a harddisk. After the copying the files, the old files (the old allocated space which was required for the files) are beign marked as deleted. While you will achieve the same result as an end-user, moving does something diffrent and more efficient.
Moving files actually only changes some records in the MFT (master file table). These changes only consist of a different path for the user to locate its files. Physically the files still remain in the same sectors on the harddisk.
If all your trying to do is move a directory and the content up one level:
MOVE folder_you_wan_to_move ..
Note that .. refers to the next directory up.
I leave this code I have written based on ljs's attempt,
It moves directory trees as Windows does, overwriting the files that already exists in destination with those in source, and avoiding the slow copy and erase method except if the destination is in a different drive.
If your S.O is not in english must change line 6 with the text used by the Dir command when it finds 0 files.
movedir.bat source_dir destination_dir
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
if %1.==. echo movedir dir1[\*] dir2 & echo move dir1 inside dir2 overwriting coincident files & echo option \* moves only dir1 content & goto end
set S=%~f1
set D=%~f2
set "noFiles= 0 File"
set "R=0" rem R is option [\*] flag
if %S:~-2%.==\.. set "S=%S:~,-2%" & set "R=1"
if not exist "%S%" goto paramERR
if not exist "%D%" goto paramERR
set "Trim=0" & set "Sc=%S%"
:LP
set /A Trim+=1 & (set "Sc=!Sc:~1!") & if NOT !Sc!.==. goto LP
if %R%==0 (if exist "%D%\%~n1%~x1" (set "D=%D%\%~n1%~x1")) else move /y "%S%" "%D%" & goto end
CALL:movefiles "%S%" "%D%"
for /D /R "%S%" %%I in (*) do (set "Way=%%~fI") & (set "Way=!Way:~%Trim%!") & if exist "%D%!Way!" (CALL:movefiles "%%I" "%D%!Way!") else (move /y "%%I" "%D%!Way!\.." > NUL)
rd /s/q "%S%" & if %R%==1 md "%S%"
goto end
:movefiles
dir %1 | find "%noFiles%" > NUL & if ERRORLEVEL 1 move /y "%~1\*.*" %2 > NUL
goto :eof
:paramERR
echo Source or Destination does not exist
:end
I think the easiest thing you could do is to modify your Attempt 2
from
MOVE "C:\Source" "C:\Destination"
to
MOVE "C:\Source\*" "C:\Destination"
KISS ;-)
Edit: this seem not to work, so my advice is to trash away the crappy DOS command line and use Cygwin with BASH as a shell! (or just add the cygwin binaries to the path so you can use mv within DOS, thus not having to change the shell as your requirements state).

Resources