Powershell - String Manipulation - 从完整文件路径获取目录

时间:2017-03-01 13:49:37

标签: string powershell-v3.0

请记住,我是新手并且要温柔。

我有一个文件“C:\ folder1 \ folder2 \ 01.03.2017 - FileName.csv”的完整文件路径,我想操纵它以返回文件存储的目录在( C:\ folder1 \ folder2 )中,减去文件名( 01.03.2017 - FileName.csv )。

我正在尝试制作这种模块化,以便无论文件所在的子文件夹数量多少都能正常工作;我们也不会提前知道FileName,所以再次需要模块化并删除包含最后一个“ \

有关当前如何构建此内容的背景信息,我在上一个问题中删除了一些我在StackOverflow上看到的代码:

Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = “All files (*.*)| *.*”
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName

# *** Entry Point to Script ***

$originalData = Get-FileName -initialDirectory “c:\” | Out-String
Write-Host $originalData
$originalDir = $originalData.Split('\')
$originalDir

运行此当前提示您将在Windows中看到“打开的对话框”。您选择一个文件夹,输出当前是:

C:\folder1\folder2\01.03.2017 - FileName.csv

C:
folder1
folder2
01.03.2017 - FileName.csv

我尝试了几次不同的-join尝试,但没有成功。

我们将输入 C:\ folder1 \ folder2 \ 01.03.2017 - FileName.csv 作为变量 $ originalData

我们希望输出 C:\ folder1 \ folder2 作为变量 $ originalDir

1 个答案:

答案 0 :(得分:0)

SELECT src.COUNTYID
     , src.INDUSTRYID
     , src.YEAR
     , src.QTR
     , case when (n_oa.value - n_oty.value) is null 
            then src.suppressed 
            else 2
       end as SUPPRESSED_CODE -- 0=NOT SUPPRESSED, 1=SUPPRESSED, 2=INFERRED
     , src.DATA_CAT
     , src.DATA_DENOM
     , coalesce(n_oa.value - n_oty.value, src.value) as VALUE
  FROM          QCEW src     --a source row from which we'll generate a record
      left join QCEW n_oa    --next yr (same qtr) overall (if src is suppressed/overall)
             on src.countyId = n_oa.countyId
            and src.industryId = n_oa.industryId
            and src.year = n_oa.year - 1
            and src.qtr = n_oa.qtr
            and src.data_denom = n_oa.data_denom
            and src.SUPPRESSED = 1 and n_oa.SUPPRESSED = 0
            and src.DATA_CAT = 1 and n_oa.DATA_CAT = 1
      left join QCEW n_oty   --next yr (same qtr) over-the-year (if src is suppressed/overall)
             on src.countyId = n_oty.countyId
            and src.industryId = n_oty.industryId
            and src.year = n_oty.year - 1
            and src.qtr = n_oty.qtr
            and src.data_denom = n_oty.data_denom
            and src.SUPPRESSED = 1 and n_oty.SUPPRESSED = 0
            and src.DATA_CAT = 1 and n_oty.DATA_CAT = 3

您可以像这样使用它,所以请使用结果或您的函数并将其与get-childitem一起使用。

编辑:注意第三行到最后一行没有Function Get-FileName($initialDirectory) { [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = “All files (*.*)| *.*” $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename } #end function Get-FileName $originalData = Get-FileName -initialDirectory “c:\” Write-Host $originalData $originalDir = (Get-ChildItem $originalData).DirectoryName