dir command and wildcards for directories in between

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

dir command and wildcards for directories in between

#1 Post by sambasiva » 29 Apr 2014 11:39

Hi All,

I have a requirement to list the ".temp" files and its contents using a particular pattern as below:

C:\test\*\servers\*\data\config\*.temp

does dir command supports to use the patterns directly ?

dir C:\test\*\servers\*\data\config\*.temp ( is not working)


Any suggestions ?

Thanks
SS

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: dir command and wildcards for directories in between

#2 Post by penpen » 29 Apr 2014 13:03

This might work:

Code: Select all

@echo off
for /D %%a in ("C:\test\*") do ^
for /D %%b in ("%%~a\servers\*") do ^
for %%c in ("%%~b\data\config\*.temp") do (
   echo(Processing file: "%%~c"
   for /F "tokens=* usebackq delims=" %%d in ("%%c") do echo(%%d
   echo(
)


penpen

Edit: Bugfixed: Added a \* to the "%%~a\servers" string.
Last edited by penpen on 30 Apr 2014 01:40, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: dir command and wildcards for directories in between

#3 Post by dbenham » 29 Apr 2014 16:41

Code: Select all

dir /b /s /a-d c:\test\*.temp | findstr /ix "c:[\\]test[\\][^\\]*[\\]servers[\\][^\\]*[\\]data[\\]config[\\][^\\]*\.temp"


Dave Benham

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: dir command and wildcards for directories in between

#4 Post by Aacini » 29 Apr 2014 21:07

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in ('dir /b /s /a-d c:\test\*.temp') do (
   set "name=%%a"
   if "!name:\servers\=!" neq "%%a" if "!name:*\data\config\=!" equ "%%~NXa" (
      echo Processing file: "%%a"
   )
)


Previous method may fail if there is another "\server\" name in another place in the path, but it is unlikely that this happen...

Antonio

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: dir command and wildcards for directories in between

#5 Post by sambasiva » 29 Apr 2014 21:56

Thanks for all the replies.
Will try the same and post you the results.

-SS

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: dir command and wildcards for directories in between

#6 Post by sambasiva » 30 Apr 2014 02:46

Hi Antonio,

Thank you.
The below code that you posted has worked for me.

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in ('dir /b /s /a-d c:\test\*.temp') do (
set "name=%%a"
if "!name:\servers\=!" neq "%%a" if "!name:*\data\config\=!" equ "%%~NXa" (
echo Processing file: "%%a"
)
)

Can you please explain what does "if "!name:\servers\=!" neq "%%a"" means ?
what it checks for?

Thanks
SS

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: dir command and wildcards for directories in between

#7 Post by Squashman » 30 Apr 2014 06:26

May help you understand it better if you echo those two variables to the screen to see what they are resolving to.

Code: Select all

echo "!name:\servers\=!" is not equal to "%%a"

Post Reply