【问题标题】:I want to be able to split the output of a directory and its subdirectory我希望能够拆分目录及其子目录的输出
【发布时间】: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


【解决方案1】:

一个选项:

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

【讨论】:

  • 感谢您,添加了一个花括号。没有为客户、ClientGroup、CLient 和 CLientDivision 返回任何信息。我正在调查这个。
  • 我在发布的第一个版本中有错字,在对象哈希中使用 $parts[4] 到 $parts[7]。应该是 $parts[0] 到 $parts[3]。它已在答案中修复,但它最初会产生该结果。
  • 仍然不知道为什么它没有返回 .txt 文件所在的子文件夹。
  • 开始工作了!不得不重组文件夹;这几乎是我所做的一切,我得到了所需的输出,但文本文件仍然填充在错误的列中,但我可以手动修复它。非常感谢!你摇滚!
  • 不客气。感谢@Anthony Stringer 的编辑。他的解决方案可能也可以工作(没有测试),但我认为简单的文本拆分会更容易理解。
【解决方案2】:

试试这个

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多