Powershell 读取.CSV文件并根据值创建目录
我正在使用PowerShell调用存储过程并将输出存储在CSV文件中。使用相同的脚本,我打算读取csv并使用特定列中的值来创建我将文件移动到其中的目录。但是,我需要帮助创建初始基本代码 csv文件中的示例记录: 标题:人员类别子类别日期文件 值:Bob人类男性20191023 C:\Temp\Bob.jpg 工作流程: 对于SQL输出中的每一行 在输出文件夹中创建一个人子文件夹(如果不存在) 在Person子文件夹中创建类别文件夹(如果不存在) 在类别子文件夹中创建子类别文件夹(如果不存在) 在子类别子文件夹中创建日期文件夹(如果不存在)。文档日期将为YYYYMMDD格式。 将文档文件从源位置复制到文档日期文件夹。 当前代码:Powershell 读取.CSV文件并根据值创建目录,powershell,Powershell,我正在使用PowerShell调用存储过程并将输出存储在CSV文件中。使用相同的脚本,我打算读取csv并使用特定列中的值来创建我将文件移动到其中的目录。但是,我需要帮助创建初始基本代码 csv文件中的示例记录: 标题:人员类别子类别日期文件 值:Bob人类男性20191023 C:\Temp\Bob.jpg 工作流程: 对于SQL输出中的每一行 在输出文件夹中创建一个人子文件夹(如果不存在) 在Person子文件夹中创建类别文件夹(如果不存在) 在类别子文件夹中创建子类别文件夹(如果不存在) 在
#Variable Parameters
## Do not add final backslash to directory path
$FilePath="C:\Temp\Output"
#Date Parameters
Echo "Getting date & time"
$GetDate=Get-Date
$DateStr = $GetDate.ToString("yyyyMMdd")
$TimeStr = $GetDate.ToString("HHmm")
#SQL Connection & SP Execution
Echo "Establishing SQL Connection"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=MyServer;Database=MyDatabase;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
#Run Stored Procedure
Echo "Running Stored Procedure"
#Return Row Count Print from SP
Echo "Row Count:" #This result with be printed via the SP
$SqlCmd.CommandText = "[SCHEMA].[STOREDPROCEDURE]"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
#Close SQL Connection
Echo "Closing SQL Connection"
$SqlConnection.Close()
#Output File
Echo "Outputting File"
$DataSet.Tables[0] | Export-CSV -notype "$($FilePath)\$($DateStr)_$($TimeStr)_Export.csv"
#Finished Exporting .csv File
Echo "File Exported to Output Directory"
你可以这样做:
# Import the CSV File
$CSVData = Import-CSV -Path C:\Temp\file.csv
# For each record in the CSV file
$CSVData | foreach-object {
# Build the personpath string
$PersonFolder = "C:\$($_.Person)"
# Test if the path (!NOT) exists
if (!test-path $PersonFolder){
# Create the person folder
new-item -path $PersonFolder -ItemType Directory -Force
}
# Build the category path string (adding to the person folder path)
$CategoryPath = "$PersonFolder\$($_.Category)"
# Test if the path (!NOT) exists
<Code Here>
# Create category folder...
# Create date folder etc. etc. etc.
}
希望这能给你一些想法 只需一个新项目调用即可创建整个路径,如下所示:
$rootPath = 'D:\Persons'
#import your csv file and loop through the results
Import-Csv -Path 'D:\persons.csv' | ForEach-Object {
# create the path to output copy the file to
# [System.IO.Path]::Combine() can combine 4 childpaths in one go
$subPath = [System.IO.Path]::Combine($_.Person, $_.Category, $_.Subcategory, $_.Date)
$fullPath = Join-Path -Path $rootPath -ChildPath $subPath
if (!(Test-Path -Path $fullPath -PathType Container)) {
New-Item -Path $fullPath -ItemType Directory | Out-Null
}
Copy-Item -Path $_.File -Destination $fullPath -WhatIf
}
如果您对控制台中显示的结果感到满意,请删除-WhatIf开关以实际开始复制文件。Hi Richard。谢谢你,这是朝着正确方向迈出的一步。我更喜欢在if,else语句中这样做。例如:“测试路径是否存在如果测试路径$PersonFolder Else创建个人文件夹new item-path$PersonFolder-ItemType Directory-Force}”如果要进入下一个目录,if中会出现什么?