具有相同名称和路径的Import-Csv和Export-Csv [英] Import-Csv and Export-Csv with same name and path

查看:83
本文介绍了具有相同名称和路径的Import-Csv和Export-Csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个非常简单的问题,但是我无法在线找到资源。我需要从 csv 中删除​​行,而无需更改文件名或路径。目前,我正在尝试导入文件,过滤所需的项目并将数据导出到具有相同文件名的相同位置:

This should be a fairly simple question to answer, but I haven't been able to find a resource online. I need to remove rows from a csv with out changing the filename or path. Currently I am trying to import the file, filter the items I need and export that data to the same location with the same file name:

foreach ($csv in $csvLists){
Import-Csv $csv | Where-Object {[DateTime]$_.'Date' -ge $2WeeksOld} | Export-Csv $csv -NoType -Force
}

也尝试过:

foreach ($csv in $csvLists){
Import-Csv $csv | Where-Object {[DateTime]$_.'Date' -ge $2WeeksOld} | Export-Csv $csv.PSParentPath -NoType -Force
}

我无法访问错误:

Export-Csv : Access to the path 'D:\Test' is denied.
At C:\script.ps1:20 char:71
+ ... .'Date' -ge $2WeeksOld} | Export-Csv $csv.PSParentPath -NoType -Force
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

我在这里想念什么?是Powershell正在读取文件并且无法同时导出文件的问题吗?我应该尝试将导入的数据设置为变量,然后在 import-csv 管道之外导出csv吗?

What am I missing here? Is the issue that powershell is reading the file and cant export over it at the same time? Should I try setting the imported data as a variable and then exporting the csv outside the import-csv pipeline?

一如既往地感谢您的帮助。

As always thanks for any help.

推荐答案

使用 $ csv.FullName 。其中包含文件/文件夹的完整路径。

这是 System.IO.FileInfo 的一些常用属性:

Use $csv.FullName. This contains the full path for a file/folder.
Here are a few common properties of System.IO.FileInfo:

Property        Contents
BaseName        MyFile
Directory*      <returns System.IO.DirectoryInfo object of "C:\Path\To">
Name            MyFile.txt
FullName        C:\Path\To\MyFile.txt
PSParentPath    Microsoft.PowerShell.Core\FileSystem::C:\Path\To

我不知道 .PSParentPath 。查看内容,假设您需要指定 PSProvider 以及父级的字符串值。否则,您可以只使用 .Directory.Name

I'm not aware of a use case for .PSParentPath. Looking at the content, I assume it's used when you need to specify the PSProvider as well as the string value of the parent. Otherwise you could just use .Directory.Name

您可能会找到 FileInfo DirectoryInfo 有用。它们是 Get-ChildItem 返回的对象。链接列表方法和属性;他们没有列出Code / Note / ScriptProperty类型。

You may find the class definitions for FileInfo and DirectoryInfo useful. They are the objects returned by Get-ChildItem. The links list Methods and Properties; they do not list Code/Note/ScriptProperty types.

提示:运行 $ csv | Get-Member 查看该对象的成员,请查看 MemberType

tip: run $csv | Get-Member to see members of this object, look at MemberType.

*对于System.IO.DirectoryInfo对象,请使用Parent。在Win10中出现了一些不同的行为,并确保引入了别名,以便Parent用于FileInfo或Directory用于DirectoryInfo。

这篇关于具有相同名称和路径的Import-Csv和Export-Csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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