Wildcard during directory lookup produces unexpected results

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Wildcard during directory lookup produces unexpected results

#1 Post by Ed Dyreen » 12 Nov 2018 09:11

I have several files in my folder namely

a.cmd
b.cmd
ab.cmd
getFullPathFile_.cmd
getFullPbathFile_.CMD

Now trying to find anything like *a.cmd

Code: Select all

@echo off

echo.
for /R %%? in ("*a.CMD") do echo(%%?

echo.
dir/B/S/A-D "*a.CMD"

echo.
for %%? in ("*a.CMD") do echo(%%?

pause
exit /b
expecting return a.cmd

but I get

Code: Select all

...\getFullPathFile_.CMD
...\a.cmd

...\getFullPathFile_.CMD
...\a.cmd

getFullPathFile_.CMD
a.cmd
Druk op een toets om door te gaan. . .
That was unexpected that *a.cmd appears to match with *athFile_.CMD ??

even more unexpected is that getFullPbathFile_.CMD does not show up ??

I thought at first, maybe some path issue but it also happens on server2003 not only XP

I moved the test folder to drive C and it works as expected but the moment I move the folder to a mapped network drive which points to a NAS at W: this happens. This NAS is running linux with windows sharing. When I move it to another NAS at Y: ( same model NAS ) this also happens

Yet when I move to a mapped drive on another pc it again works as expected. And when I move to yet another NAS at U: ( This is a different older model also running linux with windows sharing. ) it again works as expected.

What I do know is that these NASses support characters that windows does not, but that can't be it because the old NAS supports these too yet it does not show this issue.

Ifail to understand why this is happening.

Sponge Belly
Posts: 216
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Wildcard during directory lookup produces unexpected results

#2 Post by Sponge Belly » 21 Nov 2018 09:12

Hi Ed,

Is it a for /r bug? I remember reading somewhere that for /r can generate erroneous output under certain conditions. As usual, I can’t remember where I read it, sorry. :(

Maybe someone with a better memory (and a more organised bookmarks collection) can help you out. ;)

Good luck!

- SB

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Wildcard during directory lookup produces unexpected results

#3 Post by Ed Dyreen » 21 Nov 2018 10:49

Sponge Belly wrote:
21 Nov 2018 09:12
Hi Ed,

Is it a for /r bug? I remember reading somewhere that for /r can generate erroneous output under certain conditions. As usual, I can’t remember where I read it, sorry. :(

Maybe someone with a better memory (and a more organised bookmarks collection) can help you out. ;)

Good luck!

- SB
The 'XP FOR /F' bug I believe you are talking about, produces textual artifacts that do not affect the batch operation itself.
☺?♥♠a♀???☻?☻ for weirdness ♠☺??↨??$error = 7 niet vinden.

This one does returns wrong results and I observe the same behavior when issuing the dir command.

It only happens using one specific brand of NAS namely the NSA320S from ZyXEL.
If I issue the command on a 10 year old NAS from Plextor it behaves as expected.

It seems not to matter whether I use XP or server2003.
could this be a NAS firmware issue ?

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

Re: Wildcard during directory lookup produces unexpected results

#4 Post by penpen » 21 Nov 2018 18:29

Are you sure it's not the "short name"-issue - what does "dir/x" say?

Code: Select all

dir/X/S/A-D "*a.CMD"
penpen

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Wildcard during directory lookup produces unexpected results

#5 Post by Ed Dyreen » 21 Nov 2018 20:57

penpen wrote:
21 Nov 2018 18:29
Are you sure it's not the "short name"-issue - what does "dir/x" say?

Code: Select all

dir/X/S/A-D "*a.CMD"
penpen
I create a folder named "test". inside i create 2 control files "a.cmd" and "ba.CMD" and some false positives not just any name, only some names pop up for some reason so in my case "getFullPathFile_.cmd" and "$filetouch.cmd"

tst.cmd contains

Code: Select all

@echo off

dir/X/S/A-D "*a.CMD"

for /R %%? in ("*a.CMD") do echo.forR=%%?_

pause
exit /b
I move the folder to desktop and run test.cmd which gives

Code: Select all

 De volumenaam van station E is home
 Het volumenummer is 65C7-ABF4

 Map van E:\ADMIN\VIP\MSENV\Bureaublad\test

12/11/2018  15:59                 0              a.cmd
12/11/2018  15:59                 0              ba.cmd
               2 bestand(en)                0 bytes

     Totaal aantal weergegeven bestanden:
               2 bestand(en)                0 bytes
               0 map(pen)   6.990.938.112 bytes beschikbaar
forR=E:\ADMIN\VIP\MSENV\Bureaublad\test\a.cmd_
forR=E:\ADMIN\VIP\MSENV\Bureaublad\test\ba.cmd_
Druk op een toets om door te gaan. . .
I move the folder to Plextor NAS and run test.cmd which gives

Code: Select all

 De volumenaam van station T is ED-XTRA-0
 Het volumenummer is 55B8-8500

 Map van T:\test

12/11/2018  15:59                 0              a.cmd
12/11/2018  15:59                 0              ba.cmd
               2 bestand(en)                0 bytes

     Totaal aantal weergegeven bestanden:
               2 bestand(en)                0 bytes
               0 map(pen)  326.354.075.648 bytes beschikbaar
forR=T:\test\a.cmd_
forR=T:\test\ba.cmd_
Druk op een toets om door te gaan. . .
I move the folder to ZyXEL NAS and run test.cmd which gives

Code: Select all

 De volumenaam van station W is ed-serv-0
 Het volumenummer is 3091-3091

 Map van W:\test

12/11/2018  15:59                 0              ba.cmd
12/11/2018  16:01                 0 G7B7HF~A.CMD getFullPathFile_.CMD
12/11/2018  15:59                 0              a.cmd
13/11/2018  02:15                 0 $KLZMG~A.CMD $fileTouch.CMD
               4 bestand(en)                0 bytes

     Totaal aantal weergegeven bestanden:
               4 bestand(en)                0 bytes
               0 map(pen)  776.869.900.288 bytes beschikbaar
forR=W:\test\ba.cmd_
forR=W:\test\getFullPathFile_.CMD_
forR=W:\test\a.cmd_
forR=W:\test\$fileTouch.CMD_
Druk op een toets om door te gaan. . .
G7B7HF~A.CMD getFullPathFile_.CMD

G7B7HF~A.CMD appears to be the shortname for getFullPathFile_.CMD.

You hit the culprit penpen :)

When reading the comment at the /X switch one sentence is striking:

/X ... short names that are not available are displayed as spaces...

but this isn't DOS why would I need 8.3 filename support at all ? But more importantly how do i disable or suppress them ?

I just learned short names are a huge security risk, especially if you run server side software like Apache or IIS.
https://www.acunetix.com/blog/articles/ ... y-problem/

It talks about disables 8.3 file name support using the registry. But my scripts are to be run on other peoples machines.
And messing with other peoples machine settings is out of the question.

The /N switch doesn't do anything, it just hides the short names but still displays them in the query. And what switch would I use when I issue a for command ?

This is not just bad news for me but for anyone supporting 8.3 which is most people. This means DIR and FOR are unreliable per definition if not careful thought is being put into this :!:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Wildcard during directory lookup produces unexpected results

#6 Post by Ed Dyreen » 21 Nov 2018 22:21

Code: Select all

@echo off

for /F skip^=4^ delims^=^ eol^= %%? in ('dir /X /A-D "*a.CMD"') do (

	echo.%%?
	set "$=%%?"
	setlocal enableDelayedExpansion
	set "$=!$:*a.CMD=!"
	if not defined $ ( echo. longName hit on: '%%?' )
	endlocal
)

pause
exit /b
What disturbs me the most about the above solution is the skip command, it's in there because the /B switch also doesn't work if used in conjunction with /N /W or /X

Code: Select all

12/11/2018  15:59                 0              ba.cmd
 longName hit on: '12/11/2018  15:59                 0              ba.cmd'
12/11/2018  16:01                 0 G7B7HF~A.CMD getFullPathFile_.CMD
12/11/2018  15:59                 0              a.cmd
 longName hit on: '12/11/2018  15:59                 0              a.cmd'
13/11/2018  02:15                 0 $KLZMG~A.CMD $fileTouch.CMD
               4 bestand(en)                0 bytes
               0 map(pen)  776.868.945.920 bytes beschikbaar
Druk op een toets om door te gaan. . .
Maybe someone knows a better way ( not by disabling 8.3 support in registry ) ?

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

Re: Wildcard during directory lookup produces unexpected results

#7 Post by penpen » 25 Nov 2018 17:09

You possibly could use findstr as a workaround:

Code: Select all

dir /b | findstr "^.*a[.]CMD$"
penpen

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Wildcard during directory lookup produces unexpected results

#8 Post by Ed Dyreen » 25 Nov 2018 18:19

penpen wrote:
25 Nov 2018 17:09
You possibly could use findstr as a workaround:
good idea

Code: Select all

@echo off

set "file=*.cmd"
set "rExp=.*\\.*\.cmd"

setlocal disableDelayedExpansion
for /R "%~dp0" %%? in ("*.*") do echo(%%?|findstr.EXE /I "^%rExp%$"

Post Reply