Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

filespec matches both long and short names

40 views
Skip to first unread message

Tom Del Rosso

unread,
Oct 30, 2011, 10:02:23 PM10/30/11
to

I just got bitten by a bug where 'dir *84*.*' for example matched some
unwanted files by their short names, but I only want to match based on the
long name which doesn't contain 84 in the example case.

The simple form of 'for' as in 'for %f in (*84*.*) do' has the same result.

Can you match a filespec to LFN only?

Or do you have to translate the filespec into a regex and use findstr? (Not
sure if that can be a perfect emulation of wildcard matching, and for a
large file list it would be much slower.)


--

Reply in group, but if emailing add one more
zero, and remove the last word.


billious

unread,
Oct 30, 2011, 11:03:36 PM10/30/11
to

"Tom Del Rosso" <td...@verizon.net.invalid> wrote in message
news:j8l01l$ho8$1...@dont-email.me...
AFAIAA, there's no 'lfn-match-only' switch.

And conversion of an afn to a regex is not a simple task. Consider '*' needs
to be converted to '.*', '.' to [.] and '[' to '[[]'(?? - I'm no regex
expert) at the very least...

OTOH, if you had a reliable afn/regex conversion, FINDSTR-ing the matched
names shouldn't be much of a burden - even in a long list. Most of the
filtering would already have been done by the afn.

But we'll see. I'm sure someone will come up with an ingenious solution.


foxidrive

unread,
Oct 31, 2011, 1:22:25 AM10/31/11
to
On 31/10/2011 13:02, Tom Del Rosso wrote:
> I just got bitten by a bug where 'dir *84*.*' for example matched some
> unwanted files by their short names, but I only want to match based on the
> long name which doesn't contain 84 in the example case.
>
> The simple form of 'for' as in 'for %f in (*84*.*) do' has the same result.
>
> Can you match a filespec to LFN only?

You can turn off SFN but that would limit other things.

> Or do you have to translate the filespec into a regex and use findstr?


dir /b |find "84" should also work.







--
Regards,
Mic

Todd Vargo

unread,
Oct 31, 2011, 1:18:51 AM10/31/11
to
Tom Del Rosso wrote:
>
> I just got bitten by a bug where 'dir *84*.*' for example matched some
> unwanted files by their short names, but I only want to match based on the
> long name which doesn't contain 84 in the example case.
>
> The simple form of 'for' as in 'for %f in (*84*.*) do' has the same
> result.
>
> Can you match a filespec to LFN only?
>
> Or do you have to translate the filespec into a regex and use findstr?
> (Not sure if that can be a perfect emulation of wildcard matching, and for
> a large file list it would be much slower.)

Try this code in a batch file.

for %%a in (*84*.*) do (
for /f %%b in ('echo %%~na^|find /i "84"') do echo [%%a]
)


Complex wildcard searches will require additional efforts that may be better
handled using a HLL like vbscript.

Tom Del Rosso

unread,
Oct 31, 2011, 2:07:23 AM10/31/11
to

Todd Vargo wrote:
>
> Try this code in a batch file.
>
> for %%a in (*84*.*) do (
> for /f %%b in ('echo %%~na^|find /i "84"') do echo [%%a]
> )
>
>
> Complex wildcard searches will require additional efforts that may be
> better handled using a HLL like vbscript.

Yes, find is easy, but complex filespecs are the reason I thought of findstr
and regex. I don't have much faith in MS utils when it comes to complex
functions so maybe perl would be better.

I have an alternative solution that bypasses the whole issue, but it would
be nice to add an answer to the bag of tricks.

Herbert Kleebauer

unread,
Oct 31, 2011, 10:11:00 AM10/31/11
to
On 31.10.2011 03:02, Tom Del Rosso wrote:
> I just got bitten by a bug where 'dir *84*.*' for example matched some
> unwanted files by their short names, but I only want to match based on the
> long name which doesn't contain 84 in the example case.
>
> The simple form of 'for' as in 'for %f in (*84*.*) do' has the same result.
>
> Can you match a filespec to LFN only?

If it is only one type of file, maybe .txt, you can use:

ren *.txt *.txtt
for %f in (*84*.txtt) do
ren *.txtt *.txt

Tom Del Rosso

unread,
Oct 31, 2011, 2:15:50 PM10/31/11
to

Herbert Kleebauer wrote:
>
> If it is only one type of file, maybe .txt, you can use:
>
> ren *.txt *.txtt
> for %f in (*84*.txtt) do
> ren *.txtt *.txt

That's interesting but it just creates a bunch of new, unpredictable, SFNs.
They might still have unwanted filespec matches.

Timo Salmi

unread,
Oct 31, 2011, 3:11:08 PM10/31/11
to
On 31.10.2011 04:02 Tom Del Rosso wrote:
> I just got bitten by a bug where 'dir *84*.*' for example matched some
> unwanted files by their short names, but I only want to match based on the
> long name which doesn't contain 84 in the example case.

Tom, it is difficult to address the problem, if one can't test it hands-on.

Please don't get this wrong. As usual, I am probably being dense, but
your problem documentation minimalistic and thus the situation remains
vague. Therefore this leads to speculations. Give some example lists of
file names (with their sfn and lfn renditions) which [a] match correctly
[b] match incorrectly in your method.

> Can you match a filespec to LFN only?

If one can list files in an lfn-format only (which one can), then
obviously yes. How easily or not, is another matter, as you note yourself.

P.S. have you tried through the output of ATTRIB

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Herbert Kleebauer

unread,
Oct 31, 2011, 4:26:13 PM10/31/11
to
On 31.10.2011 19:15, Tom Del Rosso wrote:
> Herbert Kleebauer wrote:
>>
>> If it is only one type of file, maybe .txt, you can use:
>>
>> ren *.txt *.txtt
>> for %f in (*84*.txtt) do
>> ren *.txtt *.txt
>
> That's interesting but it just creates a bunch of new, unpredictable, SFNs.
> They might still have unwanted filespec matches.

Short filenames can only have a 3 character extension, so
*84*.txtt can never conflict with a short filename.


Tom Del Rosso

unread,
Oct 31, 2011, 4:55:40 PM10/31/11
to
I see. That should be a good solution for the general case.

Even with more than one file type, it just means that changing the names
back is a little more work, but can be done with arbitrary extensions.

Todd Vargo

unread,
Oct 31, 2011, 5:29:15 PM10/31/11
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:4eaef2cd$0$3062$9188...@news.netikka.fi...
> On 31.10.2011 04:02 Tom Del Rosso wrote:
>> I just got bitten by a bug where 'dir *84*.*' for example matched some
>> unwanted files by their short names, but I only want to match based on
>> the long name which doesn't contain 84 in the example case.
>
> Tom, it is difficult to address the problem, if one can't test it
> hands-on.
>
> Please don't get this wrong. As usual, I am probably being dense, but
> your problem documentation minimalistic and thus the situation remains
> vague. Therefore this leads to speculations. Give some example lists of
> file names (with their sfn and lfn renditions) which [a] match correctly
> [b] match incorrectly in your method.
>
>> Can you match a filespec to LFN only?
>
> If one can list files in an lfn-format only (which one can), then
> obviously yes. How easily or not, is another matter, as you note yourself.
>
> P.S. have you tried through the output of ATTRIB

My tests indicate even ATTRIB catches both SFN and LFN. Perhaps with a bit
more interest, one might create a generic filter using WSH. That would make
a nice addition to your help archive.

Todd Vargo

unread,
Oct 31, 2011, 5:51:11 PM10/31/11
to
Tom Del Rosso wrote:
> Todd Vargo wrote:
>>
>> Try this code in a batch file.
>>
>> for %%a in (*84*.*) do (
>> for /f %%b in ('echo %%~na^|find /i "84"') do echo [%%a]
>> )
>>
>>
>> Complex wildcard searches will require additional efforts that may be
>> better handled using a HLL like vbscript.
>
> Yes, find is easy, but complex filespecs are the reason I thought of
> findstr and regex. I don't have much faith in MS utils when it comes to
> complex functions so maybe perl would be better.
>
> I have an alternative solution that bypasses the whole issue, but it would
> be nice to add an answer to the bag of tricks.

This is not a new issue by any means. Microsoft provided LFNFOR in
Win95/98/ME but for whatever reason failed to include it in any of the NT
based versions (bad Microsoft). Perhaps one could solve it with Perl. OTOH,
such a solution would not be portable as solving it with WSH would be.

Tom Del Rosso

unread,
Oct 31, 2011, 5:57:04 PM10/31/11
to

Timo Salmi wrote:
> On 31.10.2011 04:02 Tom Del Rosso wrote:
> > I just got bitten by a bug where 'dir *84*.*' for example matched
> > some unwanted files by their short names, but I only want to match
> > based on the long name which doesn't contain 84 in the example case.
>
> Tom, it is difficult to address the problem, if one can't test it
> hands-on.
> Please don't get this wrong. As usual, I am probably being dense, but
> your problem documentation minimalistic and thus the situation remains
> vague. Therefore this leads to speculations. Give some example lists
> of file names (with their sfn and lfn renditions) which [a] match
> correctly [b] match incorrectly in your method.

Since I worked around it for the immediate problem, I am just looking for a
general solution.

But if it helps, suppose you are searching for 84 and you get a file with 86
just because it has 84 in the SFN. Several matches like that came up for
one search.

SFN: EN8464~1.ZIP
LFN: Eng [s02 1986].zip

Obviously I could fix it in this case by adding more characters to the
filespec, so this specific case is not what I'm asking about. I just wanted
to know if there is a general way to match filespecs to only the LFN in the
future.

At first I was very surprised to see the unwanted matches. It's not
something you expect to happen. It could happen by surprise in any other
situation where you use wildcards, possibly causing modification to the
wrong files. It's not likely we would always be careful to use extra
characters, as I was able to use "]" in this case, so to avoid an accident
it would be good if there was a way to ignore the SFN.

Turning off SFN support might even be a good idea now that we have VM's for
DOS programs, and can't even use them without a VM any more.


> P.S. have you tried through the output of ATTRIB

How can that help? It gives text to search but so does dir.

Tom Del Rosso

unread,
Oct 31, 2011, 6:30:03 PM10/31/11
to

Todd Vargo wrote:
> This is not a new issue by any means. Microsoft provided LFNFOR in

Oh yeah! I forgot about LFNFOR.


> Win95/98/ME but for whatever reason failed to include it in any of
> the NT based versions (bad Microsoft).

DIE Microsoft DIE!!!

I mean, really. It was obvious to the OS/2 people at IBM, when they added
LFN support, that they simply had to make files with an LFN invisible to DOS
programs. That was the right solution. SFN's shouldn't exist. They were
rarely even usable from a DOS app.

Yet another case where they screw things up is when a file is specified in
the registry by its SFN, which might change from ~1 to ~2. It's such a
stupid poorly thought-out system.


> Perhaps one could solve it
> with Perl. OTOH, such a solution would not be portable as solving it
> with WSH would be.

Yeah, or powershell maybe.

Todd Vargo

unread,
Oct 31, 2011, 6:36:20 PM10/31/11
to

"Tom Del Rosso" <td...@verizon.net.invalid> wrote in message
news:j8mold$cfc$1...@dont-email.me...
>
> Herbert Kleebauer wrote:
>>
>> If it is only one type of file, maybe .txt, you can use:
>>
>> ren *.txt *.txtt
>> for %f in (*84*.txtt) do
>> ren *.txtt *.txt
>
> That's interesting but it just creates a bunch of new, unpredictable,
> SFNs. They might still have unwanted filespec matches.

This will avoid changing the original SFNs on you.

@echo off
md tmp$
for %%a in ("*.txt") do type nul>tmp$\%%a
ren tmp$\*.txt *.txtt
ren tmp$\*84.txtt *.txt
del tmp$\*.txtt
for /f "delims=" %%a in ('dir /b tmp$\*') do echo %%a
rd /s/q tmp$

Try it with some complex searches in the line containing the 84.

Unfortunately, there is no easy way to work with just the LFNs.

Todd Vargo

unread,
Oct 31, 2011, 7:14:36 PM10/31/11
to
The following vbscript seems to work to exclude the unwanted SFNs.

http://www.source-code.biz/snippets/vbscript/1.htm

To list files matching *84.* invoke it as follows.

cscript /nologo ListDir.vbs *84.*


foxidrive

unread,
Oct 31, 2011, 8:59:42 PM10/31/11
to
On 1/11/2011 08:57, Tom Del Rosso wrote:
> Timo Salmi wrote:
>> On 31.10.2011 04:02 Tom Del Rosso wrote:
>>> I just got bitten by a bug where 'dir *84*.*' for example matched
>>> some unwanted files by their short names, but I only want to match
>>> based on the long name which doesn't contain 84 in the example case.
>>
>
> Since I worked around it for the immediate problem, I am just looking for a
> general solution.
>
> But if it helps, suppose you are searching for 84 and you get a file with 86
> just because it has 84 in the SFN. Several matches like that came up for
> one search.
>
> SFN: EN8464~1.ZIP
> LFN: Eng [s02 1986].zip

dir /b |find "84"

will find only the right filenames because find will reject the long filenames that don't contain 84.




--
Regards,
Mic

Tom Del Rosso

unread,
Oct 31, 2011, 9:09:22 PM10/31/11
to

foxidrive wrote:
>
> dir /b |find "84"
>
> will find only the right filenames because find will reject the long
> filenames that don't contain 84.

Well that was one example. I had to do other filespec matches with more
than one string separated by a question mark. That didn't get false
matches, but it could have. For future reference I wanted to know a method
to do wildcard matching with it's normal syntax but without matching SFN's.

foxidrive

unread,
Oct 31, 2011, 9:36:00 PM10/31/11
to
On 1/11/2011 12:09, Tom Del Rosso wrote:
> foxidrive wrote:
>>
>> dir /b |find "84"
>>
>> will find only the right filenames because find will reject the long
>> filenames that don't contain 84.
>
> Well that was one example. I had to do other filespec matches with more
> than one string separated by a question mark. That didn't get false
> matches, but it could have. For future reference I wanted to know a method
> to do wildcard matching with it's normal syntax but without matching SFN's.

Todd's suggestion of WHS?


--
Regards,
Mic

Tom Del Rosso

unread,
Nov 1, 2011, 3:07:46 AM11/1/11
to

foxidrive wrote:
>
> Todd's suggestion of WHS?

I think it's good. I won't have time to test it until tomorrow night.

Tom Del Rosso

unread,
Nov 1, 2011, 3:08:35 AM11/1/11
to

Todd Vargo wrote:
>
> The following vbscript seems to work to exclude the unwanted SFNs.
>
> http://www.source-code.biz/snippets/vbscript/1.htm

That's great. I will test it tomorrow.

Timo Salmi

unread,
Nov 3, 2011, 5:16:59 AM11/3/11
to
On 31.10.2011 23:57 Tom Del Rosso wrote:
> Since I worked around it for the immediate problem, I am just looking for a
> general solution.
> But if it helps, suppose you are searching for 84 and you get a file with 86
> just because it has 84 in the SFN. Several matches like that came up for
> one search.
>
> SFN: EN8464~1.ZIP
> LFN: Eng [s02 1986].zip

Right, is see. To make things worse the problem naturally extends to
other commands than dir. Including copy. That's quite annoying and
potentially dangerous.

> Obviously I could fix it in this case by adding more characters to the
> filespec, so this specific case is not what I'm asking about. I just wanted
> to know if there is a general way to match filespecs to only the LFN in the
> future.

Haven't come upon that kind of a solution. Todd had a WBS reference,
though, but that one is at least moderately complicated. Hence one is
most naturally reduced to solutions that avoid using wildcards.
Typically scripts that utilize a (long) list of explicit file names as
an argument for a FOR. First perhaps utilizing e.g. the regular
expressions discussed to generate the list.

Timo Salmi

unread,
Nov 4, 2011, 9:50:32 AM11/4/11
to
On 31.10.2011 23:29 Todd Vargo wrote:
> "Timo Salmi" <t...@uwasa.fi> wrote in message
(snip)

> My tests indicate even ATTRIB catches both SFN and LFN. Perhaps with a
> bit more interest, one might create a generic filter using WSH. That
> would make a nice addition to your help archive.

I wrote a brief item describing the problem and a solution based on
the FINDSTR usage

185} How to get DIR wildcards match long file names only?
http://www.netikka.net/tsneti/info/tscmd185.php

All the best, Timo

--
Prof. Timo Salmi, Vaasa, Finland
0 new messages