简体   繁体   English

在目录中递归列出不需要的文件 - Powershell

[英]Listing unwanted files recursively in a directory - Powershell

I am trying to make a Powershell script that will display a list of files and folders in a directory('s) while not displaying a specific folder and a file.我正在尝试制作一个 Powershell 脚本,该脚本将显示目录中的文件和文件夹列表,而不显示特定文件夹和文件。

I have a bunch of directories on the C:\\ that have a folder "DATA" and a "Setup.ini" file within them.我在 C:\\ 上有一堆目录,其中有一个文件夹“DATA”和一个“Setup.ini”文件。 Every once in a while people go into the data folder and make copies and then place those copies of files and directories right inside of the main directory with the "DATA" and ".ini" files.每隔一段时间,人们就会进入数据文件夹并制作副本,然后将这些文件和目录的副本与“DATA”和“.ini”文件一起放在主目录中。 Then at the end of each month, I normally need to go through each of these directories and delete anything that is not the "DATA" folder and ".ini" file.然后在每个月末,我通常需要遍历这些目录中的每一个并删除除“DATA”文件夹和“.ini”文件之外的任何内容。

Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini' |  Where-Object { $_.FullName -notmatch 'Data'}

This seems to sort of work and is what I have been using this month.这似乎有点工作,这是我这个月一直在使用的。 It runs really slow.它运行起来真的很慢。 I also noticed that one of the Directories had 2 extra folders and a zip file within it.我还注意到其中一个目录有 2 个额外的文件夹和一个 zip 文件。 The script only showed one of the folders and didn't show the zip file either.该脚本仅显示其中一个文件夹,也未显示 zip 文件。

I have tried adding other things to this code, but when I do that it begins showing me data inside of the "DATA" folder which I don't want.我曾尝试向此代码添加其他内容,但是当我这样做时,它开始向我显示我不想要的“DATA”文件夹内的数据。

Any idea what I could be doing wrong or ways to make this run quicker?知道我可能做错了什么或让这个运行更快的方法吗? I think it is taking about 7-10mins to run through 113 directories.我认为遍历 113 个目录大约需要 7-10 分钟。

Thank you!谢谢!

Expand the -Exclude and drop the Where-Object:展开 -Exclude 并删除 Where-Object:

Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini','Data' 

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM