在复制项目上重命名文件

问题描述 投票:1回答:1

我目前正在读取CSV文件,并根据列值创建文件夹结构,并将文件复制到这些子目录中。我目前正在努力进行调整,以便将文件名在复制到DotDate_CurrentName时进行修改(DotDate是csv中的一列)。

##Pull the CSV & Create Directories
Echo "Getting root directory for CSV File" #via root variable

#import the csv file and loop through the results
Echo "Importing CSV file"
Import-Csv -Path "$($FilePath)\$($DateStr)_$($TimeStr)_Export.csv" | ForEach-Object {
    Echo "Building subpath and fullpath strings"
    $subPath = [System.IO.Path]::Combine($_.'ID' + '_' + $_.'First Name' + '_' + $_.'Surname', $_.Area, $_.SubArea, $_.'DotDate')
    $fullPath = Join-Path -Path $rootPath -ChildPath $subPath
    "Test fullpath and build path from strings"
    if (!(Test-Path -Path $fullPath -PathType Container)) {
        New-Item -Path $fullPath -ItemType Directory | Out-Null
    }
    Copy-Item -Path $_.'Document File Path' -Destination $fullPath
}
powershell csv
1个回答
0
投票

您无需复制然后重命名;您可以在同一操作中同时执行这两项操作。要像执行操作一样在DotDate之前添加appending,可以执行以下操作:

#requires -Version 4
@(Import-Csv -Path "$FilePath${DateStr}_${TimeStr}_Export.csv").ForEach{
    $path = [IO.Path]::Combine(
        $rootPath,
        $_.DotDate,
        ('{0}_{1}_{2}' -f $_.ID, $_.'First Name', $_.Surname),
        $_.Area,
        $_.SubArea
    )

    if (-not (Test-Path -Path $path -PathType Container)) {
        New-Item -Path $path -ItemType Directory >$null
    }

    Copy-Item -Path $_.'Document File Path' -Destination $path
}
© www.soinside.com 2019 - 2024. All rights reserved.