Reputation: 139
I have a bunch of RAR files containing JPGs and I am trying to get the name of the first file of each RAR file.
For example, if I run 7z l -r abc.rar
, I would like to get the first file 01.jpg
path = xyz.rar
Type = zip
Physical Size = 15430338
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2003-03-06 13:32:44 ....A 330433 325405 01.jpg
2003-03-06 13:34:08 ....A 301098 291857 02.jpg
2003-03-06 13:34:14 ....A 257770 244619 03.jpg
2003-03-06 13:34:22 ....A 301220 292019 04.jpg
2003-03-06 13:34:30 ....A 326989 316380 05.jpg
So far, with the following batch script, I am able to strip the metadata of the 7z command and get the file list in the contents.txt
file.
set PATH=%PATH%;"C:\Program Files\7-Zip"
for %%f in ("*.*") do (
echo "%%f" >> contents.txt
echo.
7z l -r "%%f" | FIND /V "ing " | FIND /V "Igor Pavlov" | FIND /V "--" | FIND /V "Path" | FIND /V "Type" | FIND /V "Solid" | FIND /V "Blocks" | FIND /V "Multivolume" | FIND /V "Volumes" | FIND /V "Date" | FIND /V "---" | FIND /V "Physical Size" | FINDSTR /R /E ".jpg" >> contents.txt
echo.
)
PAUSE
Example output:
"abc.rar"
2003-03-26 01:18:50 ....A 331711 330648 01.jpg
2003-03-26 01:18:54 ....A 271173 270942 02.jpg
"xyz.rar"
2003-07-08 22:40:56 ....A 445799 430058 p00.jpg
2003-07-08 22:40:40 ....A 181324 142554 p01.jpg
2003-07-08 22:40:42 ....A 384901 370140 p02.jpg
"efg.rar"
2003-07-08 22:42:54 ....A 156436 115275 xy01abc.jpg
2003-07-08 22:42:58 ....A 456633 448240 xy02abc.jpg
2003-07-08 22:42:58 ....A 355114 339026 xy03abc.jpg
2003-07-08 22:42:58 ....A 355114 339026 xy04abc.jpg
I think if I tweak my script to find the first line break followed by ".jpg" in the 7z output, I can get the first file name in the following format:
"abc.rar"
2003-03-26 01:18:50 ....A 331711 330648 01.jpg
"xyz.rar"
2003-07-08 22:40:56 ....A 445799 430058 p00.jpg
"efg.rar"
2003-07-08 22:42:54 ....A 156436 115275 xy01abc.jpg
But I don't know how to search for line break in the FINDSTR command. I tried to find for ".jpg\n" in FINDSTR:
7z l -r "%%f" | FIND /V "ing " | FIND /V "Igor Pavlov" | FIND /V "--" | FIND /V "Path" | FIND /V "Type" | FIND /V "Solid" | FIND /V "Blocks" | FIND /V "Multivolume" | FIND /V "Volumes" | FIND /V "Date" | FIND /V "---" | FIND /V "Physical Size" | FINDSTR /R /E ".jpg\n" >> contents.txt
But it didn't work.
Can anyone help me?
My desired output is to strip all the date and file size information from the file name output and add it after the RAR file name, such as:
"abc.rar" 01.jpg
"xyz.rar" p00.jpg
"efg.rar" xy01abc.jpg
There is no fixed pattern of the first jpg file. Some start with 00, some with 01, some with text and number (example x00y.jpg). I've tried various methods to search for file names that start with 00 or 01 with and without text (example, in my command I used FINDSTR /R /E "[a-z][0-1]*.jpg"
to search for file names with text and number, but there are some RAR files with no text in file names).
Upvotes: 0
Views: 510
Reputation: 49186
Executing in a command prompt window findstr /?
outputs help for this command listing and explaining /E
to search for the specified string at end of the lines. Using this option is much easier than everything else to find a string at end of a line.
One possible batch solution for your task could be:
@echo off
del contents.txt 2>nul
for %%A in ("*.*") do call :GetFirstFileName "%%~A"
exit /B
:GetFirstFileName
"%ProgramFiles%\7-Zip\7z.exe" l "%~1" | %SystemRoot%\System32\findstr.exe /E /I "/C:.jpg">"%TEMP%\FilesList.tmp"
for /F "usebackq eol=` tokens=5* delims= " %%B in ("%TEMP%\FilesList.tmp") do (
echo "%~1" %%C>>contents.txt
del "%TEMP%\FilesList.tmp"
exit /B
)
del "%TEMP%\FilesList.tmp"
exit /B
This batch file calls 7-Zip for each file in current directory to list the archive contents which is filtered by findstr to get into temporary list file just the lines ending with .jpg
in any case.
The temporary list file is processed by a FOR loop to get just the file name from first line written together with archive file name into contents text file.
Another solution would be using FOR directly to process list output of 7-Zip.
@echo off
setlocal EnableDelayedExpansion
del contents.txt 2>nul
for %%A in ("*.*") do call :GetFirstFileName "%%~A"
endlocal
exit /B
:GetFirstFileName
set "NextLine=0"
"%ProgramFiles%\7-Zip\7z.exe" l "%~1">"%TEMP%\FilesList.tmp"
for /F "usebackq eol=` tokens=1,5* delims= " %%B in ("%TEMP%\FilesList.tmp") do (
if "%%B" == "-------------------" (
set "NextLine=1"
) else if "!NextLine!" == "1" (
echo "%~1" %%D>>contents.txt
del "%TEMP%\FilesList.tmp"
exit /B
)
)
del "%TEMP%\FilesList.tmp"
exit /B
This batch file calls 7-Zip for each file in current directory to list the archive contents redirected into a temporary list file.
The temporary list file is processed by a FOR loop to get just the file name from first line after a line containing -------------------
written together with archive file name into contents text file.
The number of lines above the list is not constant which is the reason why FOR option skip=
can't be used here.
It does not matter which extension first file in archive file has for this batch solution.
One more solution is using free UnRAR to get first file name of each RAR archive file.
@echo off
del contents.txt 2>nul
for %%A in ("*.rar") do call :GetFirstFileName "%%~A"
exit /B
:GetFirstFileName
"%ProgramFiles%\WinRAR\UnRAR.exe" lb "%~1">"%TEMP%\FilesList.tmp"
for /F "usebackq eol=: delims=" %%B in ("%TEMP%\FilesList.tmp") do (
echo "%~1" %%B>>contents.txt
del "%TEMP%\FilesList.tmp"
exit /B
)
del "%TEMP%\FilesList.tmp"
exit /B
The advantage is that UnRAR supports several options for list format including a bare format used when using command l
with b
appended. This makes getting first file name much easier.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
del /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
if /?
set /?
setlocal /?
"%ProgramFiles%\7-Zip\7z.exe" -?
"%ProgramFiles%\WinRAR\UnRAR.exe" /?
Upvotes: 1