简体   繁体   English

从一个目录导入多个 JSON/csv 文件,然后使用 powershell 转换后移动到不同的目录

[英]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.

相关问题 从文件目录导入多个选定的 json 文件,使用 Python - Import multiple selected json files from the file directory, using Python 使用 powershell 从 json 转换后的 csv 文件或从初始 json 文件本身提取特定列 - Extracting particular columns from a json converted csv file using powershell, or from the initial json file itself Powershell:目录文件结构到 JSON - Powershell: Directory file structure into JSON 通过 Python 脚本将整个目录中的 JSON 文件导入 MongoDB - Import JSON Files from an entire directory into a MongoDB via a Python script 使用 Py 循环将 JSON 文件目录转换为 CSV 文件 - Convert a directory of JSON files to one CSV file with a Py loop 使用Powershell从json文件导出csv - export csv from json file using powershell 从一个目录导入多个json文件并附加数据 - Import multiple json files from a directory and attaching the data 有没有一种方法可以将package.json文件中的依赖项安装到其他目​​录中? - Is there a way to install dependencies from a package.json file into a different directory? Python - 从目录的多个 Yaml 文件创建单个 Json - Python - Create a single Json from multiple Yaml file of a directory 使用PowerShell中的CSV数据修改JSON文件 - Modifying JSON file using data from CSV in PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM