PowerShell脚本通过目录扫描并返回划界文件信息和超链接



我的意图有点背景:

第一个功能的目的是getFilenames,是在一个主文件夹中读取包含许多设施的数字信息的目录,并返回使用Basename的文件列表。

第二个功能,即SplitFileNames,将getFileNames的输出输入,并使用下划线作为定界符将名称分为3个部分。文件的名称都是这样构成的;sitecode_facnum_filetype,例如,dkfx_00099_map。这三个部分中的每一个形成了单个列,然后我将其导入到访问中的数据库中。

另外,我从未在此之前使用过Powershell,到目前为止,我的经验基本上是反向工程和拼接代码的组合,显然是我自己的一些写作。

我的问题/尊重的请求是:

  1. 我几乎可以肯定,有一种更好的方法来做我要完成的事情,而且我对我所浏览的信息没有足够的了解它发生。因此,我绝对不愿意提出任何建议。

  2. 我也需要fullname中包含的超链接信息,但不幸的是,我永远无法使它正常工作,因为我只需要将Basename分为3件。

谢谢!

$targetPath = "C:UsersmattmGoogle DriveTestDatabase"
$outputPath_1 = "C:UsersmattmGoogle DrivePowershell ScriptsFacilities Database ScanneroutputScan_1.csv"
$outputPath_2 = "C:UsersmattmGoogle DrivePowershell ScriptsFacilities Database ScanneroutputScan_2.csv"
$delimPath = "_"

Function GetFileNames([string]$path, [string]$outputFile) {
  $list = Get-ChildItem $path -Recurse | where {!$_.PSIsContainer}
  $list | Select-Object BaseName | Export-Csv -NoTypeInformation $outputFile 
}
GetFileNames $targetPath $outputPath_1

Function SplitFileNames([string]$inputFile, [string]$outputFile) {
    $inputData = Get-Content $inputFile | select -Skip 1
    $array = @()
    $outArray = @()
    $inputData | Foreach{
                    $elements = $_.split($delimPath)
                    $array += ,@($elements[0], $elements[1], $elements[2])
                 }

    Foreach($value in $array){
        $outArray += New-Object PSObject -Property @{
        'SiteCode' = $value[0]
        'FacilityNumber' = $value[1]
        'FileTypeCode' = $value[2]
        }

    }
$outArray | Select-Object "SiteCode","FacilityNumber","FileTypeCode" | ConvertTo-Csv -NoTypeInformation | % {$_ -replace '"',""} | Out-File $outputFile -fo -en ascii
}
SplitFileNames $outputPath_1 $outputPath_2

您可能需要一些时间来学习PowerShell的基础知识。反向工程可能不是最好的老师。;-)从这样的东西开始:

$Path = 'Enter your path here'
Get-ChildItem -Path $Path -Recurse -File | 
    ForEach-Object{
        $One,$Two,$Three = $_.BaseName -split '_' 
        [PSCustomObject][ordered]@{
            SiteCode = $One
            FacNum = $Two
            FileType = $Three
            BaseName = $_.BaseName
            Hyperlink = $_.FullName
        }
    }

我对这个新版本的结果感到非常满意!

$moduleCustomUIMessage ='path to UI module'
$outputFile_3 = 'output path'
$Path = 'target path of directory to scan'
Import-Module $moduleCustomUIMessage
$results = Get-ChildItem -Path $Path -Recurse | where {!$_.PSIsContainer} | 
    ForEach-Object{
        $One,$Two,$Three = $_.BaseName -split '_' 
        [PSCustomObject]@{
            SiteCode = $One
            FacNum = $Two
            FileType = $Three
            BaseName = $_.BaseName
            Hyperlink = $_.FullName
        }
    } | Select-Object SiteCode, FacNum, FileType, BaseName, Hyperlink | Export-Csv -NoTypeInformation $outputFile_3 
    CustomUIMessage $outputFile_3

最新更新