List all Directories that do not contain a file with Wildcard Match

可紊 提交于 2019-12-11 02:24:17

问题


I need to be able to list all of the directories in a root folder that do not contain a file which contains the string merged. So for example, if I run the script from the directory C:\out, I will get a list of all the folders inside of C:\out which do not match the wildcard merged.

Example:

Tree:

-c:\out
--c:\out\test123
---sample.meta
---sample_merged.meta
--c:\out\test456
---sample.meta
---sample_merged.meta
--c:\out\test789
---sample.meta
--c:\out\test013
---sample.meta

Output:

test789
test013

回答1:


You can use Get-ChildItem, Where-Object, and Select-String:

Get-ChildItem -Directory |
Where-Object { -not (Get-ChildItem $_ -File -Recurse -Filter *merged*) } |
Select-Object Name, LastWriteTime

Below is an explanation of what each line does:

  1. Get all directories in the current directory.

  2. Set up a filter that searches recursively through each directory for file names that contain the string merged. Only pass on the directory if none are found.

  3. Get the Name and LastWriteTime attributes of each directory.

Note too that you can write it more concisely if you want:

gci -dir | ? { !(gci $_ -file -recur -filter *merged*) } | select name, *w*time



回答2:


Pull a directory listing for the target, then filter against a listing of the target folder to not contain the file you're filtering for.

$BaseDir = "C:\out"
$FilterFile = "sample_merged.meta"
GCI $BaseDir -Directory | ?{!(GCI $_.FullName | Select -Expand Name) -Contains $FilterFile}|Select -Expand Name

That should give you the desired output. Change the last Name to FullName to get the entire path, not just the folder name.




回答3:


For a batch version

@echo off

    setlocal enableextensions
    for /d %%a in ("c:\out\*") do (
        dir /b /a-d "%%~fa\*merged*" >nul 2>nul || echo %%~ta %%~nxa
    )

It simple iterates the folders inside c:\out, and for each of them a dir command for a *merged* file is executed. If the command fails the timestamp and name of folder is echoed to console.



来源:https://stackoverflow.com/questions/24661751/list-all-directories-that-do-not-contain-a-file-with-wildcard-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!