I am trying to find a way to search a directory for files by name, then copy the found files to an output directory. Because the files will have the same file name, I'm wondering if I can copy the folder structure as well, or alternatively append the filename with its parent folder name (i.e. folder\file.txt becomes folder-file.txt)
The search I'm using is a basic dir call:
dir file.xml /s
Can I do what I'm trying with only cmd? I would be ok using this in a batch script as well, though I believe the syntax is the same.
I would probably do this with robocopy (built in to newer versions of Windows). Something like this should do the trick...
robocopy.exe /S C:\FromDir C:\ToDir file.xml
This will copy the file.xml files that match on c: and keep the folder structure intact, to d:\results
xcopy "c:\file.xml" "d:\results\" /s
Related
I am trying to copy pdf files which are named with KBSMPP*.pdf, just like in Linux. The file also could have more strings, but it is stil a pdf, e.g. KBSMPP_____.pdf
I have many Files, the files are named:
KBSMPP1.pdf
KBSMPP345.pdf
KBSMPPKL.pdf
I don't know how it works. How can I search specific name files?
My code in Batch is:
xcopy "C:\Users\Manfred\Documents\KBSMPP*.pdf" "H:\Users\Hendrik\Documents\Frames"
Another example which I have tried:
ROBOCOPY "C:\Users\Manfred\Documents\KBSMPP*.*pdf" H:\Users\Hendrik\Documents\Frames "KBSMPP*.pdf"
There are several methods.
Using Copy:
Copy "C:\Users\Manfred\Documents\KBSMPP*.pdf" "H:\Users\Hendrik\Documents\Frames\"
Using Robocopy:
ROBOCOPY "C:\Users\Manfred\Documents" "H:\Users\Hendrik\Documents\Frames" KBSMPP*.pdf /MT:32 /B
Using a FOR Loop you can paste into a cmd prompt:
FOR %A IN ( "C:\Users\Manfred\Documents\KBSMPP*.pdf") DO (
COPY "%fa" "H:\Users\Hendrik\Documents\Frames\" )
Any way when copying files via Xcopy (via batch) to output the filenames of the files being copied - or better yet, the base folder and filename - rather than the whole directory path?
The batch uses %USERPROFILE%\Desktop which resolves to C:\Documents and Settings\Username\ and then Desktop. Then that's where the folder lives with the files and directories to be copied.
Of course when it copies across you see C:\Documents and Settings\Username\Desktop\TheFolder\SubFolder\filename.txt in the output window, which takes up 2 lines per copy and just looks terrible when you are trying to see where its up to.
All I want to see is SubFolder\filename.txt
Use cd /folder to change to the source folder (or one above it), and in the xcopy statement, don't include the entire path.
For example, instead of this...
xcopy c:\tmp\q\z\*.* c:\destfolder
...do this:
cd \tmp\q
xcopy z\*.* c:\destfolder
And to copy from the desktop:
cd %userprofile%
xcopy desktop\*.* c:\destfolder
I am trying to create a batch file that searches a folder for zipped files, and unzip them. I want the script to search through all the subfolders in the main folder, and unzip everything it finds. It might be a main folder with several subfolders. Some of the subfolders will contain zipped files, but some will not.
The zipped files will look like "filename.r00", "filename.r01", "filename.r02", and so on. One file will have the name "filename.rar", and it is this file that will need to extracted using 7-zip.
Is it possible to write a batch file that does this, and then deletes all the zip files? I have already installed 7-zip, so if it is possible I would like to use it. If anyone could help me write the batch file, it would be much appreciated!
Thanks!
Here you go
for /r C:\Mainfolder %%a in (filename.r*) do (
7z e %%a -o%%a_Extracted
del %%a /f /q
)
After looking at the switches for 7-Zip this might be faster (untested)
7z x filename.r* -o*_Extracted -r
del filename.r* /f /q
Either way they are extracted to a folder with _Extracted at the end, otherwise it will extract them to a folder with the same name as the archive, and when it deletes the files, it may try and delete the folders too.
I have a folder containing many other sub-folders. I am trying to write a batch file which will copy some of the folders to another place on my hard disk. I am using xcopy for this. The folder structure is as shown below:
FolderB1
FolderB2
FolderB22
File1.txt
File2.txt
File3.txt
I have some .txt files inside FolderB1, along with FolderB2 and FolderB22. I want to copy FolderB2 and FolderB22 and skip .txt files contained in Folder B1
I tried using /EXCLUDE: param of xcopy command, but it is not able to perform this operation. It does not work if I specify the exclusion as \FolderB1\*.txt or something of this sort.
The number of main folders is not known. It can be anything. Also, there is no fix pattern for names of .txt files. Have checked this question too, but did not help.
Also, I want to avoid using del command, since copying all and deleting again would consume time.
Can this be achieved using Robocopy? Exactly similar question is raised here.
Any pointers would be useful. Thanks in advance.
To clarify, I asume you mean to exclude all .txt files in FolderB1, but not exclude .txt located elsewhere.
You can do that with robocopy in two steps. First copy all files except any .txt. And in the second step copy only .txt files, but exclude FolderB1.
robocopy c:\source c:\destination /s /xf *.txt
robocopy c:\source c:\destination *.txt /s /xd c:\source\FolderB1
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.