DIR wildcard weirdness

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

DIR wildcard weirdness

#1 Post by Squashman » 30 Jul 2014 12:36

I have 1,011 batch files sitting in a folder.
I have exactly 7 of these batch files that end in 1.bat (clientname1.bat) etc...

Yet look what happens when you try to do a wildcard to find all the files that end in 1.bat
For the sake of brevity I am piping it to show you the count of all the files it finds. Trust me when I say it does list all 996 outputs when I don't pipe it to find.

Code: Select all

C:\Celeris>dir *1.bat /b |find /v /c ""
996

And just for the sake of argument lets try the FOR command. It does the same thing.

Code: Select all

C:\Celeris>@for %G in (*1.bat) do @set /a count+=1
C:\Celeris>echo %count%
996

Lets see what it does with Zero at the end of the file name. I know I have exactly two batch files that end in 0.bat

Code: Select all

C:\Celeris>dir *0.bat /b |find /v /c ""
2


C:\Celeris>@for %G in (*0.bat) do @set /a count+=1

C:\Celeris>set count
count=2

Why does that work?

Lets look at 2.bat. I know I only have 4 batch files that end in 2.bat

Code: Select all

C:\Celeris>dir *2.bat /b |find /v /c ""
16

Again, doesn't make any sense.

Weird thing is if I just put a few files in a test folder everything works out just fine.

Just so everyone doesn't think I am crazy, here is a redacted screenshot of those 7 files. I use a 3rd party file manager that allows you to put in file masks.
Image

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

Re: DIR wildcard weirdness

#2 Post by Squashman » 30 Jul 2014 12:59

Now this next one makes no sense. I purposely renamed every file and prefixed the file name with "ftp site - " and then ran the DIR command. Can't explain why I got 8 files on the output. The last one doesn't even have a 1 in the file name.
Again I have to redact the file names for confidentiality purposes.
Image

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

Re: DIR wildcard weirdness

#3 Post by dbenham » 30 Jul 2014 13:31

Simple answer - short file names.

Try DIR /X *1.BAT and the explanation will be pretty obvious.


Dave Benham

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

Re: DIR wildcard weirdness

#4 Post by Squashman » 30 Jul 2014 13:46

dbenham wrote:Simple answer - short file names.

Try DIR /X *1.BAT and the explanation will be pretty obvious.


Dave Benham

So I am basically screwed with trying to process the batch files I want to process.

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

Re: DIR wildcard weirdness

#5 Post by Squashman » 30 Jul 2014 13:47

Squashman wrote:Now this next one makes no sense. I purposely renamed every file and prefixed the file name with "ftp site - " and then ran the DIR command. Can't explain why I got 8 files on the output. The last one doesn't even have a 1 in the file name.
Again I have to redact the file names for confidentiality purposes.
Image

Per Dave's explanation the last file comes out as FTPSIT~1.BAT.
So it matches.

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

Re: DIR wildcard weirdness

#6 Post by dbenham » 30 Jul 2014 14:07

Squashman wrote:
dbenham wrote:Simple answer - short file names.

Try DIR /X *1.BAT and the explanation will be pretty obvious.


Dave Benham

So I am basically screwed with trying to process the batch files I want to process.

You can use something like:

Code: Select all

for /f "eol=: delims=" %%F in (
  'dir /b /a-d *1.bat^|findstr 1\.bat$'
) do echo %%F


Dave Benham

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

Re: DIR wildcard weirdness

#7 Post by Squashman » 30 Jul 2014 14:12

Thanks Dave. I was trying earlier to pipe the output to FINDSTR but still couldn't get it to work. I was trying to use a Regular Expression in FINDSTR as well and maybe that was screwing it up.

Post Reply