我希望能够使用PowerShell拆分目录及其子目录的输出。目前这是我的代码:
Get-Childitem -path \\serverName\lettertext\Customer -Recurse -Include *.txt |
Out-file c:\Letters\output.txt
列出目录和子文件夹的所有文本内容。
完整目录格式为:
\\serverName\lettertext\Customer\Client Group\Client\Client Division
对于这个研究,我想以这种格式输出结果:
Customer| CLient Group| CLient| CLient Division| Outputs.txt
有谁知道如何实现这一目标?
我更改了代码,这是我将其更改为但不起作用的内容:
# Trying to pull a directory listing of customized letter text files
$Testing = $false
$DisplayElapsedTime = 0
[DateTime] $StartDate = Get-Date
$RootPath = "\\servername\LetterText"
$pattern = "*.txt"
$Customer = ""
#$FileName = 'C:\letters\' + $FileName
if ($Testing -eq $false) {
$x = Read-Host 'Customer Name'
if ($x -ne $null) {$Customer = $x}
$x = $null
$x = Read-Host 'File Name (blank for all) '
if ($x -ne "") {$pattern = $x}
} else {
}
if ($Customer -eq "") {
$RequestedCustomer = " All"
$FileName = 'LetterTextFiles ' + $StartDate.Year.ToString() +
$StartDate.Month.ToString('00') +
$StartDate.Day.ToString('00') + '.ltr'
} else {
$RequestedCustomer = $Customer
$FileName = 'LetterTextFiles ' + $StartDate.Year.ToString() +
$StartDate.Month.ToString('00') +
$StartDate.Day.ToString('00') + '--' + $Customer + '.ltr'
}
#$FileName = 'C:\util\' + $FileName
Write-Host '' | Out-File -FilePath $FileName -Encoding Default
"Requested Customer: "+ $RequestedCustomer >> $FileName
"Requested Files: " + $pattern >> $FileName
$Path = $RootPath + '\' +$Customer
cls
try {
$Results = Get-ChildItem $Path -Recurse -Include:$pattern
if ($DisplayElapsedTime -eq -1) {
[DateTime] $StartTime
#'Listing Time:'
$StartTime = Get-Date
}
$DirectoryName = ''
foreach ($result in $Results) {
if ($DirectoryName -ne $result.Directory.Name) {
#" " >> $FileName
$y = ""
$DirectoryName = $result.Directory.Name
$FullName = $result.FullName.Replace($RootPath,'').Split('\')
$level = $result.FullName.Replace($RootPath,'').Split('\')
if ($result.DirectoryName -eq $RootPath) {
$y = 'COBRAPoint base letters:'
#Write-Host $y | Out-File -FilePath $FileName -Encoding Default
" " >> $FileName
$y >> $FileName
} else {
for ($x = 0; $x -lt $level.Length -1; $x++) {
switch ($x) {
1 {$y = 'Customer: '+ $level[$x]}
2 {$y = ' Client Group: '+ $level[$x]}
3 {$y = ' Client: '+ $level[$x]}
4 {$y = ' Client Division: '+ $level[$x]}
}
#Write-Host $y|out-file -FilePath $FileName -Encoding Default
$y >> $FileName
}
}
}
$y = "`t" + $result.Name
#Write-Host $y | Out-File -FilePath $FileName -Encoding Default
$y >> $FileName
#break
}
} catch [System.Exception] {
$y = "No Files Found"
$y >> $FileName
}
#Out-File $FileName
#$Output > 'out.txt'
$FileName
'Done'
if ($DisplayElapsedTime -eq -1) {
'Total Time:'
New-TimeSpan -Start $StartTime -End (Get-Date)
}
答案 0 :(得分:2)
一个选项:
Get-Childitem -path \\serverName\lettertext\Customer -Recurse -Include *.txt |
ForEach-Object {
$Parts = $_.fullname.split('\')[4..7]
[PSCustomObject]@{
Customer = $Parts[0]
ClientGroup = $Parts[1]
Client = $Parts[2]
ClientDivision = $Parts[3]
FileName = $_.FullName | Split-Path -Leaf
}
} | Export-Csv c:\somedir\clientfile.csv -NoTypeInformation
答案 1 :(得分:1)
试试这个
$folder = 'c:\temp'
$customer = Split-Path $folder -Leaf
Get-Childitem -Path $folder -Recurse -Include *.txt | % {
$sub = ($_.fullname -replace [regex]::Escape($folder)).trim('\')
$split = $sub.split('\')
if ($split.count -eq 4) {
[pscustomobject]@{
Customer = $customer
ClientGroup = $split[0]
Client = $split[1]
ClientDivision = $split[2]
File = $split[3]
}
}
} | epcsv c:\Letters\output.csv -not