Batch script to zip like files using wildcard

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AlaskanFive
Posts: 4
Joined: 22 Aug 2014 08:17

Batch script to zip like files using wildcard

#1 Post by AlaskanFive » 22 Aug 2014 08:25

I'm new to the forum, I hope to learn a great deal from this community.

I have a folder with 600 text files, I'm looking to zip together files with similar identifiers. So for instance I have 10 files with id001~ as the identifier, those 10 will be zipped in the same directory and so forth. So far I have a code that will zip 1 file when you hard code the file name. I'm not sure if this should read off a list or maybe specify some type of wildcard criteria for which files to zip

Any help is greatly appreciated.

Code: Select all

echo off
set archievePatch=id001~testfile3.txt
7z.exe a %archievepatch%.zip "%archievePatch%"

PATH :\Users\Jeffery\Desktop\TestFolder
pause
exit


This will output a zip file named: id001~testfile3.zip

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to zip like files using wildcard

#2 Post by foxidrive » 22 Aug 2014 08:30

Does every filename start with the identifier and have a tilda character at the end of the identifier?

Are all files in a single folder?

AlaskanFive
Posts: 4
Joined: 22 Aug 2014 08:17

Re: Batch script to zip like files using wildcard

#3 Post by AlaskanFive » 22 Aug 2014 08:49

Yes every text file will start with the identifier and tilda character at the end and all files are in the same folder.
Example:
id001~testfilebasketball.txt
id001~testfilebaseball.txt
id001~testfilehockey.txt
id001~testfilesoccer.txt
id001~testfilefootball.txt

id002~testfilebasketball.txt
id002~testfilebaseball.txt
id002~testfilehockey.txt
id002~testfilesoccer.txt
id002~testfilefootball.txt

id005~testfilebasketball.txt
id005~testfilebaseball.txt
id005~testfilehockey.txt
id005~testfilesoccer.txt
id005~testfilefootball.txt

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch script to zip like files using wildcard

#4 Post by Squashman » 22 Aug 2014 09:02

What should be the output file name for each of those 3 groups?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to zip like files using wildcard

#5 Post by foxidrive » 22 Aug 2014 09:22

I'm guessing the indentifier is the zip filename.

Test this:

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *~* /b /a-d ') do (
   for /f "delims=~" %%b in ("%%a") do (
     if not exist "%%b~.zip" 7z.exe a "%%b~.zip" "%%b~*"
   )
)
pause

AlaskanFive
Posts: 4
Joined: 22 Aug 2014 08:17

Re: Batch script to zip like files using wildcard

#6 Post by AlaskanFive » 22 Aug 2014 09:36

Thank you Foxidrive the code worked!

The output should be the identifier, name and year month so
Example:
id001~Sports_YYYYMM
id001~Sports_201408

AlaskanFive
Posts: 4
Joined: 22 Aug 2014 08:17

Re: Batch script to zip like files using wildcard

#7 Post by AlaskanFive » 26 Aug 2014 05:41

8) Thanks again. Here's the finish code if someone may need it.

Code: Select all

@echo off
set datef=%date:~10,4%%date:~4,2%

for /f "delims=" %%a in ('dir *~* /b /a-d ') do (
   for /f "delims=~" %%b in ("%%a") do (
     if not exist "ZipFiles\%%b~Sports_%datef%.zip" 7z.exe a "ZipFiles\%%b~Sports_%datef%.zip" "%%b~*"
     )
  )
 pausencase

Post Reply