用文件名替换字符串的 Powershell 脚本
如何通过替换文件中存在的字符串将文件名(基本名称而不是全名)写入同一文件中。 基本上,我在名为 C:\Downloads\PreValidation 的文件夹内有一堆 .psf 文件。
- 我想将每个文件更改为 .csv 格式,
- 同时我想替换存在的名为“Space Planning Project”的字符串 在文件的记录之一内,文件名不带 扩大。
这是我尝试运行的脚本 -
foreach($file in (dir C:\Downloads\PreValidation\*.psf)){
$fromdir = "C:\Downloads\Prevalidation\*.psf"
$fullname = gi $fromdir| select fullname
$b = $fullname[0]
$b.fullname
Copy-Item $file.fullname C:\Downloads\Validation\WIP\psaload1.csv
Get-Content C:\Downloads\Validation\WIP\psaload.csv | ForEach-Object {$_.replace("Space Planning Project","$b.fullname")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv
}
但它错误提示“
Get-Content : Cannot find path 'C:\Downloads\Validation\WIP\psaload.csv' because it does not exist.
At C:\cap_sps\powershell\testfilename.ps1:7 char:12
+ Get-Content <<<< C:\Downloads\Validation\WIP\psaload.csv | ForEach-Object {$_.replace("Space Planning Project","$b.fullname")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv
+ CategoryInfo : ObjectNotFound: (C:\Downloads\Validation\WIP\psaload.csv:String) [Get-Content], ItemNotF
oundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
请注意,我正在使用最终的 psaload.csv 加载到 Oracle 数据库中,并且在脚本的最后一步中,我将删除此文件,以便我可以运行对上述文件夹中存在的其余 .psf 文件循环执行相同的命令。
非常感谢任何帮助。
谢谢, 桑德斯。
How do I write the filename (basename and not the fullname) into the same file by replacing a string present in the file.
Basically, I have a bunch of .psf files inside a folder called C:\Downloads\PreValidation.
- I want to change each file to a .csv format and
- while doing so I want to replace the string called 'Space Planning Project' present
inside one of the records of the file with the filename without the
extension.
This is the script that I am trying to run -
foreach($file in (dir C:\Downloads\PreValidation\*.psf)){
$fromdir = "C:\Downloads\Prevalidation\*.psf"
$fullname = gi $fromdir| select fullname
$b = $fullname[0]
$b.fullname
Copy-Item $file.fullname C:\Downloads\Validation\WIP\psaload1.csv
Get-Content C:\Downloads\Validation\WIP\psaload.csv | ForEach-Object {$_.replace("Space Planning Project","$b.fullname")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv
}
but its erroring out saying
Get-Content : Cannot find path 'C:\Downloads\Validation\WIP\psaload.csv' because it does not exist.
At C:\cap_sps\powershell\testfilename.ps1:7 char:12
+ Get-Content <<<< C:\Downloads\Validation\WIP\psaload.csv | ForEach-Object {$_.replace("Space Planning Project","$b.fullname")} | Set-Content C:\Downloads\Validation\WIP\psaload.csv
+ CategoryInfo : ObjectNotFound: (C:\Downloads\Validation\WIP\psaload.csv:String) [Get-Content], ItemNotF
oundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Note that I am using my final psaload.csv to load into an Oracle database and in the last step of my script I am removing this file so that I can run the same commands in loop for the rest of the .psf files present in the above folder.
Any help is deeply appreciated.
Thanks,
Sanders.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中有很多冗余,
dir
在循环中,然后使用该文件再次获取该项目。此外,您还为每个 psf 文件复制到同一个 csv 文件,并且复制行提到了一个文件psaload1.csv
,但您正在尝试获取psaload.csv
的内容code>,导致您看到的错误。尝试这样的事情(我在这里做了一些假设并且未经测试):
There is a lot of redundancy in your code, with the
dir
in the loop and then using the file to get the item again. Also you are copying to the same csv file for each of the psf files and the copy line mentions a filepsaload1.csv
, but you are trying to get the content ofpsaload.csv
, causing the error that you see.Try something like this ( I am making some assumptions here and untested):
要获取文件对象的 BaseName,请使用 BaseName 属性。您可以像这样修改脚本:
至于错误消息,您正在尝试检索不存在的文件的内容。您的复制行的
文件名中包含“1”,但在下一行中,您的 Get-Content 文件名中没有。
To get the BaseName of a file object , use the BaseName property. You can modify your script like so:
As for the error message, you are trying to retrieve the contents of a file that does not exist. Your copy line
has a "1" in the file name, but in the next line, your filename to Get-Content does not.