long filename -> short filename conversion incorrect

H

Han

I've been using the %~sI method of converting a batch file argument from a
long file name (with potential spaces) into a short file name. This has
worked well for ages but I've not run into a problem where a specific
directory path won't convert properly. E.g. If I have the following path:

d:\app\administrator\product\11.1.0\client_1\

Running the following produces the wrong result:

set O=%~s1

Produces d:\app\ADMINI~1\product\111~1.0\client_1nt_1

Notice the extra nt_1? Does anyone know where that comes from and how to
avoid it?
 
P

Pegasus \(MVP\)

Han said:
I've been using the %~sI method of converting a batch file argument from a
long file name (with potential spaces) into a short file name. This has
worked well for ages but I've not run into a problem where a specific
directory path won't convert properly. E.g. If I have the following
path:

d:\app\administrator\product\11.1.0\client_1\

Running the following produces the wrong result:

set O=%~s1

Produces d:\app\ADMINI~1\product\111~1.0\client_1nt_1

Notice the extra nt_1? Does anyone know where that comes from and how to
avoid it?

I think there is a little more to your code than
set O=%~s1
Best to post the whole relevant section.
 
H

Han

Pegasus (MVP) said:
I think there is a little more to your code than
set O=%~s1
Best to post the whole relevant section.

I tried to narrow down the problem to a very simple test case. The entire
batch file in this case is:

shortname.bat:

@set O=%~s1
@echo %O%

If I run
shortname "c:\Program Files\Windows Media Player\Sample Playlists"

I get
c:\PROGRA~1\WINDOW~2\SAMPLE~1

which looks correct.

If I run
shortname "d:\app\administrator\product\11.1.0\client_1"

I get
d:\app\ADMINI~1\product\111~1.0\client_1nt_1

which isn't correct.
 
P

Pegasus \(MVP\)

Han said:
I tried to narrow down the problem to a very simple test case. The entire
batch file in this case is:

shortname.bat:

@set O=%~s1
@echo %O%

The code
set O=%~s1
requires a "for" statement. I cannot see a "for" statement in your code. If
you don't have one then I suggest you describe what you're trying to
achieve.
 
H

Han

Pegasus (MVP) said:
The code
set O=%~s1
requires a "for" statement. I cannot see a "for" statement in your code. If
you don't have one then I suggest you describe what you're trying to
achieve.

I'm kind of surprised at that because I've been using that syntax without
for loops for ages. I never got the impression that they were exclusively
for for loops.

Anyways, what I'm trying to do is convert a given path to its shortname
equivalent. The reason for this is to eliminate the potential for spaces in
paths that have downstream impact on things that can't handle the spaces.
The %~s1 substitution is supposed to replace the first parameter to the batch
file with its shortname equivalent.
 
P

Pegasus \(MVP\)

Han said:
I'm kind of surprised at that because I've been using that syntax without
for loops for ages. I never got the impression that they were exclusively
for for loops.

Anyways, what I'm trying to do is convert a given path to its shortname
equivalent. The reason for this is to eliminate the potential for spaces
in
paths that have downstream impact on things that can't handle the spaces.
The %~s1 substitution is supposed to replace the first parameter to the
batch
file with its shortname equivalent.

It is now becoming clear that you are invoking your batch file like so:
han.bat "%cd%"
It would have been helpful if you had stated this important fact right at
the start to avoid the confusion.

In this particular case you do not need a "for" statement.

I suspect that you found a bug. The following batch file, when invoked with
a parameter as above, illustrates it. It also shows a work-around that
avoids the issue.

If you want to get the opinion of some top-notch batch file experts then I
suggest you repost your question here: alt.msdos.batch.nt, preferably giving
full details about your batch file and how you invoke it.

@echo off
echo The short path is %~s1

pushd "%cd%"
echo Q > "%temp%\debug.scr"
debug < "%temp%\debug.scr" > nul
echo The short path is %cd%
popd
 
P

Pegasus \(MVP\)

Han said:
I'm kind of surprised at that because I've been using that syntax without
for loops for ages. I never got the impression that they were exclusively
for for loops.

Anyways, what I'm trying to do is convert a given path to its shortname
equivalent. The reason for this is to eliminate the potential for spaces
in
paths that have downstream impact on things that can't handle the spaces.
The %~s1 substitution is supposed to replace the first parameter to the
batch
file with its shortname equivalent.

Here is a variant of the previous batch file. It relies on getting the path
as a parameter, same as your batch file.
@echo off
pushd "%1"
echo Q > "%temp%\debug.scr"
debug < "%temp%\debug.scr" > nul
echo The short path is %cd%
popd
 
H

Han

Pegasus (MVP) said:
Here is a variant of the previous batch file. It relies on getting the path
as a parameter, same as your batch file.
@echo off
pushd "%1"
echo Q > "%temp%\debug.scr"
debug < "%temp%\debug.scr" > nul
echo The short path is %cd%
popd

Thanks for the suggestion I'll give it a shot.
 
Joined
Sep 25, 2012
Messages
1
Reaction score
0
Because of the "." in the Folder names., The buffer.Reads it as another extend of buffer.
So the foldername at last of remainins spaces repeats.

13 chars
use mid String to extract last 13 char from main string and save the main string. In ur case 11.1.0
Then it will be Okay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top