You are not logged in.

#1 12 Aug 2006 10:06

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Short Name Directory converter

Hi, My name is Marcus.

I've been doing a lot of batch scripting here lately. And I've came up with a script that will convert a path to a directory that has long names to their short 8 character names. I'd figure this script would be fairly useful for batch programmers that are doing heavy searching for files in their own batch scripts. But I would like to know, especially on this kind of forum, of what you guys think of my batch script.

Hopefully this script will help many, but feel free to post your comments about the script.  Anything you think needs to be changed or modified, or what can be done faster I would be more than glad to here about. Rate my batch script.

short_dirnames.txt

Thanks!

Offline

#2 12 Aug 2006 12:31

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Short Name Directory converter

Interesting stuff, my first thought is what if the creation of short names has been disabled with
"FSUTIL behavior set disable8dot3 1"

You can check the current setting of this with "FSUTIL behavior query disable8dot3 1"

Offline

#3 12 Aug 2006 14:35

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Short Name Directory converter

I tried out FSUTIL and it really didn't do much as far as getting information from directories.

My main focus of the script was to get the short name of the directory name if it exists so you could properely access a file or an application without using quotes.

Currently I'm writing a script called TRONRez, a batch application that helps you setup the tools you need to edit the game TRON 2.0, and I found out that for some reason I couldn't quote a path to an application file from the use of the for /F "options" %%parameter in ('command'). This is what lead to me to actually writing this script.

I could of sworn I did it before, but for some reason I get invalid command errors if I do quote the full path to that particular application file.

This way, when you try to access a file, you'll know it will always be under that specified path.

Offline

#4 12 Aug 2006 14:48

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Short Name Directory converter

To call a command using FOR via a long pathname you need the usebackq option

FOR /F "usebackq" %%G IN (`c:\program files\some command.exe`) DO ECHO %%G

Offline

#5 12 Aug 2006 16:18

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Short Name Directory converter

Using the backquote is not entirely necessary, unless some how LNFs would work in Windows 2000 for some strange reason, but probably not so. Plus if you wanted to use LNFs for the path to the command in the FOR-Files loop, you would have to quote the path. But the thing is, you can only use one set of quotes within the single or backquoted barrier, even if you escape the quotes by using the carrot character. So if you were to use a command that had to reference another path that also has LNFs, one of the paths would have to use short names, or both to where you wouldn't even have to use quotes.

Offline

#6 12 Aug 2006 21:49

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Short Name Directory converter

OK sorry the right syntax to get long filenames to work is this

FOR /F "usebackq" %%G IN (`"c:\program files\some command.exe"`) DO ECHO %%G

the back quotes ` tell FOR that this is the file/command we want to run
the normal double quotes " delimit the long filename w/ spaces

so for example you can add parameters like so
FOR /F "usebackq" %%G IN (`"c:\program files\some command.exe" /?`) DO ECHO %%G

That said I wouldnt be surprised if there are exceptions where this fails to work, I tested this in XP sp2

Offline

#7 13 Aug 2006 00:48

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Short Name Directory converter

My main system is also running XP sp2, but I don't see where using backquotes actually helps. Because if you leave out backquotes like this:

FOR /F %%g in ('"C:\Program Files\command.exe"') do echo.%%g

The application in in the Program Files folder will still execute. I'm confused about the benefits of backquotes.

What you cannot do, regardless of what type of single quotes you use for the barrier, you cannot use two sets of double quotes.

Valid

FOR /F %%g in ('"C:\Program Files\command.exe" -parameter value') do echo.%%g

and

FOR /F %%g in ('^"C:\Program Files\command.exe^" -parameter value') do echo.%%g

Invalid

FOR /F %%g in ('"C:\Program Files\command.exe" -parameter "value"') do echo.%%g

and

FOR /F %%g in ('^"C:\Program Files\command.exe^" -parameter ^"value^"') do echo.%%g

and even

FOR /F %%g in ('"C:\Program Files\command.exe" -parameter ^"value^"') do echo.%%g

Offline

#8 13 Aug 2006 13:53

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Short Name Directory converter

Yes I see what you mean now - usebackq doesnt make any difference, for a file-set usebackq would allow "long file 1" "long file 2"... but for executing a command it doesnt help.

What i would suggest is a temporary file for the command

set _prog="C:\Program Files\command.exe" -parameter "value"
echo %_prog%>%temp%\mycommand.cmd

FOR /F %%g in ('%temp%\mycommand.cmd') do echo.%%g
DEL %temp%\mycommand.cmd

unless anyone else has a better idea?

Offline

#9 13 Aug 2006 14:45

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Short Name Directory converter

Yeah that works really good actually, I was not familiar with cmd files.

Less code, and it only needs to be done when you're in need of using the output of a file in FOR /F loop.

Do cmd files work in Win9x as well?

Offline

#10 13 Aug 2006 15:03

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Short Name Directory converter

The distinction between BAT and CMD files is simply that win95/98 wont attempt run a CMD, which can be a good thing if it's full of NT/XP specific syntax. I havent used Win9x since about 1996 so I'm in the habit of writing everything as a cmd

Offline

#11 07 Sep 2006 13:51

//[T.0.P]//
Member
Registered: 12 Aug 2006
Posts: 50

Re: Short Name Directory converter

Hey Simon, check this out:

for %%g in ("C:\Program Files") do echo. %%~fsg
echo. C:\PROGRA~1

Durh! LoL. Oh well live and learn.

Offline

#12 22 Mar 2007 23:01

NDog
Member
From: New Zealand
Registered: 31 May 2006
Posts: 121
Website

Re: Short Name Directory converter

nice!


cmd, vbs, ps, bash
autoit, python, swift

Offline

Board footer

Powered by