【问题标题】:File/Directory Path should look like this - Powershell output文件/目录路径应如下所示 - Powershell 输出
【发布时间】:2014-09-26 16:32:33
【问题描述】:

我正在使用一个 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

到目前为止,输出看起来像这样

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 

但我需要按这个顺序:

.../Johannes                         Test-Datei1.txt 

...Johannes\Sonstige Datein\Musik    WACKEN.txt 

如何实现?

【问题讨论】:

  • 请把脚本提供给我们,我会让它为你工作:)
  • $path = "golf.de/dgv" Get-ChildItem -Path $folder -recurse |排序目录,名称| format-Table -auto $path, 目录, 名称 |输出文件 C:\Users\J.Kammermeier\Desktop\Johannes\testtext.txt
  • 所以...您只想删除“C:\Users\J.Kammermeier\Desktop\”部分,就这样吗?
  • 是的,应该只留下“Johannes”和“Johannes”之后的文件夹

标签: windows powershell output powershell-2.0 powershell-3.0


【解决方案1】:

您必须使用Select-Objectcalculated properties 稍微修改Directory 属性:

# 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

从细节来看,我猜您正在尝试审核网站的文件,您可以结合 PathDirectory 属性并使用 -replace 修复反斜杠:

@{Name="URLPath";Expression={"$path/" + $($_.Directory.FullName.Substring($basePath.Length - 1) -replace "\\","/")}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    相关资源
    最近更新 更多