Advertisement
BrainWaveCC

ZipByName.BAT

Feb 3rd, 2025 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.83 KB | Source Code | 0 0
  1. @REM - ZipByName.BAT (03 Feb 2025 // 03 Feb 2025): Create zip folders based on the name of the files found
  2. @REM - Original Message: https://www.reddit.com/r/Batch/comments/1ih4v5s/batch_creating_multiple_zip_folders_based_on/
  3. @ECHO OFF
  4.  
  5.  :::  This script will search the specified source folder for files to zip up by
  6. :::  name.  It will create the zip archives in a subfolder to the source, using
  7. :::  the native tar command in Windows 10 and 11.
  8. :::
  9. :::  You can run it at the command-line or directly in explorer:
  10. :::    ZipByName.BAT
  11. :::
  12. ::: -----------------------------------------------
  13. :::  This script can be found in the following locations:
  14. :::      https://pastebin.com/SPNKHq5z
  15. :::      https://github.com/BrainWaveCC/MiscWinScripts/blob/main/ZipByName.BAT
  16. :::
  17. :::
  18. :::  NOTE: I like prefacing my variables with # or @ because then it makes it less
  19. :::        likely that they will clash with anything else, and a simple command like
  20. :::        "SET @" or "SET #" will enumerate all my variables at one time.
  21. :::
  22. :::  Tested on Windows 10 x64 and Windows 11
  23. :::
  24.  
  25. :Variables -- v1.0.0
  26.  SETLOCAL
  27.  SET "#SOURCE=C:\Temp\Other"
  28.  SET "#DEST=%#SOURCE%\Zips"
  29.  SET "#SKIP=--exclude *.ZIP --exclude *.GZ --exclude *.TAR --exclude *.7Z"
  30.  
  31.  rem -- Search specified folder (but not subfolders) for files to zip, but skip all zip-type archives (*.zip, *.gz, *.tar, *.7z, etc)
  32. :Main
  33.  MD "%#DEST%" >NUL 2>&1
  34.  PUSHD "%#SOURCE%
  35.  FOR %%F IN ("%#SOURCE%\*.*") DO IF NOT DEFINED $%%~nF CALL :ZipMyFiles "%%~nF"
  36.  
  37. :ExitBatch
  38.  POPD
  39.  ECHO:
  40.  DIR /OGD "%#DEST%"
  41.  ENDLOCAL
  42.  EXIT /B
  43.  
  44. :ZipMyFiles -- Zip files by name, except zip/archival type files (%1 = current file group to zip)
  45.  SET "#ZIP=%#DEST%\%~1.zip"
  46.  
  47.  ECHO: & ECHO Creating "%#ZIP%" ...
  48.  tar -cf "%#ZIP%" -v %#SKIP% "%~1.*" && SET "$%~1=TRUE"
  49.  GOTO :EOF
  50.    
Tags: Reddit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement