powershell-基于另一个文件的存在删除文件

发布于 2025-01-23 04:22:30 字数 1104 浏览 0 评论 0原文

我正在尝试创建一个PowerShell脚本,该脚本只有在与要删除的文件中的另一个文件夹中存在另一个文件时,才会删除特定文件。我现在所有的尝试都毫无结果。这是我正在使用的文件夹结构的示例:

rootfolter

  • client1folder
    • 子文件夹
      • abc1.txt
      • abc1_control.txt
      • def1.txt
      • def1_control.txt
      • ...其他文件
  • client2folder
    • 子文件夹
      • abc2.txt
      • abc2_control.txt
      • ...其他文件
  • client3folder
    • 子文件夹
      • abc3.txt
      • def3.txt
      • ...其他文件(但没有_control.txt文件)
  • client4folder
    • 子文件夹
      • abc4.txt
      • abc4_control.txt
      • ...其他文件

我想要做的是在相应的abc*_control.txt文件时删除abc*.txt文件存在于文件夹中(因此,在此示例中,我希望能够删除abc1.txt,abc2.txt和abc4.txt,,但不是 abc3.txt或def1.txt,def3.txt, ETC)。我也希望能够在完成ABC*.txt文件的删除后删除ABC*_control.txt文件,但这应该很微不足道。

我可以通过在Windows Explorer中进行手动搜索并以这种方式删除它们需要删除我需要删除的文件,但是考虑到此根文件夹中有200多个客户感谢。

I'm trying to create a Powershell script that will delete specific files only when another file exists in the same folder as the file to be deleted. All my attempts up to now have proven fruitless. Here's an example of the folder structure I'm working with:

RootFolder

  • Client1Folder
    • SubFolder
      • abc1.txt
      • abc1_control.txt
      • def1.txt
      • def1_control.txt
      • ... other files
  • Client2Folder
    • SubFolder
      • abc2.txt
      • abc2_control.txt
      • ... other files
  • Client3Folder
    • SubFolder
      • abc3.txt
      • def3.txt
      • ... other files (but no _control.txt files)
  • Client4Folder
    • SubFolder
      • abc4.txt
      • abc4_control.txt
      • ... other files

What I want to be able to do is to delete abc*.txt files when the corresponding abc*_control.txt files exists in the folder (so in this example I want to be able to delete abc1.txt, abc2.txt, and abc4.txt, but not abc3.txt or def1.txt, def3.txt, etc). I also want to be able to delete the abc*_control.txt files after I've completed the deletion of the abc*.txt files, but that should be trivial.

I can find the files I need to delete by doing a manual search in Windows Explorer and deleting them that way, but considering there are over 200 clients in this root folder that I would need to delete the two files for, any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

别把无礼当个性 2025-01-30 04:22:30

您可以使用Get-ChildItem递归找到'*_control.txt'文件,然后从这些文件中构造'其他文件',然后可以删除这些文件:

Get-ChildItem -Path $rootFolder -Filter 'abc*_control.txt' -File -Recurse |
ForEach-Object {
    # construct the full path and name of the 'other file'
    $toDelete = '{0}\{1}{2}' -f $_.DirectoryName, ($_.BaseName -replace '_control

请注意,我已经添加了> -Whatif 切换到remove-item cmdlet。这是一个安全措施,通过该开关,您只会在控制台中看到一条线,告诉您将执行什么动作。实际上没有任何删除。
一旦您满意所有的控制台消息,一切都可以完成预期的事情,就可以删除该 -Whatif Switch。

), $_.Extension if (Test-Path -Path $toDelete -PathType Leaf) { Remove-Item -Path $toDelete -WhatIf # remove the *_control.txt file as well? # $_ | Remove-Item -WhatIf } }

请注意,我已经添加了> -Whatif切换到remove-item cmdlet。这是一个安全措施,通过该开关,您只会在控制台中看到一条线,告诉您将执行什么动作。实际上没有任何删除。
一旦您满意所有的控制台消息,一切都可以完成预期的事情,就可以删除该-Whatif Switch。

You could use Get-ChildItem to recursively find the '*_control.txt' files and from these files construct the 'other file' which you can then delete:

Get-ChildItem -Path $rootFolder -Filter 'abc*_control.txt' -File -Recurse |
ForEach-Object {
    # construct the full path and name of the 'other file'
    $toDelete = '{0}\{1}{2}' -f $_.DirectoryName, ($_.BaseName -replace '_control

Please note that I have added a -WhatIf switch to the Remove-Item cmdlet. This is a safety measure and with that switch, you will only see a line in the console telling you what action would be performed. Nothing actually gets deleted.
Once you are satisfied with all console messages that everything does what is expected, you can remove that -WhatIf switch.

), $_.Extension if (Test-Path -Path $toDelete -PathType Leaf) { Remove-Item -Path $toDelete -WhatIf # remove the *_control.txt file as well? # $_ | Remove-Item -WhatIf } }

Please note that I have added a -WhatIf switch to the Remove-Item cmdlet. This is a safety measure and with that switch, you will only see a line in the console telling you what action would be performed. Nothing actually gets deleted.
Once you are satisfied with all console messages that everything does what is expected, you can remove that -WhatIf switch.

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