簡體   English   中英

從一個目錄導入多個 JSON/csv 文件,然后使用 powershell 轉換后移動到不同的目錄

[英]Import multiple JSON/csv file from one directory then move to different directory after converted via using powershell

我在 Content 中有一批 JSON 格式的 csv 擴展文件,並嘗試使用 powershell 轉換為分隔格式的 csv。

一個JSON文件一旦被轉換,轉換后需要移動到另一個目錄進行備份。 下面的腳本僅在從一個目錄導入一個靜態文件時才有效。 我正在嘗試使用“Get-ChildItem | foreach-object”,但由於某種原因它不起作用。 通過運行這個腳本,raw 中的所有文件將被立即移動到備份文件夾中,但轉換后的文件夾中沒有任何文件被轉換。

感覺 #execute 轉換腳本根本沒有運行。 我不太確定是否 $fromJson = Get-Content -Raw $InputPath | ConvertFrom-Json”適用於循環導入文件。

需要幫忙!

工作腳本:

$InputPath ="C:\Users\Desktop\raw\a.csv"

$OutputPath = "C:\Users\Desktop\raw\Converted\$(get-date -f yyyy-MM-ddhhmmssfffff).csv"

$fromJson = Get-Content -Raw $InputPath | ConvertFrom-Json 
& {

# execute converting script

} | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content -path $OutputPath 


Move-Item -path $InputPath -destination "C:\Users\Desktop\raw\backup"



有問題的腳本:

$InputPath ="C:\Users\Desktop\raw\*.csv"

Get-ChildItem $InputPath| Foreach-Object{
$OutputPath = "C:\Users\Desktop\raw\Converted\$(get-date -f yyyy-MM-ddhhmmssfffff).csv"

$fromJson = Get-Content -Raw $InputPath | ConvertFrom-Json 
& {

# execute converting script

} | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content -path $OutputPath 


Move-Item -path $InputPath -destination "C:\Users\Desktop\raw\backup"

}

您需要先獲取所有 CSV 文件,然后循環瀏覽它們。 以下內容應該適用於您所追求的內容。

$CSVFiles = Get-ChildItem -Path "C:\Users\Desktop\raw\" -Filter "*.CSV"
Foreach($File in $CSVFiles) {
    Get-Content -Raw $File.FullName | ConvertFrom-Json 
    & {
        # execute converting script
    } | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Set-Content -path $("C:\Users\Desktop\raw\Converted\$($File.BaseName)_$(get-date -f yyyy-MM-ddhhmmssfffff).csv")
    Move-Item -path $File.FullName -destination "C:\Users\Desktop\raw\backup"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM