PS脚本导出空的CSV [英] PS Script is exporting an empty CSVs

查看:167
本文介绍了PS脚本导出空的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙着尝试理解为什么此脚本未按预期工作的原因.这是一个简单的脚本,我尝试在其中导入CSV,选择所需的几列,然后导出CSV并复制自身. (由于内存大小的限制,基本上我们已经存档了数据,对于另一个项目,我只需要其中的几列).该脚本非常简单,显然与它在不起作用时会造成多少挫败感成反比...现在,最终结果是我最终得到一个空的csv,而不是仅包含我选择的列的csv选择对象.

I am having a helluva time trying to understand why this script is not working as intended. It is a simple script in which I am attempting to import a CSV, select a few columns that I want, then export the CSV and copy over itself. (Basically we have archived data that I only need a few columns from for another project due to memory size constraints). This script is very simple, which apparently has an inverse relationship with how much frustration it causes when it doesn't work... Right now the end result is I end up with an empty csv instead of a csv containing only the columns I selected with Select-Object.

$RootPath = "D:\SomeFolder"

$csvFilePaths = Get-ChildItem $RootPath -Recurse -Include *.csv | 
    ForEach-Object{
        Import-CSV $_ |
        Select-Object Test_Name, Test_DataName, Device_Model, Device_FW, Data_Avg_ms, Data_StdDev | 
        Export-Csv $_.FullName -NoType -Force
}

推荐答案

除非您将输入文件预先完整地读入内存 ,否则您将无法安全地进行读写操作给定管道中的相同文件.

Unless you read the input file into memory in full, up front, you cannot safely read from and write back to the same file in a given pipeline.

具体来说,诸如Import-Csv file.csv | ... | Export-Csv file.csv之类的命令会擦除 file.csv的内容.

Specifically, a command such as Import-Csv file.csv | ... | Export-Csv file.csv will erase the content of file.csv.

最简单的解决方案是将用于读取输入文件的命令封装在(...) 中,但请注意:

The simplest solution is to enclose the command that reads the input file in (...), but note that:

  • 文件的内容(已转换为对象)必须整体上容纳在内存中.

如果在所有(已转换的)对象都写回到文件之前中断了管道,则存在轻微的数据丢失风险.

There is a slight risk of data loss if the pipeline is interrupted before all (transformed) objects have been written back to the file.

应用于您的命令:

$RootPath = "D:\SomeFolder"

Get-ChildItem $RootPath -Recurse -Include *.csv -OutVariable csvFiles | 
    ForEach-Object{
        (Import-CSV $_.FullName) | # NOTE THE (...)
          Select-Object Test_Name, Test_DataName, Device_Model, Device_FW, 
                        Data_Avg_ms, Data_StdDev | 
            Export-Csv $_.FullName -NoType -Force
}

请注意,我已经使用-OutVariable csvFiles来收集输出变量$csvFiles中的CSV文件信息对象.您尝试通过$csvFilePaths = ...收集文件路径的尝试无效,因为它尝试收集 Export-Csv输出,但是Export-Csv没有任何输出.

Note that I've used -OutVariable csvFiles in order to collect the CSV file-info objects in output variable $csvFiles. Your attempt to collect the file paths via $csvFilePaths = ... doesn't work, because it attempts to collects Export-Csv's output, but Export-Csv produces no output.

为了安全起见,我将Import-Csv参数从$_更改为$_.FullName,以确保Import-Csv找到输入文件(因为很遗憾,文件信息对象$_已绑定作为字符串,有时 会扩展为纯文件名).

Also, to be safe, I've changed the Import-Csv argument from $_ to $_.FullName to ensure that Import-Csv finds the input file (because, regrettably, file-info object $_ is bound as a string, which sometimes expands to the mere file name).

一种更安全的解决方案是先输出到临时文件,然后(仅)在成功完成后替换原始文件.

A safer solution would be to output to a temporary file first, and (only) on successful completion replace the original file.

无论采用哪种方法,替换文件都将具有默认文件属性和权限;否则,替换文件将具有默认文件属性和权限.如果原始文件具有要保留的特殊属性和/或权限,则必须显式重新创建它们.

With either approach, the replacement file will have default file attributes and permissions; if the original file had special attributes and/or permissions that you want to preserve, you must recreate them explicitly.

这篇关于PS脚本导出空的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆