Cmd 7z仅列出文件名

Cmd 7z仅列出文件名,cmd,7zip,Cmd,7zip,我使用的是7z版本18.05,我只想列出归档内容的文件名 如果我使用命令7z l myArchive.7z,我会得到以下输出: 7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30 Scanning the drive for archives: 1 file, 146863932 bytes (141 MiB) Listing archive: myArchive.7z -- Path = myArchi

我使用的是7z版本18.05,我只想列出归档内容的文件名

如果我使用命令7z l myArchive.7z,我会得到以下输出:

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 146863932 bytes (141 MiB)

Listing archive: myArchive.7z

--
Path = myArchive.7z
Type = 7z
Physical Size = 146863932
Headers Size = 393
Method = LZMA:26
Solid = +
Blocks = 1

Date       Time     Attr          Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2017-12-06 08:55:47 D...A            0            0  myArchive
2017-12-06 08:55:42 D...A            0            0  myArchive\folder
2017-12-05 19:50:41 ....A     21816530    146863539  myArchive\folder\Test.dat
2017-12-06 08:55:42 ....A     21877463               myArchive\folder\Test2.dat
2017-12-05 19:51:05 ....A       153953               myArchive\folder\Test3.dat
2017-12-05 19:50:41 ....A         4193               myArchive\folder\Test4.dat
2017-12-06 08:55:47 ....A     24128956               myArchive\log.txt
2017-12-06 08:55:47 ....A        79980               myArchive\readme.txt
2017-12-05 19:51:05 ....A   3256759999               myArchive\folder\zTest.txt
------------------- ----- ------------ ------------  ------------------------
2017-12-06 08:55:47         3324821074    146863539  7 files, 2 folders

我不知道为什么7z没有切换到只列出文件名。如何获得唯一的名称列?有没有建议使用dos命令?

如果可以在计算机上安装PowerShell模块,列出文件名就很容易了。这可以在任何现代受支持的Windows系统上完成

描述如何安装该模块

下面是一个.bat文件脚本,显示其用法和输出

C:>TYPE zipfnlist.bat
@ECHO OFF
SET "ZIP_FILENAME=.\7zIntf20.zip"
powershell -NoLogo -NoProfile -Command (Get-7Zip -ArchiveFileName "%ZIP_FILENAME%").FileName

C:>CALL zipfnlist.bat
bin
Properties
Ole32.cs
Program.cs
SevenZipFormat.cs
SevenZipInterface.cs
SevenZip.csproj
SevenZip.sln
bin\Debug
bin\Release
bin\Release\SevenZip.exe
Properties\AssemblyInfo.cs

如果您可以在计算机上安装PowerShell模块,那么列出文件名就很容易了。这可以在任何现代受支持的Windows系统上完成

描述如何安装该模块

下面是一个.bat文件脚本,显示其用法和输出

C:>TYPE zipfnlist.bat
@ECHO OFF
SET "ZIP_FILENAME=.\7zIntf20.zip"
powershell -NoLogo -NoProfile -Command (Get-7Zip -ArchiveFileName "%ZIP_FILENAME%").FileName

C:>CALL zipfnlist.bat
bin
Properties
Ole32.cs
Program.cs
SevenZipFormat.cs
SevenZipInterface.cs
SevenZip.csproj
SevenZip.sln
bin\Debug
bin\Release
bin\Release\SevenZip.exe
Properties\AssemblyInfo.cs

在另一个线程中找到此答案:

有一个未记录的switch-ba,它删除了所有的头和表格式,只列出了行条目


从那里,您可以解析每一行,并用空格或制表符将其拆分,或者可能使用正则表达式。

在不同的线程中找到了这个答案:

有一个未记录的switch-ba,它删除了所有的头和表格式,只列出了行条目


从那里,您可以解析每一行并用空格或制表符将其拆分,或者可能使用正则表达式。

如果您不喜欢使用未记录的命令行开关的声音,您可以执行以下操作从完整输出中解析出文件名。此awk脚本确定名称列标题的开始索引,并使用该索引从表中提取列

awk_script='{
    if (ix == 0) {
        ix = index($0, "Name");
    }
    p = (body == 1);
    if (ix > 0) {
        # The table body is delimited by dashed lines, after the "Name" column header has been seen
        body = (body + ($0 ~ / *-[ -]+/)) % 2;
    }
    if (p == 1 && body == 1) {
        # Only print if "body" was 1 before and after the previous block; otherwise, we are in
        # the table body delimiter line (or outside the table completely)
        print substr($0, ix);
    }
}'

7z l your-file.7z | awk "$awk_script"
PowerShell等效:

$ix=-1;
$body=$false;

& 7z l your-file.7z | foreach { `
    if ($ix -eq -1) {`
        $ix = $_.IndexOf("Name");`
    }`
    $p = $body;`
    if ($ix -gt 0) {`
        # The table body is delimited by dashed lines, after the "Name" column header has been seen`
        $body = ($body -ne ($_ -match ' *-[ -]+'))
    }`
    if ($p -and $body) {`
        # Only print if "body" was 1 before and after the previous block; otherwise, we are in`
        # the table body delimiter line (or outside the table completely)`
        write-output $_.Substring($ix)`
    }`
}

如果您不喜欢使用未记录的命令行开关,可以执行以下操作从完整输出中解析出文件名。此awk脚本确定名称列标题的开始索引,并使用该索引从表中提取列

awk_script='{
    if (ix == 0) {
        ix = index($0, "Name");
    }
    p = (body == 1);
    if (ix > 0) {
        # The table body is delimited by dashed lines, after the "Name" column header has been seen
        body = (body + ($0 ~ / *-[ -]+/)) % 2;
    }
    if (p == 1 && body == 1) {
        # Only print if "body" was 1 before and after the previous block; otherwise, we are in
        # the table body delimiter line (or outside the table completely)
        print substr($0, ix);
    }
}'

7z l your-file.7z | awk "$awk_script"
PowerShell等效:

$ix=-1;
$body=$false;

& 7z l your-file.7z | foreach { `
    if ($ix -eq -1) {`
        $ix = $_.IndexOf("Name");`
    }`
    $p = $body;`
    if ($ix -gt 0) {`
        # The table body is delimited by dashed lines, after the "Name" column header has been seen`
        $body = ($body -ne ($_ -match ' *-[ -]+'))
    }`
    if ($p -and $body) {`
        # Only print if "body" was 1 before and after the previous block; otherwise, we are in`
        # the table body delimiter line (or outside the table completely)`
        write-output $_.Substring($ix)`
    }`
}

我在寻找这个确切问题的答案,并在Nisse Knudsen提供的链接中找到了答案,但对我来说,-ba未注册的开关无法自行完成我需要的任务,这个开关本身也不起作用,Nisse的回答似乎也不能完全回答OP的问题—OP想知道如何只获取每个文件的名称,并且—在解析.7z文件中的数据时,单独使用ba并不总是有效的。我会发表评论,而不是完整的回答,但我没有赢得足够的评论代表,我相信这些信息仍然是相关的和准确的,我的评论无论如何都会太长

使用-slt开关引用Nisse提供的链接,将输出格式化为更可读的格式,以便循环/解析,批处理文件中的简单for/f循环可以解析并提供所需的内容

让我列出输出中的一些更改,以便您了解每个开关的作用

此:7z.exe l C:\Some Directory\Some FileIZipped.zip

变为:7z.exe l-slt C:\Some Directory\Some FileIZipped.zip

添加-ba命令进一步简化了格式,避免了跳过我在for循环的注释中引用的标题行的需要,如最后的脚本示例所示

这将进一步变成:7z.exe l-ba-slt C:\Some Directory\Some FileIZipped.zip

我使用此方法将存档zip/7z/rar与实际目录进行文件比较,以在主目录处创建镜像副本。为此,我正在解析一个包含7z list命令输出的文件。我想我可以直接从7z命令迭代for循环,但是我发现在某些情况下,当归档文件中有大量数据时,这种迭代速度会变慢

我遇到过多个试图解析标准输出失败的实例——它发生在列出.7z归档文件的内容时,如下所示。这是不容易解决使用for循环解析空间。对于大多数显示为压缩空间的行,标记5将成为文件名,文件名以.zip格式存档为标记6,因此,您将面临一个非常混乱的情况,这是一场噩梦。这也是OP在给出的示例中引用的确切问题

类似于OP提供的示例:

7-Zip [64] 15.12 : Copyright (c) 1999-2015 Igor Pavlov : 2015-11-19

Scanning the drive for archives:
1 file, 446600 bytes (437 KiB)

Listing archive: C:\Some Directory\Some _OTHER_ FileIZipped.7z

--
Path = C:\Some Directory\Some _OTHER_ FileIZipped.7z
Type = 7z
Physical Size = 446600
Headers Size = 283
Method = LZMA2:22
Solid = +
Blocks = 1

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2020-08-28 15:06:46 D....            0            0  SomeDirectoryInside
2020-08-28 15:06:46 D....            0            0  SomeDirectoryInside\OtherDir
2020-08-28 15:06:46 D....            0            0  SomeDirectoryInside\Zips
2020-08-28 15:13:14 .....      1064960       446317  SomeDirectoryInside\Zips\Some_File.Doc
2020-08-28 15:08:02 .....       313080               SomeDirectoryInside\Zips\Some_Other_File.Doc
2020-08-28 15:07:34 .....      1561728               SomeDirectoryInside\Zips\Foo.mov
2020-08-28 15:07:46 .....       262144               SomeDirectoryInside\Zips\Fancy.Doc
2020-08-28 15:07:26 .....       262144               SomeDirectoryInside\Zips\Fancy2.Doc
------------------- ----- ------------ ------------  ------------------------
2020-08-28 15:13:14            3464056       446317  5 files, 3 folders
下面是我编写的一个批处理脚本示例,它将7z.exe输出放入一个文件中,然后从文件中提取数据,得到我所需要的。请原谅多行REM-我更喜欢这种注释方法,而不是长的单行字符串,这样读者就不必为了阅读而向右滚动代码块

由于For/f如何遍历数据,我们需要确保令牌%%c不是空的。我之所以使用这种方法,是因为有时我们的文件名中有空格,我们使用Sp解析7z输出 ACE作为分隔符

令牌3*将为您提供两个单独的令牌,您可以检查-令牌%%b[令牌3]和%%c[令牌*]-如果%%c为空-我们知道%%b没有空格,可以安全地回显到我们需要的任何文件或设置为稍后使用的变量,等等

@Echo Off
  REM Sending the output of 7z into a file to use later
  7z.exe l -slt "SomeFileIZipped.zip" >"ZipListRAW.txt"
  
  REM Example of 7z.exe command with '-ba' switch
  REM 7z.exe l -ba -slt "SomeFileIZipped.zip"
  
  REM If you do not use '-ba' in the 7z command above, you can simply skip the first
  REM 11-12 lines of the file to get ONLY the filenames (skips past first line containing
  REM "Path" which contains the original archive filename.
  
  For /f "Usebackq Skip=11 Tokens=1,3* Delims= " %%a in ("ZipListRAW.txt") do (
    REM Checking if %%a equals word "Path"
    If "%%a"=="Path" (
      If [%%c]==[] (
        Echo %%b
      ) ELSE (
        Echo %%b %%c
      )
    )
  )

我在寻找这个确切问题的答案,并在Nisse Knudsen提供的链接中找到了答案,但对我来说,-ba未注册的开关无法自行完成我需要的任务,这个开关本身也不起作用,Nisse的回答似乎也不能完全回答OP的问题—OP想知道如何只获取每个文件的名称,并且—在解析.7z文件中的数据时,单独使用ba并不总是有效的。我会发表评论,而不是完整的回答,但我没有赢得足够的评论代表,我相信这些信息仍然是相关的和准确的,我的评论无论如何都会太长

使用-slt开关引用Nisse提供的链接,将输出格式化为更可读的格式,以便循环/解析,批处理文件中的简单for/f循环可以解析并提供所需的内容

让我列出输出中的一些更改,以便您了解每个开关的作用

此:7z.exe l C:\Some Directory\Some FileIZipped.zip

变为:7z.exe l-slt C:\Some Directory\Some FileIZipped.zip

添加-ba命令进一步简化了格式,避免了跳过我在for循环的注释中引用的标题行的需要,如最后的脚本示例所示

这将进一步变成:7z.exe l-ba-slt C:\Some Directory\Some FileIZipped.zip

我使用此方法将存档zip/7z/rar与实际目录进行文件比较,以在主目录处创建镜像副本。为此,我正在解析一个包含7z list命令输出的文件。我想我可以直接从7z命令迭代for循环,但是我发现在某些情况下,当归档文件中有大量数据时,这种迭代速度会变慢

我遇到过多个试图解析标准输出失败的实例——它发生在列出.7z归档文件的内容时,如下所示。这是不容易解决使用for循环解析空间。对于大多数显示为压缩空间的行,标记5将成为文件名,文件名以.zip格式存档为标记6,因此,您将面临一个非常混乱的情况,这是一场噩梦。这也是OP在给出的示例中引用的确切问题

类似于OP提供的示例:

7-Zip [64] 15.12 : Copyright (c) 1999-2015 Igor Pavlov : 2015-11-19

Scanning the drive for archives:
1 file, 446600 bytes (437 KiB)

Listing archive: C:\Some Directory\Some _OTHER_ FileIZipped.7z

--
Path = C:\Some Directory\Some _OTHER_ FileIZipped.7z
Type = 7z
Physical Size = 446600
Headers Size = 283
Method = LZMA2:22
Solid = +
Blocks = 1

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2020-08-28 15:06:46 D....            0            0  SomeDirectoryInside
2020-08-28 15:06:46 D....            0            0  SomeDirectoryInside\OtherDir
2020-08-28 15:06:46 D....            0            0  SomeDirectoryInside\Zips
2020-08-28 15:13:14 .....      1064960       446317  SomeDirectoryInside\Zips\Some_File.Doc
2020-08-28 15:08:02 .....       313080               SomeDirectoryInside\Zips\Some_Other_File.Doc
2020-08-28 15:07:34 .....      1561728               SomeDirectoryInside\Zips\Foo.mov
2020-08-28 15:07:46 .....       262144               SomeDirectoryInside\Zips\Fancy.Doc
2020-08-28 15:07:26 .....       262144               SomeDirectoryInside\Zips\Fancy2.Doc
------------------- ----- ------------ ------------  ------------------------
2020-08-28 15:13:14            3464056       446317  5 files, 3 folders
下面是我编写的一个批处理脚本示例,它将7z.exe输出放入一个文件中,然后从文件中提取数据,得到我所需要的。请原谅多行REM-我更喜欢这种注释方法,而不是长的单行字符串,这样读者就不必为了阅读而向右滚动代码块

由于For/f如何遍历数据,我们需要确保令牌%%c不是空的。我之所以使用这种方法,是因为有时我们的文件名中有空格,我们使用空格作为分隔符来解析7z输出

令牌3*将为您提供两个单独的令牌,您可以检查-令牌%%b[令牌3]和%%c[令牌*]-如果%%c为空-我们知道%%b没有空格,可以安全地回显到我们需要的任何文件或设置为稍后使用的变量,等等

@Echo Off
  REM Sending the output of 7z into a file to use later
  7z.exe l -slt "SomeFileIZipped.zip" >"ZipListRAW.txt"
  
  REM Example of 7z.exe command with '-ba' switch
  REM 7z.exe l -ba -slt "SomeFileIZipped.zip"
  
  REM If you do not use '-ba' in the 7z command above, you can simply skip the first
  REM 11-12 lines of the file to get ONLY the filenames (skips past first line containing
  REM "Path" which contains the original archive filename.
  
  For /f "Usebackq Skip=11 Tokens=1,3* Delims= " %%a in ("ZipListRAW.txt") do (
    REM Checking if %%a equals word "Path"
    If "%%a"=="Path" (
      If [%%c]==[] (
        Echo %%b
      ) ELSE (
        Echo %%b %%c
      )
    )
  )

很高兴看到您在Windows上更深入地挖掘它!很高兴看到您在Windows上更深入地挖掘它!