[英]Import multiple JSON/csv file from one directory then move to different directory after converted via using powershell
I have a batch of csv extension files with JSON format in Content and tried to convert to a delimited format csv using powershell.我在 Content 中有一批 JSON 格式的 csv 扩展文件,并尝试使用 powershell 转换为分隔格式的 csv。
Once one JSON file been converted, it needs to be moved to another directory for backup after been converted.一个JSON文件一旦被转换,转换后需要移动到另一个目录进行备份。 The script below works only if import one static file from one directory.
下面的脚本仅在从一个目录导入一个静态文件时才有效。 I am trying to use "Get-ChildItem | foreach-object", but it doesn't work for some reason.
我正在尝试使用“Get-ChildItem | foreach-object”,但由于某种原因它不起作用。 By running this script, all files from raw will be moved to the backup folder straight away, but there is no files been converted in the converted folder.
通过运行这个脚本,raw 中的所有文件将被立即移动到备份文件夹中,但转换后的文件夹中没有任何文件被转换。
It feels like the # execute converting script didn't run at all.感觉 #execute 转换脚本根本没有运行。 I'm not too sure if $fromJson = Get-Content -Raw $InputPath |
我不太确定是否 $fromJson = Get-Content -Raw $InputPath | ConvertFrom-Json" works for importing files by loop.
ConvertFrom-Json”适用于循环导入文件。
Need help!需要帮忙!
working script:工作脚本:
$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"
Script with problem:有问题的脚本:
$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"
}
You Need to get all the CSV Files first then cycle through them.您需要先获取所有 CSV 文件,然后循环浏览它们。 The below should work for what you are after.
以下内容应该适用于您所追求的内容。
$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.