用于循环复制和重命名文件的 Powershell 脚本

发布于 2024-12-16 15:03:26 字数 1312 浏览 5 评论 0原文

我的 Prevalidation 文件夹中有许多扩展名为 .psa 的文件,我想要:

  1. 将它们逐个复制到我的工作文件夹中
  2. 将 .psa 文件重命名为 psaload.csv
  3. 针对该文件运行一组命令以将其加载到我的 工作文件夹中db
  4. ,然后从我的工作文件夹中删除 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:

  1. copy them one by one into my working folder
  2. rename the .psa file to psaload.csv
  3. run a set of commands against the file to load it to my db
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘然心甜 2024-12-23 15:03:26

由于所有其他答案都是非常糟糕的代码,并且不是很惯用的 PowerShell,所以这是我的看法(尽管未经测试):

# Get all .psa files
Get-ChildItem C:\Downloads\PreValidation\*.psa |
   ForEach-Object {
      # Load the file's contents, replace commas with spaces
      (Get-Content $_) -replace ',', ' ' |
         # and write it to the correct folder and file name
         Out-File C:\Downloads\WIP\psaload.csv

      # I guess you'd run whatever you're doing against the file here,
      # not after the loop

      Remove-Item C:\Downloads\WIP\psaload.csv
   }

Since all the other answers were quite horrible code and not very idiomatic PowerShell, here is my take (though untested):

# Get all .psa files
Get-ChildItem C:\Downloads\PreValidation\*.psa |
   ForEach-Object {
      # Load the file's contents, replace commas with spaces
      (Get-Content $_) -replace ',', ' ' |
         # and write it to the correct folder and file name
         Out-File C:\Downloads\WIP\psaload.csv

      # I guess you'd run whatever you're doing against the file here,
      # not after the loop

      Remove-Item C:\Downloads\WIP\psaload.csv
   }
如梦亦如幻 2024-12-23 15:03:26

您可以将 foreach 与 Get-Item 一起使用来执行循环。 Get-Item 将返回 FileInfo
您可以使用它来获取文件名(和其他信息)的对象。所以你可以做类似的事情:

foreach($file in (Get-Item .\*.psa))
{
   Copy-Item $file.FullName "C:\Downloads\Validation\WIP\$($file.Name)";
}

等等。

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:

foreach($file in (Get-Item .\*.psa))
{
   Copy-Item $file.FullName "C:\Downloads\Validation\WIP\$($file.Name)";
}

etc.

兔姬 2024-12-23 15:03:26

试试这个:

$a = Get-Item .\*.psa

foreach($file in $a)
{
   Copy-Item $file.FullName "C:\Downloads\Validation\WIP\$($file.Name.replace(".psa",".psload.csv)";

remove-item $file

}

Try this:

$a = Get-Item .\*.psa

foreach($file in $a)
{
   Copy-Item $file.FullName "C:\Downloads\Validation\WIP\$($file.Name.replace(".psa",".psload.csv)";

remove-item $file

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文