Command Prompt and batch files - batch-file

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.

Related

Check if file exists in 2 directories, delete files that aren't in both directories

So, I'm programming a game, but the compiler I use is written as a Windows batch file. I'm using Windows 10 as my operating system.
In my game files, I have one folder with images, and another folder with upscaled versions of those images that have the same file name and extension.
What I want to do is have the batch file go through all the images in the directory with the upscaled images, and check if a file with the same name and extension exists in the directory with the original images. If it doesn't exist in the original directory, it will delete it from the upscaled directory.
I got it working using an answer over here:
Batch Extract path and filename from a variable
All I had to do was extract the file name and extension, and then run all the files in one folder through the loop, checking if the file exists in the other folder.
Here is my code:
for %%i in (%folder1%\*.png) DO (
if not exist "%folder2%\%%~nxi" ECHO %%~nxi
)
If you actually want to delete the files, change ECHO to del /q, be warned you will lose files, so make sure you have backups.

Post Build event xcopy - exclude some set of files

I want to copy some of the files to the specific folder after the successful compilation of the project. I have written a post build event as mentioned below:
xcopy "$(ProjectDir)bin" "$(TargetDir)..\..\Support Files\DBUpgradeUtility\" /Y
Note: the output path of my project has been set to bin folder for debug and release both the mode.
The above mentioned build event worked fine and all the files present under bin folder has been copied to destination folder. But along with the required files, the ‘vshost.exe’ files also copied, I don’t’ want this file. So, I have used the exclude parameter of xcopy build event as mentioned below:
xcopy "$(ProjectDir)bin" "$(TargetDir)..\..\Support Files\DBUpgradeUtility\" /Y /exclude:$(TargetDir)..\..\Support Files\DBUpgradeUtility\*.vshost.exe
With the above build event, the compilation failed and the error was:
The command "xcopy "C:\TFSWorkspace\FASTER.Web -
v6.3.Sprint.06\Source\Installer\Application\DBUpgradeUtility\bin"
"C:\TFSWorkspace\FASTER.Web -
v6.3.Sprint.06\Source\Installer\Application\DBUpgradeUtility\bin....\Support
Files\DBUpgradeUtility\" /Y /exclude:"C:\TFSWorkspace\FASTER.Web -
v6.3.Sprint.06\Source\Installer\Application\DBUpgradeUtility\bin....\Support
Files\DBUpgradeUtility*. vshost.exe" exited with code 4.
I have also googled for exclude parameter and then written the build event mentioned above. I cannot find what I am missing here or what I did wrong.
Please help me on this.
Thank you.
The /exclude option of the xcopy command works differently - it allows you to specify files which contain exclude filters:
Specifies a list of files. At least one file must be specified. Each
file will contain search strings with each string on a separate line
in the file.
When any of the strings match any part of the absolute
path of the file to be copied, that file will be excuded from being
copied. For example, specifying the string, \obj\ or .obj will exclude
all files underneath the directory obj or all files with the .obj
extension.
Therefore you may create a new file in your project (for example $(ProjectDir)excludes.txt) and add this line:
vshost.exe
Then change the option in your xcopy command to:
/exclude:"$(ProjectDir)excludes.txt"
This excludes all files containing vshost.exe in their absolute path. If you have to exclude other files, just add a new line to the file.

Batch - Extract many zip, rename files extracted in a same directory

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

How to get files in a directory including all subdirectories files?

I need to get all files that are inside a dir including files that are inside all dirs.
I found this post that gets all files of the dir, but it is for the current level files and don't goes any deeper.
Maybe there is already a built-in function to do this?
To list all files in the current directory and sub-directories, use:
dir /s

How to exclude a specific file from being copied using xcopy

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

Resources