Using xcopy with wildcard character - batch-file

I have a requirement of copying few files in to a folder. I am not sure what the folder name will be but I am sure that the folder will be ending with -Distribution.
I have written a command:
xcopy D:\config.dat D:\All_Dist\basic_dist\*-distribution\/R
But I am getting an error message as:
File creation error - The filename, directory name, or volume label syntax is incorrect.

for /d /r "D:\All_Dist\basic_dist" %%a in (*-distribution) do (
copy "d:\config.dat" "%%~fa"
)
Search for the required folder, then copy the file into it.

Since my directories and files are named differently from yours, I tried to match what you are trying to do and I got this to work.
xcopy low\javadeployreg.* \Users\Philip\appData\Local\temp\*-Dist /R/Y
However, if I include the last backslash after the "*-Dist" I get the same error as you but with the added information Unable to create directory - C:\Users\Philip\appData\Local\temp*-Dist so it looks like you just have to remove that last backslash.
the /Y is needed or you will get a prompt for each file you attempt to overwrite a file.

Related

How can I suppress an error message when using move with a wildcard on an empty directory?

I am using the following command to move files from source to destination:
move c:\src\* c:\dst
This works when there are files in src but gives the following error message when the directory is empty:
The filename, directory name, or volume label syntax is incorrect.
How can I suppress this error? This is a cosmetic/management/UX issue, as if there are no files to move then I don't need to take any further action.
So your real goal is to conditionally move files only when files exist in the source folder.
dir /b /a-d c:\src\* >nul 2>nul && move c:\src\* c:\dst

Create batch file for copy all the contained in root folder (including folder and files) to another folder

I have a batch file to test copy all the contain of the folder to another folder as below:
XCOPY /S /Y "C:\Windows\Source\1\*.*" "C:\Windows\Source\O365PP\Office\Data"
But, when i run it, It will never copy a single files from the source and the cmd screen was shown and won't disappear. Kindly advice.
Thank you
First, you'll have to tell XCOPY if the destination is a File or Directory:
C:\Scripts>xcopy /s /y c:\Scripts\Test\*.* c:\scripts\mine
Does C:\scripts\mine specify a file name or directory name on the target
(F = file, D = directory)?
In this case, adding the switch /I would help.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
Also, double quotes are not needed UNLESS your filenames have a space.
#franklin is correct saying the destination needs the \*.*

dos command delete folder and files using a wild card character

In a DOS batch command window, I want to delete folders (and corresponding files within that directory) with part of the name that contains the following string (SUB
I want to start at a specfic root directory called C:\app2\proc.
I want to delete the directory and the files contained within the directory.
I want to delete folders where part of the file name is (SUB.
What I have tried so far does not work.
Here is what I have tried to far:
del /f /s /q C:\app2\proc\*(SUB*
Note: the asterik before (SUB and the asterik after (SUB is not showing up in the display of what I have tried to far.
Thus can you tell me how to solve this problem?
You can use a for loop to run through your files and send a variable (%x in this case) to rmdir with your path. Try this:
for /d %x in (*(sub*) do rmdir /s /q c:\app2\proc\%x
You porbably need to escape that ( character so that it does not get evaluated.
Checkout http://www.robvanderwoude.com/escapechars.php it seems you should replace ( with ^(

xcopy does not create directory structure

I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:
I am using xcopy <src> <dest> /s/y.
<src>=C:\sourcefolder\a\b\c\d\something.java and
<dest>=C:\destinationfolder.
Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .
what I want is C:\destinationfolder\a\b\c\d\something.java and
what I get is C:\destinationfolder\something.java
I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
Tried it without any target in above
There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.
A. Yes I have done xcopy /? to see all options
B. /T also does not create any empty directory structure
C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>
UPDATE
I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.
Assuming your folder structure is like this:
SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST = C:\MyDestination
Run XCOPY like this:
XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E
Note the * in something*.java.
The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to
xcopy *.java C:\myfolder /s/y
it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:
xcopy something.jav* C:\myfolder /s/y
Edit
You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:
FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y
What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.
I'm assuming here that:
You can get the list of files in a text file, with only the file names (and optinally the paths)
You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.
You can also use the following form:
FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y
where cmd needs to be replace with the command you use to generate your list of files to copy.
Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).
I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:
xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y
Hope this helps!
In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.
You can use:
C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S
Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.
It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.
You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.
xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D
If something.java were the only file under C:\MyJavaStuff, you could also use
xcopy /sei c:\MyJavaStuff C:\destinationfolder
That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.
I have written a very similar batch file using xcopy. Perhaps what I did will help you.
This is the command I used:
xcopy "c:\Data Files\Dave's Data\*.*" "m:\Dave's Data" /R/D /E/H
In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP
I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.
The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.

Move all files except some (file pattern) from a DOS command

From a DOS command I want to move all files that do not match a file name pattern.
Something like this:
For example I want to move all files that do not start with "aaa"
for %i in (*) do if not %i == aaa* move %i .\..
XCOPY is designed to work with 'exclude' lists... See below:
dir /b /a-d "source"|findstr /b "aaa" >"%temp%\aaafiles.tmp"
xcopy "source" "destination\" /exclude:%temp%\aaafiles.tmp /y
The first line performs a DIR (directory) listing of the source folder, listing files in bare format (/b) ignoring directory names (/a-d). The output is piped into the FINDSTR command.
FINDSTR looks at each filename and compares it's beginning (/b) with the string "aaa".
If the start of a filename matches the string "aaa", the filename is redirected (written) to the file aaafiles.tmp in the users TEMP directory.
The /b is vital because you don't want to exclude files such as theaaafile.txt.
The XCOPY command copies files from the source folder to the destination folder except files that are listed in aaafiles.tmp.
Prompting to overwrite existing files (/y) is turned off.
source and destination will need to be replaced your own foldernames.
Robocopy is a possibility
robocopy source destination *.* /mov /XF aaa*.*
for options see here http://technet.microsoft.com/en-us/library/cc733145.aspx
If you don't mind fiddling with the archive bit, you can use it to selectively copy and delete files based on a file mask.
Move (copy and delete) all files except those beginning w/"aaa" from current directory to "dest". May also specify full source path.
attrib +a *.*
attrib -a aaa*.*
xcopy /a [or /m] *.* dest
del /aa /q *.*
One way you can do it is create a list of the files to move in a temporary file. Then use the file in with the for command. Generate the list using findstr.
> dir/b/a-d | findstr /v aaa* > "%temp%\#movelist"
> for /f %f in (%temp%\#movelist) do move %f ...
The first command gets a list of all files (with no directories) in the current directory and then pipes the list to findstr which excludes (/v) filenames that match the pattern and puts it in the file #movelist in the temp directory. The second command just takes those results so you may do what you will with them (move it).
There's probably a better way to do it in a single command without the temporary file, I just don't know how to write it. I'm not sure how to call the dir command from within the for command. AFAIK it only takes program files that exist, not builtin commands.
In some cases it can be made more simple. For example, I had to copy recursively a bunch of directories but excluding all images (png and bmp files), so I simply created an excludeList.txt file containing:
.png
.bmp
and run
xcopy /S /I <source> <dest> /exclude:c:\excludeList.txt
It will match any file or directory containing .png, but not necessarily ending by .png. (I did not investigate if smart use of wildcards or regular expressions are possible).
It does not handle your particular example (for which you have already a good answer) but it solved my problem, and this page is what I found when I googled in search of a solution :)
Not ideal, but moving all files to the destination and moving the files back to the source is a fast way with actual move operation (no copies). Of course this assumes there are no files in destination matching the wildcard.
move source\*.* destination\ && move destination\aaa*.* source\

Resources