Batch script help request: How to find a line break?

本小妞迷上赌 提交于 2019-12-02 09:11:26

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" /?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!