Joh*_*nes 2 windows powershell powershell-2.0 powershell-3.0 output
我正在使用Powershell脚本,它应该创建一个包含目录顺序(文件夹,子文件夹,文件等)的文件:
$path = "golf.de/dgv"
Get-ChildItem -Path $folder -recurse | sort Directory, Name| format-Table -auto $path, Directory, Name | Out-File C:\Users\J.Kammermeier\Desktop\Johannes\testtext.txt
Run Code Online (Sandbox Code Playgroud)
到目前为止,输出看起来像这样
C:\Users\J.Kammermeier\Desktop\Johannes Test-Datei1.txt
C:\Users\J.Kammermeier\Desktop\Johannes Test-Datei2.txt
C:\Users\J.Kammermeier\Desktop\Johannes\Sonstige Datein\Musik WACKEN.txt
Run Code Online (Sandbox Code Playgroud)
但我按此顺序需要它:
.../Johannes Test-Datei1.txt
...Johannes\Sonstige Datein\Musik WACKEN.txt
Run Code Online (Sandbox Code Playgroud)
怎么实现呢?
你将不得不Directory使用Select-Object和计算属性来破坏属性:
# Set the path and folder property
$path = "golf.de/dgv"
$folder = "C:\Users\J.Kammermeier\Desktop\Johannes"
# Get the name of the parent folder (the part we want to remove)
$basePath = (Get-Item $folder).Parent.FullName
# Retrieve the files
$files = Get-ChildItem -Path $folder -Recurse
# Select the Name property and then two calculated properties, "Directory" and "Path"
$files = $files |Select-Object @{Name="BaseURL";Expression={"$path"}},
@{Name="Directory";Expression={$_.Directory.FullName.Substring($basePath.Length - 1)}},
Name
# Sort them
$files = $files |Sort-Object Directory, Name
# Formatted output to file
$files | Format-Table -AutoSize | Out-File C:\Users\J.Kammermeier\Desktop\Johannes\testtext.txt
Run Code Online (Sandbox Code Playgroud)
根据细节,我猜您正在尝试审核网站的文件,您可以组合使用Path和Directory属性并修复反斜杠-replace:
@{Name="URLPath";Expression={"$path/" + $($_.Directory.FullName.Substring($basePath.Length - 1) -replace "\\","/")}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |