每天有 3 個新的 CSV 檔案被發送到 3 個不同的路徑。我想將這些檔案復制到每個路徑中的存檔中,然后運行一個腳本來洗掉每個檔案的第一行。然后將生成的檔案移動到另一個檔案夾,批處理讀取它們。
路徑示例 1:“Z:\Test\1\Script\Export1_2022-11-09.csv” 存檔示例 1:“Z:\Test\1\Script\Archive\Export1_2022-11-09.csv”
路徑示例 2:“Z:\Test\2\Script\Export2_2022-11-09.csv” 存檔示例 2:“Z:\Test\2\Script\Archive\Export2_2022-11-09.csv”
$Files = @( "Z:\Test\1\Script\Export1_2022-11-09.csv", "Z:\Test\2\Script\Export2_2022-11-09.csv")
$Files | ForEach-Object {
Copy-Item $_ -Destination
(Get-Content $_ | Select-Object -Skip 1) | Set-Content $_
}
每個檔案的第一行被洗掉的部分作業正常,但我想不出一種方法將每個檔案復制到之前的特定和不同路徑。我將非常感謝一些幫助。先感謝您。
@Theo:我編輯了我的問題,因為我犯了一個錯誤,檔案有不同的名稱。
uj5u.com熱心網友回復:
看來您想要的是將原始檔案保存在同一路徑中的存檔子檔案夾中。然后重寫原始檔案,以便洗掉標題行。
用于[System.IO.Path]::GetDirectoryName()從完整路徑和檔案名字串中拆分路徑,然后將其與“存檔”連接
# if the file dates in their names are always todays date,
# you should not hard code these, but instead create your array like:
# $today = Get-Date -Format 'yyyy-MM-dd'
# $Files = "Z:\Test\1\Script\Export1_$($today).csv", "Z:\Test\2\Script\Export2_$($today).csv"
$Files = "Z:\Test\1\Script\Export1_2022-11-09.csv", "Z:\Test\2\Script\Export2_2022-11-09.csv"
$Files | ForEach-Object {
# construct the archive path
$archivePath = Join-Path -Path ([System.IO.Path]::GetDirectoryName($_)) -ChildPath 'Archive'
# create that folder if it does not already exist
$null = New-Item -Path $archivePath -ItemType Directory -Force
# copy the file as-is
Copy-Item -Path $_ -Destination $archivePath
# now remove the header line from the original file
(Get-Content $_ | Select-Object -Skip 1) | Set-Content $_
# create a new path and filename to move the edited files to a specific folder where a batch can process them
$basePath = '{0}\Transfer' -f $_.Substring(0,[System.IO.Path]::GetDirectoryName($_).LastIndexof("\"))
$baseName = ([System.IO.Path]::GetFileNameWithoutExtension($_) -split '_')[0]
$extension = [System.IO.Path]::GetExtension($_) # this includes the dot
$finalPath = Join-Path -Path $basePath -ChildPath ('{0}{1}' -f $baseName, $extension)
$_ | Move-Item -Destination $finalPath -Force
}
uj5u.com熱心網友回復:
嘗試以下獲取新檔案名
Files = @( "Z:\Test\1\Script\Export_2022-11-09.csv", "Z:\Test\2\Script\Export_2022-11-09.csv")
foreach($File in $Files)
{
$lastIndex = $File.LastIndexOf("\")
$baseFilename = $File.Substring($lastIndex 1)
$newFilename = $File.Substring(0, $lastIndex 1)
$newFilename = "Archive\" $baseFileName
Write-Host "New File Name = " $newFilename
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531491.html
標籤:电源外壳复制
