This question already has answers here:
How can I compress (/ zip ) and uncompress (/ unzip ) files and folders with batch file without using any external tools?
(6 answers)
Closed 4 years ago.
I would like to use a .bat file to unzip a ZIP compressed archive file if possible at all. Nothing fancy, I just want to extract the entire archive file to the same location, i.e. download a .zip file to desktop and want to extract it next to desktop with the same name.
I tried this, but with no success.
for /R "C:\Users\Desktop\test.zip" %%I in ("*.zip") do(
"%ProgramFiles(x86)%\7-zip\7z.exe" x - y -o"%%~dpnI" "%%~fI"
)
exit
You shouldn't need the loop. Depending on whether you want to extract the directory structure contained within the archive or just extract everything to a single directory, you would use:
7z e C:\Users\Desktop\test.zip -o C:\Users\Desktop\test
or
7z x C:\Users\Desktop\test.zip -o C:\Users\Desktop\test
See https://sevenzip.osdn.jp/chm/cmdline/commands/index.htm for a list of commands and drill down as needed for the various options.
You should not need a for loop in your batch file, unless you intend to only extract files based on a list of patterns.
Related
This question already has answers here:
Delete file with specific extension in batch file
(3 answers)
Closed 2 years ago.
I have .blk and .blkx files in the same folders, of wich im trying to delete only the .blk file. Ive attempted examples found on this post as in del "\folder\*.blk" /s. I managed to get the chosen files deleted however the .blkx files were deleted too (other files werent so i assume its an issue with both the file having .blk in their extension).
How can i select only the .blk file?
Edit: I dont know why this post was marked a duplicate but the above pinned post does not resolve the problem (wich i already talked about not working)
You must have SFNs (short filenames) enabled on that drive, and the .blkx extension is mapped to .blk in the short 8.3 filename. Because of this, and because wildcards match both long and short names, *.blk returns both .blk and blkx files. To distinguish between them you'll need to run a for loop, check the actual extension, and delete each .blk individually.
for %%b in (*.blk) do #(if /I "%%~xb" == ".blk" echo del "%%~b")
Remove echo from the above to actually delete the .blk files.
To recurse into subdirectories (like del /s) use for /r instead of for.
The command is written as to be used in a batch file (per the question tag). To run it at a command prompt, instead, replace the double percents %%b with single ones %b.
This question already has answers here:
Batch command to copy files from source to destination [duplicate]
(1 answer)
Copy Contents From Folder Using Wildcard On The Directory
(1 answer)
xcopy wildcard source folder name to destination
(2 answers)
Using a batch file to copy files with a wildcard in the directory path?
(2 answers)
Closed 3 years ago.
I'm trying to write a batch-file to uninstall GoToMeeting.
Currently I use the following to uninstall the current version:
echo UnInstalling GoToMeeting........
"C:\Program Files (x86)\GoToMeeting\13190\G2MUninstall.exe" /uninstall -silent
The problem is, with each install of a new version, the install directory changes. For example, the version is 13190 but the previous time it was 13022 and 12771 before that. So I practically have to add a line for each version and that is really annoying.
Is there a way using a batch-file to make the directory a wild card during the uninstall?
sorry, no wildcard allowed unless in the very last element of a path or filename.
Instead use a for /d loop to list all subdirectories of a certain directory:
for /d %%D in ("C:\Program Files (x86)\GoToMeeting\*") do (
echo the file you look for is "%%D\G2MUninstall.exe"
)
Bonus: if several versions are installed, this finds all of them
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.
This question already has an answer here:
Batch file creating another batch file, how to ignore commands when writing lines?
(1 answer)
Closed 7 years ago.
So, on school we need to make this assignment.
So we tried to make a bat file that make's some folders and dox.
but the problem is: we want to let the bat file make another bat file.(with commands in it)
but if we write it like:
echo. #echo off F: tree/f>menu.bat
this creates a .bat file that doesn't work. probebly cause we didnt add lines. so how to do this?
how do we fix this?
looking forward to it.
You can use the >> operator to add lines to existing files. One > by itself will create a new file (or overwrite the contents of an existing file).
echo #echo off >menu.bat
echo F: >>menu.bat
echo tree /f >>menu.bat
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