批处理脚本请求帮助:如何找到一个换行符? [英] Batch script help request: How to find a line break?

查看:98
本文介绍了批处理脚本请求帮助:如何找到一个换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆的JPG文件包含RAR文件,我试图让每一个RAR文件的第一个文件的名称。

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.

例如,如果我跑 7Z升-r abc.rar ,我想获得的第一个文件 01.JPG

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

到目前为止,用下面的批处理脚本,我能够剥离7Z命令的元数据和获取文件列表中的 contents.txt 文件。

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

输出示例:

"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

我想如果我调整我的剧本找到第一个换行符随后在7z格式输出.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

但我不知道如何在 FINDSTR 命令搜索换行符。我试图找到.JPG \\ N的 FINDSTR

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

但它没有工作。

谁能帮我?

我的期望的输出是从文件名,输出去除所有的日期和文件大小信息和RAR文件名后添加,例如:

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

有是第一个JPG文件没有固定的模式。有些开始与00,部分01,有的用文字和数字(例如x00y.jpg)。我已经试过各种方法来搜索以00开始或01有和没有文字(例如,在我命令我使用的文件名 FINDSTR / R / E[AZ] [0-1] * .JPG来搜索文本和数字文件名,但也有在文件名中没有文字一些RAR文件)。

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).

推荐答案

执行在命令提示符窗口 FINDSTR /?输出此命令的帮助列出并解释 / E 搜索在行结束指定的字符串。使用此选项更容易比其他一切在行尾找到字符串。

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

此批处理文件调用的7-Zip 在当前目录下的每个文件列出这是由 FINDSTR 以进入临时列表文件刚刚结束的行过滤存档内容与 .JPG 在任何情况下。

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.

临时列表文件是由处理FOR 循环,从档案文件的名字写在一起为内容的文本文件的第一行得到的只是文件名。

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.

另一个解决方案是使用作为直接处理的列表输出的7-Zip

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

此批处理文件调用的7-Zip 在当前目录下的每个文件列出重定向到一个临时列表文件存档内容。

This batch file calls 7-Zip for each file in current directory to list the archive contents redirected into a temporary list file.

临时列表文件是由作为循环处理包含 A线之后,从第一行得到的只是文件名----------- -------- 与归档文件的名称到内容的文本文件写在一起。

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.

上面的列表中的行数不恒定这是SKIP = 不能在此处使用的原因作为选项

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.

还有一个解决方案是使用免费的的UnRAR 以获得每个RAR压缩文件的第一个文件名。

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

的优点是,的UnRAR 支持多种选项列表格式包括使用命令时使用空格式 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.


  • 呼叫/?

  • DEL /?

  • 回声/?

  • ENDLOCAL /?

  • 退出/

  • FINDSTR /?

  • 为/?

  • 如果/?

  • 设置/?

  • SETLOCAL /?

  • 为%ProgramFiles%\\ 7-Zip的\\ 7z.exe? -

  • 为%ProgramFiles%\\ WinRAR的\\ UnRAR.exe/?

  • call /?
  • del /?
  • echo /?
  • endlocal /?
  • exit /?
  • findstr /?
  • for /?
  • if /?
  • set /?
  • setlocal /?
  • "%ProgramFiles%\7-Zip\7z.exe" -?
  • "%ProgramFiles%\WinRAR\UnRAR.exe" /?

这篇关于批处理脚本请求帮助:如何找到一个换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆