-1

what im looking for is a .bat file code to zip files individually in all subfolders in the current folder and then delete the of files after, to exclude already zipped/compressed files, what i dont want is folders to be zipped and i want the files to keep there name when zipped

i have a bunch of folders/files and the only code i found

@ECHO OFF
FOR %%i IN (*.*) DO (
ECHO "%%i" | FIND /I "batch zip files.bat" 1>NUL) || (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
if %ERRORLEVEL% == 0 del "%%i"
)
)

zips all files in the current directory and doesnt touch subfolders

i'd appreciate it if anyone can do this for me as i can save a ton of space with all files zipped

  • 1
    Does this answer your question? [How can you zip or unzip from the script using ONLY Windows' built-in capabilities?](https://stackoverflow.com/questions/17546016/how-can-you-zip-or-unzip-from-the-script-using-only-windows-built-in-capabiliti) – Toni Feb 07 '21 at 02:24
  • Try reading help. EG `-r` and `sdel`. http://sevenzip.osdn.jp/chm/cmdline/switches/index.html. – user14797724 Feb 07 '21 at 02:24
  • these solutions would take me along time to compress all my files as i have 10,000+ files and 1,000s of folders – dominater01 Feb 07 '21 at 05:08
  • Given your information in the comment above, and your suggestion that you'll be saving a lot of space, perhaps you should be looking at NTFS compression instead. Please open a Command Prompt window, type `compact /?` and press the `[ENTER]` key to read its usage information. – Compo Feb 07 '21 at 06:28
  • This question appears to have been closed as a duplicate of unzipping files using built-in Windows capabilities, however the question is clearly using a the specific third party utiliy `7z.exe`. No suggestion was made by the OP to do it using anything other than 7z, and their comment above even seems to imply they don't want that either. In addition the OP has accepted an answer which uses 7z too, which is further validation that there was no requirement to perform the task using built-in Windows capabilities/tools. I have for those reasons reopened the question. – Compo Feb 07 '21 at 15:51
  • check this-> https://stackoverflow.com/questions/28043589/how-can-i-compress-zip-and-uncompress-unzip-files-and-folders-with-bat – npocmaka Feb 07 '21 at 15:58

1 Answers1

0

The first issue you have with your provided code is that your For loop is only parsing files in the current directory, there is no recursion into subdirectories. To parse files within the subdirectories, I'd advise that you use a For /F loop, with the Dir command using its /B and /S options. I would also advise that you include the attribute option, /A, which will include every item, then omit those which you're not interested in. For instance, it's unlikely that you want to zip the directories, hidden files, reparse points, or system files. You can do that by excluding those attributes, /A:-D-H-L-S. To learn more about the For command, and the Dir command, open a Command Prompt window, type for /?, and press the ENTER key. You can then do the same for the Dir command, i.e for /?. As you have not defined a working directory at the start of your script, it will run against every file and directory in whatever is current at the time you run it. Because your code has a line excluding a file named batch zip files.bat, I'm going to assume that is the name of your running script, and that your intention is to therefore run the script against everything in the tree rooted from the same location as the batch file itself. To ensure that is always the case, for safety, I've defined that directory as the current directory from the outset, using the CD command, CD /D "%~dp0". %0 is a special batch file argument reference to itself, to learn more about this please take a look at the output from both call /?. You can also learn about the CD command entering cd /?, in a Command Prompt window too. To also omit your batch file, as you don't want it to be zipped and deleted, I've piped the results from the Dir command through FindStr, printing only items which do not exactly match the case insensitive literal string %~f0 (expanding to the full path of the batch file itself). Additionally, I've piped those results through another findstr.exe command to omit any files already carrying a .zip extension, as there's no point in zipping files which already zip files. (Please note however, that for more robust code, you should really check that those are zip archives and not just files carrying a misleading extension). The results from those commands are then passed one by one to the Do portion which includes your 7z.exe command. I've assumed at this stage, that your intention was to save the zipped archives to the same location as the originating files. To do that I've used variable expansion on %%G to stipulate its directory, path, and name, %%~dpnG, (see the usage information under for /? to recap). Upon successful completion of the archiving process, the original file will be deleted, to do that I appended the -sdel option to your original command string. Please be aware that you may want to include additional options, should you wish to update existing zip files etc. (use "%ProgramFiles%\7-Zip\7z.exe" -? in a Command Prompt window to see them). As I've not mentioned it previously, at the beginning of the script, I made sure that extensions were enabled. Whilst it is the default option, it's safer to be sure, as variable expansion and the commands CD, and For can be affected, if they're not.

Here's the code as explained above:

@Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:-D-H-L-S /B /S 2^> NUL ^|
 %SystemRoot%\System32\findstr.exe /I /L /V /X "%~f0" ^|
 %SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnG.zip" "%%G" -sdel

Looking at your question, which has changed from what you'd asked initially, you appear to not be interested in the files of the batch file directory any more, "zip files individually in all subfolders in the current folder". For that reason, I've provided the following alternative, methodology.

The difference is that I first of all use a For loop to include only directories in the current working location, /A:D-H-L-S, before running the same method used in my previous example, but with one difference. As we're now no longer zipping files in the current working directory, we can remove the findstr.exe command filtering out the running batch file:

@Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:D-H-L-S /B 2^> NUL'
) Do For /F "EOL=? Delims=" %%H In ('Dir "%%G" /A:-D-H-L-S /B /S 2^> NUL ^|
 %SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnH.zip" "%%H" -sdel

Please be aware, that my answers above are to essentially correct your code attempt, and not a personal recommendation for speed, or in performing the task laid out in your question. Additionally, I have no idea what will happen if any of those files are in use/locked, and have made no attempt at checking for such scenarios.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • and what would i have to do to unzip all again just to cover all my bases – dominater01 Feb 08 '21 at 08:54
  • That is another question, this site answers a single, specific, and reproducible, issue with your provided code. The code I've provided, and the explanatory text I included along side it, should be more than adequate for you to write your own code. If when you've written it, it fails to achieve the intended task, and after debugging it you have a particular issue which needs addressing, feel free to start another question topic. Also in order to unzip all of the files you've just zipped, you'd need to have a log of either their names, or possibly the approximate timestamp for that zipping run. – Compo Feb 08 '21 at 10:15
  • The reason for that, @dominater01, is your code is, as asked, ignoring files already with the `.zip` extension. You cannot therefore unzip all `.zip` files, because that would include those which were not part of the just zipped group. Please note also that you did not indicate what types of files you were zipping, which means that many filetypes which are already compressed, including many images, office documents, and archives without the .zip extension, when compressed, will not increase your storage capacity very much, if at all. – Compo Feb 08 '21 at 10:21
  • ah well i just thought i should have a bat file extract all zips would help me when sorting duplicates since i add new files and have to extract manually everytime, im compressing rom files so no images or anything just 1 file per game – dominater01 Feb 10 '21 at 10:45