For loop copying files in subfolders that contain hyphens [duplicate] - batch-file

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.

Related

Move all the contents of a folder with a .bat

I using the following syntax to move all the files inside a folder, to another folder
set OGGI=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%
move C:\DOCUMENTALE\dms_in\*.* C:\DOCUMENTALE\dms_backup\%OGGI%\
This syntax is working with files, but which syntax I have to use if I would move also folders?
The problem is that this path C:\DOCUMENTALE\dms_in\ contains folders automatically generated. I don't know how many folders there are in or which names they have.
I did some attempt but I didn't figure it out
Thanks
An solution to this is to use the command ROBOCOPY instead. It is arguable more flexible.
ROBOCOPY <source> <destination> /MOV /E
/MOV will move the file/folder in question instead of the default copy.
/E will allow you to mirror the directory structure of the target folder if there are any folders within the folder including empty ones
Note: you may need to use quotation marks for the source and destination if either path contains spaces.
Another neat feature of Robocopy is that it will show you what is it doing; what is being copied and too where, how long it takes etc. And if it goes wrong due to paths for example, it will tell you.
You can also browse ss64.com for more help and examples to fit your needs.

Robocopy directory and its contents

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.

Command Prompt Wildcard File Delete Incorrectly Matching

On a regular basis, I am trying to clean up some data folders for an ERP program prior to doing a backup and performing maintenance on the data tables. I've been using Windows Explorer to search for extraneous backup and temporary files prior to the full backup (the maintenance procedures create backup files during the process that aren't always removed), but I'd like to just run it all through a batch file to simplify and speed up the process. I'm filtering with the following:
*NGT????????????.old
*Wrk*????????????.m4t
Also, the command I'm using:
del /S /Q
Both of these work perfectly though the search function within Explorer. The first one works correctly in a command prompt, but the second doesn't. The series of ?s are created by the ERP software as a time stamp, to indicate a copy of the original was created at that time. And the second * represents a one or two character user ID that indicates the user that created the file (it isn't all that important except that the character length isn't always the same). When I try to filter in the command prompt with that second filter, not only does it grab the files I want it to, but it also grabs the original source files which DO NOT have a time stamp on them. For example, the following file names:
File 1) AR_AgedInvoiceReportWrk.M4T
File 2) AR_AgedInvoiceReportWrkTB081615903027.M4T
File 2 is the only one that should be deleted, but it will delete both File 1 and File 2. I've even tried using two or three ?s instead of the second * just to see if a difference would occur, but it doesn't.
Does the command prompt not recognize the ? the way Explorer does? What am I missing?
DIR and it seems other tools match the short file name and the long filename. Your short filenames have wrk as the leading characters and then you are matching a whole swag of any-character.
A solution is to use something like DIR /b /a-d and pipe it through findstr with a regexp, and that will match only the long filenames.

XCOPY exclude list ignored after first exclusion

I have a batch file I've created which uses xcopy to copy a dir, and child dirs, and merge them into one file. (ie. all my modulised development css stylesheets merged into one production stylesheet).
Everything is good, apart from the fact that when I reference the excludelist.txt, it only excludes the first line, and not the subsequent files I want excluded.
Anyone fancy a crack at this? You'd be most helpful.
Here's the code:
XCOPY C:\xampp\htdocs\PROJECT\css\*.* C:\temp /S /I /Y /EXCLUDE:C:\xampp\htdocs\PROJECT\exclude.txt
...and inside my exclude.txt is...
1.css
2.css
3.css
4.css
5.css
///// I know the code works (to an extent) because it is infact excluding file 1.css -- just not the ones below it. Am I right to put each exclusion on a new line?
I use the following,
xcopy "C:\users\dad\*.*" dad /s /d <yesnoyesno /EXCLUDE:excluexclu 1>cop.txt 2>err.txt
as somewhere on the web I saw a note to the effect that the /Y switch could not be used directly with an exclude file.
What I wanted to point out here, was the useful output files 1 & 2, which detail the success & failure issues.
Mine shows only success.
The short answer: Create a new text file, type the entries and save the file.
The longer explanation: I ran into this very issue on a Windows 2008 server today. I tried all kinds of things to get it to work. Forced notepad to save the file as ANSI, Unicode and UTF-8 but all of them had the same problem. Finally I started thinking about the fact that I put the file on the server via FTP and it's quite likely that FTP tweaked the file format. I created a new text file on the server, typed all the same entries in the new file and now it works.
I had a similar problem. I was trying to exclude all files with a certain extension in the root folder and any sub-folders and it didn't work. The reason was I was putting *.pdb instead of just .pdb. The newline/carriage return thing was a total red herring for me.
So my file just looked like:
.pdb
.obj
.vb
.cs
You seem to be using xcopy correctly - each exclusion file (or match depending on wildcards) should be on a new line within the file. So far I've been unable to reproduce the behaviour you're experiencing.
Is the exclude.txt listing you've given above a test file, or are they the actual css names?
What are the names of the other files that your batch file is supposed to copy?
Edit:
That the xcopy is failing to exclude further files after a single match is giving me most pause. I thought it might be to do with the type of carriage-return that was used in the exclude file, but xcopy handles unix-style carriage-returns just fine.
Can you re-verify that the correct exclude file is being used?
Try forcing it to save with ANSI encoding on your text editor.
I was having a similar issue and that did it.

How to say no to all "do you want to overwrite" prompts in a batch file copy?

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\

Resources