用于循环复制和重命名文件的 Powershell 脚本
我的 Prevalidation 文件夹中有许多扩展名为 .psa 的文件,我想要:
- 将它们逐个复制到我的工作文件夹中
- 将 .psa 文件重命名为 psaload.csv
- 针对该文件运行一组命令以将其加载到我的 工作文件夹中db
- ,然后从我的工作文件夹中删除 csv 文件。
将对源文件夹中的所有 .psa 文件重复此操作。
因此,问题是如何在现有的尽可能多的 .psa 文件上循环执行这组命令。
这是我的一段代码,仅测试 Prevalidation 文件夹中的一个文件 -
Copy-Item C:\Downloads\PreValidation\*.psa C:\Downloads\Validation\WIP
Rename-Item 'C:\Downloads\Validation\WIP\abc 1234.psa' 'psaload1.csv'
Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
<run the psaload.csv to load to my db>
这就是我打算做的 -
考虑我的 C:\Downloads\Prevalidation 文件夹中的多个 .psa 文件。
For each C:\Downloads\PreValidation\*.psa
BEGIN LOOP
Copy-Item C:\Downloads\PreValidation\aaaa.psa C:\Downloads\Validation\WIP\aaaa.psa
Rename-Item 'C:\Downloads\Validation\WIP\aaaa.psa' 'psaload1.csv'
Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
END LOOP
我正在寻找语法来为我的 /prevalidation 文件夹中的每个文件逐个运行这些命令集。
I have a number of files with extension .psa in my Prevalidation folder and I want to:
- copy them one by one into my working folder
- rename the .psa file to psaload.csv
- run a set of commands against the file to load it to my db
- and then delete the csv file from my working folder.
This will be repeated for all the .psa files I have on my source folder.
So, the question is how do I execute the set of commands in a loop over as many .psa files as present.
Here's my piece of code testing for only one file in my Prevalidation folder -
Copy-Item C:\Downloads\PreValidation\*.psa C:\Downloads\Validation\WIP
Rename-Item 'C:\Downloads\Validation\WIP\abc 1234.psa' 'psaload1.csv'
Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
<run the psaload.csv to load to my db>
This is what I intend to do -
Consider multiple .psa files in my C:\Downloads\Prevalidation folder.
For each C:\Downloads\PreValidation\*.psa
BEGIN LOOP
Copy-Item C:\Downloads\PreValidation\aaaa.psa C:\Downloads\Validation\WIP\aaaa.psa
Rename-Item 'C:\Downloads\Validation\WIP\aaaa.psa' 'psaload1.csv'
Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
END LOOP
I am looking for the syntax to run these set of commands for each files one by one as present in my /prevalidation folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于所有其他答案都是非常糟糕的代码,并且不是很惯用的 PowerShell,所以这是我的看法(尽管未经测试):
Since all the other answers were quite horrible code and not very idiomatic PowerShell, here is my take (though untested):
您可以将 foreach 与 Get-Item 一起使用来执行循环。 Get-Item 将返回 FileInfo
您可以使用它来获取文件名(和其他信息)的对象。所以你可以做类似的事情:
等等。
You can use foreach with Get-Item to do the loop. Get-Item will return a FileInfo
object that you can use to get the file name (and other info) from. So you could do something like:
etc.
试试这个:
Try this: