I'm new to Windows batch files, but I'm writing a .bat file that just copies a bunch of files from one place to another maintaining the file directory structure. Using xcopy this is simple but I need to exclude some files from being copied. You can use /exclude and create a text file full of strings that you want to be excluded, but this doesn't just exclude files with the exact names in the text file, it excludes all files whose filenames contain any of the strings in the text file.
What this means is, if I want to exclude any files named 123.txt and put this string in my exclusions text file, if there was a file called 1123.txt anywhere in the source folder or any of its subfolders that would also be excluded.
How can I exclude only files with a specific filename from being copied?
Evening Bill.
Can you add a slash before each file name? That should work
EG
instead of
123.txt
blah.txt
use
\123.txt
\blah.txt
Try creating a temporary folder, xcopying all of the files into that folder, deleting the ones you don't want, then xcopying those to the final destination. Finally, delete the temporary folder and its contents with rd xyzzy /q/s
Related
I have 2 identical folder trees, let's call them C:\First and C:\Second, with many subfolders. C:\First has many XML files in many sub and sub-sub folders. C:\Second just has the folder tree created.
I want to go through all XML files in C:\First\* and put it in it's equivalent place in C:\Second.
But first I want to check if a file with the same name exists in the C:\Current folder (no subfolders there), in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second.
In other words, I want to copy the whole structure and files from C:\First to C:\Second but I want to take the latest version that may or may not exist in C:\Current. And, in C:\Current there are many files I don't care about.
Example:
C:\Current has these files:
a.xml
b.xml
1.xml
c.xml
d.xml
e.xml
2.xml
f.xml
g.xml
3.xml
In C:\First I have a.xml, b.xml, c.xml, d.xml, e.xml, f.xml, g.xml spread out in its sub-folders.
I hope I'm not being too confusing...
When a problem is properly stated, the same problem description may serve as specifications to write the program. In your case, you have this description:
I want to go through all XML files in C:\First* and put it in it's
equivalent place in C:\Second.
But first I want to check if a file with the same name exists in the
C:\Current folder
The "But first" doesn't serve to write a program. You just need to write the first things in first place! For example:
I want to go through all XML files in C:\First*. I want to check if a
file with the same name exists in the C:\Current folder in which case
I will copy the one from C:\Current to the proper sub-sub-sub folder
in C:\Second. Otherwise put the file from C:\First in it's equivalent
place in C:\Second.
The pseudo-code below is an example of how your original problem description may be translated into a program:
rem I have 2 identical folder trees, let's call them C:\First and C:\Second, with many subfolders.
rem C:\First has many XML files in many sub and sub-sub folders. C:\Second just has the folder tree created.
rem I want to go through all XML files in C:\First\*
for /R inside C:\First with all *.XML files do (
rem But first I want to check if a file with the same name exists in the C:\Current folder (no subfolders there)
if exist "C:\Current folder\place here just the name of the file from the for command" (
rem in which case I will copy the one from C:\Current to the proper sub-sub-sub folder in C:\Second.
set properFolder=place here the sub-sub-sub folder from the for command
set properFolder=change "C:\First" by "C:\Second" in properFolder
copy "C:\Current folder\just the name" "!properFolder!"
) else (
rem ... and put it in it's equivalent place in C:\Second.
set properFolder=place here the sub-sub-sub folder from the for command
set properFolder=change "C:\First" by "C:\Second" in properFolder
copy "the file from for command" "!properFolder!"
)
)
If you analyze this code you will realize that the lines that get properFolder are the same in both parts, so a simpler method would be to get properFolder just one time before the if command.
You may use this pseudo-code as starting point to write your Batch file.
Hi, I have many zip files located at g:\toto. These zips contain some files. I would like to extract all zip in a same directory (g:\toto\extracted) then rename various files of the zip.
Example 1 :
www_12567.vp.zip : 3 files : alpha.doc, beta.xls, teta.doc
I would like after extraction, files are renamed with the name of the zip
www_12567.vp.alpha.doc, www_12567.vp.beta.xls, www_12567.vp.teta.doc
Example 2 :
www_12.vp.zip : 3 files : al.doc, bea.xls, tta.doc
www_12.vp.al.doc, www_12.vp.bea.xls, www_12.vp.tta.doc
I found this question, but it talks about .txt and the zip contain one file, so, it doesn't work.
Without knowing the contents of the archive you can't know which files to rename, because you are putting them into a directory that may already contain other files.
This, however, would be much easier if there was a dedicated directory to put the files temporarily. Here's how you could use it:
#ECHO OFF
SET "srcdir=G:\toto"
SET "tgtdir=G:\toto\extracted"
SET "tmpdir=G:\toto\extracted-tmp"
FOR %%Z IN ("%srcdir%\*.zip") DO (
unpack "%%Z" with your favourite tool into "%tmpdir%"
FOR %%I IN ("%tmpdir%\*") DO MOVE "%%I" "%tgtdir%\%%~nZ.%%~nxI"
)
Of course, the temporary directory would need to be empty before running the batch file. You could add DEL "%tmpdir%\*" somewhere before the loop to make sure it is.
One other note is, the above assumes that the archives do not contain subdirectories or, at least, that the files are extracted without subdirectories.
UPDATE
If you are using the 7-Zip archiver to work with .zip files, then this is how your extract command might look:
7z e "%%Z" -o"%tmpdir%"
Disclaimer: I'm not an active user of 7-Zip. This is what I used as a reference to come up with the above command:
7-Zip Command-Line Examples
would anyone know how to write a batch file (or two) that would:
copy hundreds of "index.html" files that are in different folders and keep within the same folder (so needs to search within sub folders)
rename the copies (keep the originals as is) to "index.jpg"
could anyone help?
I used a bulk rename tool to rename all index.html files to index.jpg in a copy of the main folder, then used robocopy to copy everything over, including the file structure back to the original folder (leaving the index.html's alone)
I want to move a few files from a folder to another folder, these few files have the same prefix in their filename, for example, myFile789789, myFile789798, myFile6787678, etc.
How do I target these files.
Just all files starting with that name?
MOVE myFile* "c:\somewhere"
move path1\myFile789789*.* path2
should do the trick.
I'm trying to copy a number of files from a directory. I want to include the file path from the base of this particular directory tree, however, I only have a list of the file names to go by. Is there a way to copy either:
a list of files with their directories appended to the beginning in a .txt file
a copy of the folders in the directory with copies of the files placed in their original places in the original directory.
Each file in the directory has a unique name.
I've looked all over google, but the closest I've found is xcopy, which I don't believe is capable of this.
Thanks!
For the second option you can use xcopy /s or robocopy /s. Both are great tools for this kind of job.