【发布时间】:2016-03-16 20:13:16
【问题描述】:
我希望能够使用 PowerShell 拆分目录及其子目录的输出。目前这是我的代码:
Get-Childitem -path \\serverName\lettertext\Customer -Recurse -Include *.txt |
Out-file c:\Letters\output.txt
这列出了目录和子文件夹的所有文本内容。
完整目录格式为:
\\serverName\lettertext\Customer\Client Group\Client\Client Division
为此,我想以这种格式输出结果:
客户|客户群|客户|客户事业部|输出.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)
}
【问题讨论】:
-
什么是“目录的输出”?
-
这里有点猜想,但是你想根据文件夹的名字把每个文件夹的结果输出到一个单独的文件中吗?
-
基本上父目录包含包含txt文件的子目录或另一个包含文件的子目录或另一个包含文件的子目录..
-
例如,这是脚本的输出: Directory: \\serverName\lettertext\Customer\Client Group Mode LastWriteTime Length Name ---- ----------- -- ------ ---- -a--- 2013 年 6 月 11 日下午 2:39 5167 COBRATermination.txt
-
如此基本,而不是将结果标记为 \\server\customer\etc\etc...我希望它仅将结果输出为:Customer CLient Group Client CLient Division,然后是文本文件。当我将其粘贴到 excel 中时,所有格式都已格式化,而无需重新格式化输出。
标签: powershell directory