How to extract only 1st file in list of archives

Infina

Weaksauce
Joined
May 23, 2005
Messages
83
Hey guys,

I was wondering if you could help me out with something. I'm trying to extract the first file in a series of 7z archives. Because of the different naming conventions of the files, I can't simply say to extract only files that are named "this". Is there any way using the 7zip command line or maybe even a batch script using a loop to extract just the first file of each archive, no matter what the name is? I'd really appreciate any help

Thanks alot
 
Nope, no way in 7Z or batch. You'd have to write a script in a better language, like Perl or vbscript; or a real program in C or C# or whatever.
 
@kingkaeru - no, not really a pattern

@mikeblas - could you help me out with a higher level script? :)
 
If you do something like the following, you'll get the filename of the first file in the archive at the end of the first line in firstfile.txt

Code:
7z.exe l archive.7z | find /I "...a" > temp.txt && [url=http://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file/130298#130298]head.bat[/url] 1 temp.txt > firstfile.txt

That'd be a dirty way to get part of the way there.
 
I guess the first question I'd ask is: how do you identify which file is "first" on the list? By name? Create date? size? type?
 
Looks like Shadow's got you covered. That's a pretty crafty use of for().
 
What I'm trying to do is:
I have a large collection of comics in .cbr format. I would like to extract just the cover pages (the first file) from the archives. Alot of the comics have different naming conventions for the pages, e.g. "pg0.jpg" for the cover, or "pg1.jpg" or "cvr.jpg". I wish it could be as easy as saying extract everything that is named pg1.jpg, so that's why I started the thread. Typically the first file in the archive is determined by its name.

Shadow2531: What is the usage of head.bat? Is that a different batch your command links to?
 
Shadow2531: What is the usage of head.bat? Is that a different batch your command links to?

head.bat reads N lines of a file and prints them out. So, if you want the first line of a file printed out, you'd do: "head.bat 1 foo.ext"

"7z.exe l archive.7z" makes 7z.exe list the contents of the .7z archive. You take that and pipe it through 'find' to filter things so that it only prints lines that have "...a" in them (7z.exe uses ...a to signify a file as opposed to a directory). Then, you redirect the output of the command to temp.txt, which will then contain a list of files in the archive with one entry per line.

Then, you can use head.bat to read the first line in temp.txt. Then, you can redirect the output to firstfile.txt so that firstfile.txt contains the first file entry.

From there, you'd need to grab the first line of firstfile.txt and get the substring that contains the filename. (I'm thinking the first " " from the end of the string to the end of the string would get you the filename).

From there, you'd have to use a 7z command to extract the specific file. But, not sure if the -i and -x switches work for both compressing and extracting or just the former. rar.exe has a command to extract a specified file. But, just realized it won't extract .7z archives like the gui version does.

If you got that to work you'd have to set that up to do it for all of the archives.

With that said, you might have to use the 7-zip SDK to create a program to extract just one file.
http://www.7-zip.org/sdk.html

The demo linked below says that it can extract a single file, which I think is done by index in the demo's case (as opposed to filename).
http://www.codeproject.com/KB/DLL/cs_interface_7zip.aspx

, but not really sure.
 
shadow2531 said:
From there, you'd have to use a 7z command to extract the specific file.

Code:
7z e archive.7z -otest firstfile.ext

That will extract just firstfile.ext in archive.7z to the test folder that's in the current directory.
 
O.K. Here's a Horrible Windows example that I threw together.

You select the .7z archives, drag them and drop them on the exe. The exe produces a batch file with commands to extract the first file from each of the archives.

It works in my limited testing. But, I'd consider it more of an idea than anything.
 
i'm now using an archiving program called powerarchiver and it just so happens to have a command line utility. using shadow's suggestions, i've been able to list the contents of a .cbr (essentially a .rar), export the list to a temp txt file, read it into head.bat and save the 1st filename to firstfile.txt, then read this back into the command utility to extract only the file listed in firstfile.txt. the only thing i would like is to be able to apply this batch to an entire folder of .cbr's.. I've tried using a for loop (i'm not too experienced with batch scripting) but it always gets caught after saving the first file name in firstfile.txt (it doesn't actually get to the command to extract the file, let alone move on to the next file in the directory to apply the commands to) could you help me a bit with this one?

here is the usage info for the powerarchiver utility:

------------------------------------------------------------------------------------------
PAEXT {-command ...} {d:}{\path\}filespec{.ext} {@list} {files ...}

Commands:
e : extract files from archive (default)
v : list contents of archive
t : test achive integrity

o+ : overwrite existing files without prompting
o- : do not overwrite existing files
or : allow overwriting read-only files
r : rename -- automatically rename files if they already exist
d : restore/create directory structure stored in archive
c : use character translations (use if you have problems with special characters)
q : quiet mode
l<file> : quiet mode + moves output to <file>
s<pwd> : decrypt with password
p<outputpath>: extract files to outputpath (default is current directory)
psub : extract files to filename/ subdirectory
(subdirectories - if extracting multiple archives)
$<directory> : set custom temp directory
@list : specify list of files for extraction (use filename instead of list - see example)
Note: this does not mean list of archives for extraction

Examples:

paext docs.zip
--> extract all files from docs.zip to current directory WITHOUT using archive folders

paext -d docs.zip
--> extract all files from docs.zip to current directory using archive folders

paext -t docs.zip
--> test contents of docs.zip

paext docs.arj *.dat *.txt
--> extract only those files in docs.arj with extensions .dat and .txt, to current directory

paext -pc:\temp *.*
--> extract all files from all supported archive files to c:\temp directory

paext -v docs.zip
--> list all files in docs.zip

paext -p"c:\my docs" docs.zip
--> extract all files from docs.zip to "c:\my docs"

paext -o+ docs.zip
--> extract all files from docs.zip and overwrite existing files without asking

paext -r docs.zip
--> extract all files in docs.zip but rename any existing file when extracted

paext *.zip @files.txt
--> extract all files specified in the file "files.txt" from all *.zip archives

Exit Codes:
0 --> no errors found
1 --> error in command line syntax, no action
2 --> error while extracting
-------------------------------------------------------------------------------------------------------------
Now, I have this in my batch: (DIR is a long path)
for /d %%X in (DIR\) do echo
c:
cd c:\pacl\
paext -v -d "DIR\*.cbr" | find /i ".jpg" > x:\temp.txt && x:\head.bat 1 x:\temp.txt > x:\firstfile.txt
paext -px:\temp "DIR" @x:\firstfile.txt
#^-output directory

what happens is:
-firstfile.txt get the 1st filename of the 1st file ONLY
-temp.txt gets the list of ALL files in ALL archives in the directory
-and nothing gets extracted to the temp folder

is there a way to get the batch script to apply the commands to each file and move to the next .cbr and finish when it reaches the last file in the directory? or even have it just output the name of the first file of each archive to firstfile.txt, so it is a list of "1st files" and apply the powerarchiver utility to that list. i've tried by manually copying and pasting the 1st files to firstfile.txt and seeing if it works, and it does. i just need a way to automate that whole procedure

EDIT: forgot you need to use call command to call another batch script. it successfully called the batch, and ran through all of the .cbrs in the directory, but only actually extracted the first file from the first archive. i think it's really an issue of having a loop traverse all of the files, now
 
EDIT 2:

Ok guys, I'm back to using 7zip, and I actually have it doing something:

This is on one line:

for /f "tokens=* delims=*" %%a IN ('dir /b x:\comics\new_comics\unchecked\') do 7z l x:\comics\new_comics\unchecked\%%~nxa | find /i ".jpg" | sort > x:\temp.txt && call x:\head.bat 1 x:\temp.txt >> x:\firstfile.txt

results:
1. the command is not executed on files with spaces in their name | can anyone lend help with using tokens and delimiters? I've read briefly online about them but I don't completely understand the concept

2. for some reason, some pages, like page 47 and 23 and 24 on some comics end up being extracted instead of page 1. i thought that was what the sort would do - to ensure that the pages would be sorted by name/number and the 1st would be extracted. any help on this too, guys?
 
Back
Top