[英]Import-Csv locks file in PowerShell ISE
I want to read some CSV files with PowerShell and then save them in the same spot with a different delimiter: 我想使用PowerShell读取一些CSV文件,然后使用不同的定界符将它们保存在同一位置:
$content = (import-csv -path $filepath -Encoding utf8 -Delimiter ",")
write-host $(& ..\DLLs\Handle\handle.exe $filepath)
remove-item $filepath
export-csv ...
I fails at the deleting the file step. 我在删除文件步骤失败。 I have added the handle.exe Debug information as suggested in PowerShell unlock/unload imported CSV and it indeed shows that the powershell_ise.exe still has a handle on the file. 我已按照PowerShell解锁/卸载导入的CSV中的建议添加了handle.exe调试信息,它的确表明powershell_ise.exe对该文件仍然具有句柄。 When the script finishes, I can delete or rename it as I wish. 脚本完成后,我可以根据需要删除或重命名它。
How to get around this? 如何解决这个问题?
Try to omit the Remove-Item
cmdlet since its probably not necessary. 尝试省略 Remove-Item
cmdlet,因为它可能不是必需的。 The Import-CSV
cmdlet shouldn't lock the file and I can't reproduce that. Import-CSV
cmdlet不应锁定文件,我无法复制该文件。 However, you could try to use the Get-Content
to read the file and pipe it to the ConvertFrom-Csv
cmdlet (note: you don't need to explicit specify the delimiter for a comma separated CSV): 但是,您可以尝试使用Get-Content
读取文件并将其通过管道传递到ConvertFrom-Csv
cmdlet(注意:您无需为逗号分隔的CSV明确指定定界符):
$content = Get-Content $filepath -raw | ConvertFrom-Csv
I got it now. 我知道了 My particular setup, that part, which I had omitted, because I didn't think it was necessary, was the culprit. 我的特殊设置是罪魁祸首,我已经省略了这部分,因为我认为这不是必需的。
The above code was running within a pipeline, part of which was searching in the file for a certain text 上面的代码在管道中运行,其中一部分在文件中搜索特定文本
ls *.csv | select-string "someText" | % { do the CSV Stuff }
What was locking then, was the select-string command, not the import-csv. 然后锁定的是select-string命令,而不是import-csv。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.