We are in the process of migrating files from one share to another. We have built a tool in which the user can select directories and/or individual files to be copied to a destination share. The tool generates an individual RoboCopy command for each of the files or directories in the collection that results from the selection made by the user.
We are having problems if an individual file to be copied starts with a dash, for instance:
robocopy c:\temp c:\temp2 -a.txt
RoboCopy bails out with: ERROR : Invalid Parameter #3 : "-a.txt"
We tried the usual suspects (quotes around the filename etc.), but so far nothing seems to work. Any idea how to get around this, without resorting to renaming the file prior to copying?
This appears to be a bug in robocopy; it has some other known similar ones:
https://support.microsoft.com/en-us/kb/2646454
Here's a possible workaround:
robocopy c:\temp c:\temp2 *-a.txt /xf *?-a.txt
*-a.txt will still match "-a.txt", but it also matches "x-a.txt", "xx-a.txt", etc.
The /xf file exclusion knocks out "x-a.txt", "xx-a.txt", and any other file with characters (specifically, at least one character) in front of the hyphen.
I've confirmed that the above command will match only "-a.txt" even if c:\temp also contains these files:
other folder\-a.txt
-a.txt1
-a1.txt
x-a.txt
xx-a.txt
I'm not 100% confident though, so you might want to think up some other filenames to test that against.
I feel like this should be straightforward, but I've been struggling with this for long enough that it's making me pull my hair out. I'm writing a couple batch files to do automated file syncing and some cleanup between a few different drives. When copying a directory I use the following command.
robocopy %datestr% %2 /E
What this line of code does is take the contents of the path specified by %datestr% and copies it to the path specified by %2. I need it to copy the directory itself (and its contents, of course) to the path specified by %2. I looked at the Robocopy docs and I can't seem to find a simple way to do this.
EDIT: I fixed the problem I was having, but it feels more like a hack than a fix. Appending the source folder to the destination path 'copies' it, as such:
robocopy %datestr% "%2/%datestr%" /E
I don't love the fact that this isn't truly 'copying' it, but just making a new directory with the same name. For what I'm using this it will work fine, but I still feel as if there must be a better way to do so.
Problem
I'm trying to move a folder (ex. \\myUNC\folder1) and its contents to another folder (ex. \\myUNC\Parent\folder1). There are two easy enough ways I'd typically do this - either using move (similar to here) or using ren (similar to here).
Example Code
set oldPath=\\myUNC\folder1
set newPath=\\myUNC\Parent\folder1
move "%oldPath%" "%newPath%"
::ren"%oldPath%" "%newPath%"
Troubleshooting
When attempting the move solution in my first like, I get the error:
The filename or extension is too long. 0 dir(s) moved.
As a result, I tried ren like in my second link, which gave the error:
The syntax of the command is incorrect.
For this second error, I'm assuming that is because I'm passing the path as part of my variable - which ren doesn't accept. The batch calling this change is NOT in the same directory as the folder or its new path. As a result I can't use current directory code (like ren or cd), at least as far as I know.
If anyone has a possible solution it would be greatly appreciated! Thanks.
The filename or extension is too long. 0 dir(s) moved.
This error refers to a 'feature' in Windows that limits file names to a maximum of 255 characters. To overcome this, you would need to shorten the names of the folders on the Network drive. See Maximum filename length in NTFS (Windows XP and Windows Vista)?
The syntax of the command is incorrect.
This error occured because you cannot state a new folder for the ren command. A file can only be renamed in the same folder, not even subdirectories are allowed. But the reason why you got The syntax of the command is incorrect. error, is because you left out a space in between the ren and the "
Possible solution:
Depending on your scenario, you might be able to pushd into the folder and then use the move command. In my experience, some commands don't respond equally to UNC locations vs local file locations (ex. if exist "\\UNC\"):
#echo off
pushd \\myUNC\
set oldPath=folder1
set newPath=Parent\folder1
move "%oldPath%" "%newPath%"
popd
This will only work though, if you haven't exceeded the 255 char limit
After a lot of troubleshooting, I was able to find a solution with no errors!
Solution
set oldPath=\\myUNC\folder1
set newPath=\\myUNC\Parent\folder1
robocopy /move "%oldPath%" "%newPath%"
Why
Robocopy is a newer command from Microsoft, and accounts for filename strings longer than 256 characters (apparently the issue with move and/or copy command).
You can google robocopy to learn more about the command options and parameters, but its fairly straight forward. For my issue, I wanted to move the file, so I just used the /move option, which deletes the original folder and files.
I'm trying to write a small bat script to put on my teams desktops and allow them to update their personal macro file with mine when ever I push out an update or have created new tools.
I have the following
xcopy "O:\abc Supply chain\Supply Chain Team\David Peters\Excel\Macro File" "%AppData%\Roaming\Microsoft\Excel\XLSTART\" /y
under CMD is says 1 files copied yet there isn't anything in the XLSTART folder.
can you pleased tell me what I'm doing wrong
many thanks
Not sure about your configuration, but for me the Roaming folder is already included in the value of the %AppData% variable.
"%AppData%\Roaming\Microsoft\Excel\XLSTART\"
^......^
So, probably you should use "%AppData%\Microsoft\Excel\XLSTART\"
By default, copying from the command prompt will prompt you to overwrite files that already exist in the target location.
You can add "/Y" to say "Yes to all" replacements.
But how can you say "No to all" ?
In other words, I want to copy everything from one directory that does not already exist in the target.
The closest thing I see is the XCOPY argument to only copy things after a specific mod-datetime.
Unless there's a scenario where you'd not want to copy existing files in the source that have changed since the last copy, why not use XCOPY with /D without specifying a date?
echo "No" | copy/-Y c:\source c:\Dest\
You can make a text file with a single long line of "n" then run your command and put < nc.txt after it. I did this to copy over 145,000 instances where "No overwrite" was what I wanted and it worked fine this way.
Or you can just hold the n key down with something, but that takes longer than using the < to pipe it in.
Here's a workaround. If you want to copy everything from A that does not already exist in B:
Copy A to a new directory C.
Copy B to C, overwriting anything that overlaps with A.
Copy C to B.
I use XCOPY with the following parameters for copying .NET assemblies:
/D /Y /R /H
/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.
/Y - Suppresses prompting to confirm you want to overwrite an existing destination file.
/R - Overwrites read-only files.
/H - Copies hidden and system files also.
I know you all think /D: date is going to use date stuff, but just /D without the: does exactly what we want so...
xcopy {Source} {Destination} /E /D
Will copy without overwriting to pickup those files that are new or maybe failed before for some reason.
Just try it, it works.
I expect xxcopy has an option for that.
Bingo:
http://www.xxcopy.com/xxcopy27.htm#tag_231
2.3 By comparison with the file in destination
The switches in this group select files based on the
comparison between the files in the source and those in
the destination. They are often used for periodic backup
and directory synchronization purposes. These switches
were originally created as variations of directory backup.
They are also convenient for selecting files for deletion.
2.3.1 by Presence/Absence
The /BB and /U switches are the two switches which select
files by the pure presence or absence as the criteria.
Other switches in the this group (Group 2.3) are also
affected by the file in the destination, but for a
particular characteristics for comparison's sake.
/BB Selects files that are present in source but not in destination.
/U Selects files that are present in both source and destination.
-Adam
this works fine
no | cp -rf c:\source c:\Dest\
echo N | copy /-y $(SolutionDir)SomeDir $(OutDir)
Try this:
robocopy "source" "destination" /e /b /copyall /xo /it
Copy that line into notepad and save as a .bat file. Run the file and it will copy everything from the source to the destination. When you run it again it will not replace files that are identical. when you change or a file changes it will replace the file at the destination.
test it out. I created a .txt file with a few works, ran the script, change the wording on the .txt file and ran the script again, it replace only the change file from the source.
/e=Copies subdirectories. Note that this option includes empty directories
/b=Copies files in Backup mode
/copyall=Copies all file information
/xo=Excludes older files. (this is what prevents it from copy the same file over and over)
/it=Includes "tweaked" files. (this will allow the copy and replace of modified files)
Thanks for this. I am using the command line utility AzCopy (v 3.1.0.93) to move ~1 million files from my local PC to my Azure blob storage. I've got some duplicates and cannot babysit the copy to answer each prompt and don't want to re-upload the same file.
The AzCopy utility offers a /Y command to suppress the confirmation prompts but ends up telling it to overwrite the destination file. Going this route I was able to get it to NOT re-upload the file. However, it does seem like a bit of a hack since it is not actually answering the prompt with "No", instead I get the error "No input is received when user needed to make a choice among several given options." but does not upload the file.
Here is the command I used: echo n | AzCopy /Source:"{file path}" /Dest:"{blob storage URL}" /DestKey:{key}
Hope this helps the next guy.
Depending on the size and number of files being copied, you could copy the destination directory over the source first with "yes to all", then do the original copy you were doing, also with "yes to all" set. That should give you the same results.
We used "robocopy" through "invoke-command" to copy a huge amount of VMs in our environment. We've discovered that "robocopy" unexpectedly exits sometimes and the whole proccess goes to down. So we've decided to use "xcopy". Now we're checking it's work and to "create" "Not for all" option we use that function (powershell):
function gen_long_no([string]$path) {
$result = ""; Get-ChildItem $path -Recurse | ? { if ($_.PSIsContainer -eq $false) { $result += "n" } };
return $result
}
Maybe helps somebody.
Adding the switches for subdirectories and verification work just fine.
echo n | xcopy/-Y/s/e/v c:\source*.* c:\Dest\