Powershell 读取文件的文件名和路径
Powershell read file for filename and path
我有以下脚本,其中 运行s 在一个目录中的 .zip 文件上,该目录具有包含许多文件的整个目录结构。然后这些文件上有 7zip 运行 以提取它们,然后将 .eml 添加到提取的文件中。
& "c:\program files-zipz.exe" x c:\TestZip -r -oC:\TestRestore 2> c:\TestLog\ziplog.txt
& "c:\program files-zipz.exe" x c:\TestRestore -r -aos -oc:\TestExtract 2> c:\TestLog\sevenzip.txt
gci -path "c:\TestExtract" -file | rename-item -newname {$PSItem.name + ".eml"}
我的问题是,在这些文件中,有时 7zip 无法完成最终提取,因为它没有将其视为存档。我发现这些特定的文件,如果我只是将 .eml 放在它们上面,它们就可以作为电子邮件访问。因此,当存档无法提取时,我将输出写入 sevenzip.txt 文件。
我需要帮助的是如何读取此文件以获取文件名并将它们放在一个目录中,以便我可以添加 .eml 扩展名。
sevenzip.txt文件中的输出示例如下
ERROR: c:\TestRestore[=11=][=11=]25ccb78f80d28b7569b6759554d.0_4011
Can not open the file as archive
ERROR: c:\TestRestore[=11=][=11=]bfa7acb219dec5d9e55d4eadd6eb1.0_3958
Can not open the file as archive
如能提供有关如何执行此操作的任何帮助,我们将不胜感激。
很抱歉收到所有评论,但我正在努力
$SourceFile = 'c:\testlog\sevenzip.txt'
$DestinationFile = 'c:\testlog\testlogextractnew.txt'
$Pattern = 'c:\TestRestore\' (Get-Content $SourceFile) |
% {if ($_ -match $Pattern){$_}} |
Set-Content $DestinationFile (Get-Content $DestinationFile).replace('ERROR: ', '') |
Set-Content $DestinationFile (Get-Content$DestinationFile).replace('7z.exe : ', '') |
Set-Content $DestinationFile
如果没有其他错误,那么您可以使用正则表达式模式匹配从文件中挑选出那些文件名:
$filesToFix = Select-String -Pattern "ERROR: (.*)" -LiteralPath 'c:\testlog\sevenzip.txt' |
ForEach-Object {$_.Matches.Groups[1].Value}
并可能将它们重命名为
$filesToFix | ForEach-Object {
Rename-Item -LiteralPath $_ -NewName {"$_.eml"}
}
如果可能存在其他错误而您只需要这些,则需要在匹配上做更多工作:
$fileContent = Get-Content -LiteralPath 'c:\testlog\sevenzip.txt' -Raw
$filesToFix = [regex]::Matches($fileContent,
"ERROR: (.*)\nCan not open the file as archive",
'Multiline') |
ForEach-Object {$_.Groups[1].Value}
这就是我最后在评论中使用它的原因,因为我不知道如何格式化,但现在已经解决了,所以希望这能让它更容易理解,以防它有用。
此部分创建所有未能提取的文件及其目录位置的列表。供下一节使用。
$SourceFile = 'c:\testlog\sevenzip.txt' $DestinationFile = c:\testlog\testlogextractnew.txt'
$Pattern = 'c:\TestRestore\'
(Get-Content $SourceFile) | % {if ($_ -match $Pattern){$_}} | Set-Content $DestinationFile
(Get-Content $DestinationFile).replace('ERROR: ', '') | Set-Content $DestinationFile
(Get-Content $DestinationFile).replace('7z.exe : ', '') | Set-Content $DestinationFile
这只是一些结果检查
Get-Content C:\testlog\testlogextractnew.txt | Measure-Object –Line > c:\testlog\numberoffilesthatfailed.txt
这会将 txt 文件中列出的所有文件复制到另一个目录
c:\testlog\testlogextractnew.txt | copy-item -destination "c:\TestCopy"
这会将当前扩展名重命名为 .eml
gci -path "C:\TestCopy" -file | rename-item -newname { [io.path]::changeextension($_.name, "eml")}
我有以下脚本,其中 运行s 在一个目录中的 .zip 文件上,该目录具有包含许多文件的整个目录结构。然后这些文件上有 7zip 运行 以提取它们,然后将 .eml 添加到提取的文件中。
& "c:\program files-zipz.exe" x c:\TestZip -r -oC:\TestRestore 2> c:\TestLog\ziplog.txt
& "c:\program files-zipz.exe" x c:\TestRestore -r -aos -oc:\TestExtract 2> c:\TestLog\sevenzip.txt
gci -path "c:\TestExtract" -file | rename-item -newname {$PSItem.name + ".eml"}
我的问题是,在这些文件中,有时 7zip 无法完成最终提取,因为它没有将其视为存档。我发现这些特定的文件,如果我只是将 .eml 放在它们上面,它们就可以作为电子邮件访问。因此,当存档无法提取时,我将输出写入 sevenzip.txt 文件。 我需要帮助的是如何读取此文件以获取文件名并将它们放在一个目录中,以便我可以添加 .eml 扩展名。 sevenzip.txt文件中的输出示例如下
ERROR: c:\TestRestore[=11=][=11=]25ccb78f80d28b7569b6759554d.0_4011
Can not open the file as archive
ERROR: c:\TestRestore[=11=][=11=]bfa7acb219dec5d9e55d4eadd6eb1.0_3958
Can not open the file as archive
如能提供有关如何执行此操作的任何帮助,我们将不胜感激。
很抱歉收到所有评论,但我正在努力
$SourceFile = 'c:\testlog\sevenzip.txt'
$DestinationFile = 'c:\testlog\testlogextractnew.txt'
$Pattern = 'c:\TestRestore\' (Get-Content $SourceFile) |
% {if ($_ -match $Pattern){$_}} |
Set-Content $DestinationFile (Get-Content $DestinationFile).replace('ERROR: ', '') |
Set-Content $DestinationFile (Get-Content$DestinationFile).replace('7z.exe : ', '') |
Set-Content $DestinationFile
如果没有其他错误,那么您可以使用正则表达式模式匹配从文件中挑选出那些文件名:
$filesToFix = Select-String -Pattern "ERROR: (.*)" -LiteralPath 'c:\testlog\sevenzip.txt' |
ForEach-Object {$_.Matches.Groups[1].Value}
并可能将它们重命名为
$filesToFix | ForEach-Object {
Rename-Item -LiteralPath $_ -NewName {"$_.eml"}
}
如果可能存在其他错误而您只需要这些,则需要在匹配上做更多工作:
$fileContent = Get-Content -LiteralPath 'c:\testlog\sevenzip.txt' -Raw
$filesToFix = [regex]::Matches($fileContent,
"ERROR: (.*)\nCan not open the file as archive",
'Multiline') |
ForEach-Object {$_.Groups[1].Value}
这就是我最后在评论中使用它的原因,因为我不知道如何格式化,但现在已经解决了,所以希望这能让它更容易理解,以防它有用。
此部分创建所有未能提取的文件及其目录位置的列表。供下一节使用。
$SourceFile = 'c:\testlog\sevenzip.txt' $DestinationFile = c:\testlog\testlogextractnew.txt'
$Pattern = 'c:\TestRestore\'
(Get-Content $SourceFile) | % {if ($_ -match $Pattern){$_}} | Set-Content $DestinationFile
(Get-Content $DestinationFile).replace('ERROR: ', '') | Set-Content $DestinationFile
(Get-Content $DestinationFile).replace('7z.exe : ', '') | Set-Content $DestinationFile
这只是一些结果检查
Get-Content C:\testlog\testlogextractnew.txt | Measure-Object –Line > c:\testlog\numberoffilesthatfailed.txt
这会将 txt 文件中列出的所有文件复制到另一个目录
c:\testlog\testlogextractnew.txt | copy-item -destination "c:\TestCopy"
这会将当前扩展名重命名为 .eml
gci -path "C:\TestCopy" -file | rename-item -newname { [io.path]::changeextension($_.name, "eml")}