chencyu
chencyu

Reputation: 49

dir subtree with regex path string

I have file tree like this:

C:\Users\user\AppData\Local\app-package\blabla
C:\Users\user\AppData\Local\app-package\blabla\something
C:\Users\user\AppData\Local\app-package\app-1.1.6
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources\app.package.unpacked
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources\app.package.unpacked\daemon

I've tried command

D:\tmp>dir /B/S/AD "%LOCALAPPDATA%\app-package\app*"

Expect:

C:\Users\user\AppData\Local\app-package\app-1.1.6
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources\app.package.unpacked
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources\app.package.unpacked\daemon

Real output:

C:\Users\user\AppData\Local\app-package\app-1.1.6
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources\app.package.unpacked

Why it happens and how can I fix it?

Upvotes: 1

Views: 79

Answers (1)

aschipfl
aschipfl

Reputation: 34909

You seem to think that the wildcard app* only matches against items in the given target directory, but actually it matches against the last element of a path. That explains why:

dir /B/S/AD "%LOCALAPPDATA%\app-package\app*"

provides this result:

C:\Users\user\AppData\Local\app-package\app-1.1.6
C:\Users\user\AppData\Local\app-package\app-1.1.6\resources\app.package.unpacked

Change app* to app-* and the output will only be this:

C:\Users\user\AppData\Local\app-package\app-1.1.6

I think what you need is the following:

pushd "%LOCALAPPDATA%\app-package" && (for /F "eol=| delims=" %I in ('dir /B /A:D-H-S "app-*"') do @echo(%~fI& dir /S /B /A:D-H-S "%I" & popd)

To better understand what this does, here is a version with some explanatory comments:

@echo off
rem // Change into the target directory:
pushd "%LOCALAPPDATA%\app-package" && (
    rem // Loop through matching directories within target directory:
    for /F "eol=| delims=" %%I in ('dir /B /A:D-H-S "app-*"') do (
        rem // Return matching sub-directory itself:
        echo(%%~fI
        rem // Return contents of matching sub-directory:
        dir /S /B /A:D-H-S "%%I"
    )
    rem // Return from the target directory:
    popd
)

Upvotes: 1

Related Questions