Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to zip some files with an unknown file type individually. I am using the following code in a batch script to do that:

@ECHO OFF 
FOR %%A IN (bestbuy*nat*component.cpi*) DO "C:\Program Files\7-Zip\7z.exe" a -mx9 -m0=lzma2:d256m "%%~nA.7z" "%%A"

The code will compress files individually ONLY if the file has an extension. Unfortunately the files that I have don't have any extension. In the code I am trying to zip files by doing a pattern match, the files are getting compressed into ONE file (which I do not want, I want each file compressed individually).

Why does this code create separate zip files when the files have an extension (for example if I add .txt to the end of the files) and when there is no extension the code creates one zipped file.

Can anyone please help me with the code to compress files with unknown file type so that each file gets compressed individually

Your help would be greatly appreciated.

share|improve this question
1  
What is you output if you just run: FOR %%A IN (bestbuy*nat*component.cpi*) DO echo."%%~nA.7z" Just want to make sure that the ~n file name parser is correctly parsing the file names as unique. –  David Ruhmann Dec 20 '12 at 21:52
    
What could help is to ommit the @ECHO OFF from the bat file and see what the output is. What version of Windows are your running? –  hot2use Dec 21 '12 at 6:54
add comment

1 Answer

You might have been misled by the 7z help file (alternative URL) where he writes

7-Zip doesn't uses the system wildcard parser. 7-Zip doesn't follow the archaic rule by which *.* means any file. 7-Zip treats *.* as matching the name of any file that has an extension. To process all files, you must use a * wildcard.

At this point in your batch file where you are providing the wildcard parameters 7z.exe is not involved. On a Windows system the command line should read:

FOR %%A IN (bestbuy*nat*component*)  DO "C:\Program Files\7-Zip\7z.exe" a –mx9   –m0=lzma:d=256m "%%~nA.7z" "%%A"

I have changed the –m0 parameter as it does not match my specifications in the version I am running. After these slight modifications it works fine for me.

Update:

Here is the output from my test environment:

C:\Temp> 7zip.bat
C:\Temp> FOR %A IN (7*test*) DO "C:\Program Files\7-Zip\7z.exe" a -mx9 -m0=lzma:d=256m %~nA.7z %A
C:\Temp> "C:\Program Files\7-Zip\7z.exe" a -mx9 -m0=lzma:d=256m 7ziptest_1.7z 7ziptest_1.txt
7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning
Creating archive 7ziptest_1.7z
Compressing  7ziptest_1.txt
Everything is Ok
C:\Temp> "C:\Program Files\7-Zip\7z.exe" a -mx9 -m0=lzma:d=256m 7ziptest_2.7z 7ziptest_2.txt
7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning
Creating archive 7ziptest_2.7z
Compressing  7ziptest_2.txt
Everything is Ok
C:\Temp> "C:\Program Files\7-Zip\7z.exe" a -mx9 -m0=lzma:d=256m 7ziptest_3.7z 7ziptest_3
7-Zip [64] 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning
Creating archive 7ziptest_3.7z
Compressing  7ziptest_3
Everything is Ok
share|improve this answer
    
Thank you for your help but i still have the same issue even with your code. I changed the code as you suggested but it still zips those files into one file instead of creating a separate zip file for each file. FOR %%A IN (bestbuy*nat*component*) DO "C:\Program Files\7-Zip\7z.exe" a -mx9 -m0=lzma:d=256m %%~nA.7z %%A Kind Help –  Jason Mander Dec 20 '12 at 21:10
1  
Doesn’t he need to keep the %%A in quotes in case he has a file with space(s) in its name? –  Scott Dec 20 '12 at 22:56
    
Ok. That's true with the quotes for files with spaces. Point taken. I think a list of files from @JasonMander's directory would help solve the problem. As I stated: The code works for me with my test files. –  hot2use Dec 21 '12 at 6:50
    
File name: bestbuy_abocom_20121002071039_nat_component.cpi-121002124501_653343675_ff File Extension: cpi-121002124501_653343675_ff File name: bestbuy_casemate_metrics_request_523_20121219_16-52.txt-20121219165227 File Extension: txt-20121219165227 File Name: bestbuy_huntkey_20121002071039_nat_component File Extension: File. Above are the three types of files that i want to compress individually using a batch script. Thanks –  Jason Mander Dec 26 '12 at 17:31
    
Thanks for the file list. Looking at this list and comparing it to the search string, this would mean that only the first file would be zipped. The second file doesn't match nat*component and the third file doesn't match component.cpi in the search string. –  hot2use Jan 4 '13 at 6:55
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.