如何使用PowerShell将一个文本文件的内容复制到另一个只包含文件名及其扩展名的文本文件?

时间:2018-04-12 13:50:59

标签: powershell text powershell-v3.0 powershell-v4.0

我有一个文本文件,其中包含文件列表及其完整路径以及扩展名,我想要的是将此文件列表复制到另一个文本文件,只包含文件名和扩展名,而不是整个路径。

例如 - 将demo.txt内容复制到test.txt

Demo.txt 

C:\Demo\Excluded Files\AppData\List\ConfigurationUtility.BackUp.exe.config
C:\Demo\Excluded Files\AppData\List\ConfigurationUtility.exe.Config
C:\Demo\Excluded Files\AppData\List\ConfigurationUtility.WMIExtensions.XML
C:\Demo\Excluded Files\AppData\List\Microsoft.Administration.dll
C:\Demo\Excluded Files\AppData\List\ModelConfiguration.xml
C:\Demo\Excluded Files\AppData\List\Newtons.Json.dll
C:\Demo\Excluded Files\AppData\List\Data\Help\List User Guide.pdf
C:\Demo\Excluded Files\AppData\List\Data\Help\ListHelp.chm
C:\Demo\Excluded Files\AppData\List\Data\Help\intel Load Balancing.pdf


test.txt

ConfigurationUtility.BackUp.exe.config
ConfigurationUtility.exe.Config
ConfigurationUtility.WMIExtensions.XML
Microsoft.Administration.dll
ModelConfiguration.xml
Newtons.Json.dll
List User Guide.pdf
ListHelp.chm
intell Load Balancing.pdf

我如何使用PowerShell执行此操作?

1 个答案:

答案 0 :(得分:2)

Get-Content demo.txt | %{Split-Path $_ -leaf} | Set-Content test.txt

要回答你从一开始就没有问过的另一个问题(因为我已经在狗窝里为不首先尝试的海报提供代码):

Get-Content demo.txt | %{(Split-Path $_ -leaf).Split('.')[-1]} | Set-Content test.txt