powershell-基于另一个文件的存在删除文件
我正在尝试创建一个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
- SubFolder
- Client2Folder
- SubFolder
- abc2.txt
- abc2_control.txt
- ... other files
- SubFolder
- Client3Folder
- SubFolder
- abc3.txt
- def3.txt
- ... other files (but no _control.txt files)
- SubFolder
- Client4Folder
- SubFolder
- abc4.txt
- abc4_control.txt
- ... other files
- SubFolder
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Get-ChildItem
递归找到'*_control.txt'文件,然后从这些文件中构造'其他文件',然后可以删除这些文件:请注意,我已经添加了
> -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: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.