<http://www.netikka.net/tsneti/info/tscmd138.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Wed 21-Apr-2010 21:26:34

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



138} How can I get the long name of a short-named file or folder?

Getting the short name of a file or folder is relatively easy with FOR /F and %%~sI but the reverse is more complicated.

Is it possible to cd to short-named dir, but prompt to show full, long name?

The question essentially is, when in a short-named folder, how can one get its long name? The solution below should do this, for XP at least
  @echo off & setlocal enableextensions
  cd C:\DOCUME~1
  for /f %%d in ("%cd%") do set name_=%%~nd
  for /f %%p in ("%cd%") do set path_=%%~dpp
  for /f "tokens=* delims=" %%a in (
    'dir /x ..^|find "%name_%"') do set long_=%%a
  set long_=%long_:~49%
  echo %path_%%long_%
  endlocal & goto :EOF

The output would be
  C:\_M>c:\_d\test\cmdfaq
  C:\Documents and Settings


DIR /X displays the short names generated for non-8dot3 file names.

More generally:
  @echo off & setlocal enableextensions
  echo.
  echo +----------------------------------------------------+
  echo : Get the long name of a short-named folder or file  :
  echo : By Prof. Timo Salmi. Last modified Fri 28-Apr-2006 :
  echo +----------------------------------------------------+
  echo.
  ::
  :: Usage

  if "%~1"=="" (
    echo Usage: %~f0 "ShortFolderOrFileName"
    goto :EOF
    )
  ::
  :: Check that it is about a short name

  echo "%~1"|find "~">nul
  if %errorlevel% GTR 0 (
    echo "%~1" is not a valid short name
    goto :EOF
    )
  ::
  :: Check the existence

  if not exist "%~1" (
    echo "%~1" not found
    goto :EOF
    )
  ::
  :: Get and process the information

  for /f %%d in ("%~1") do set name_=%%~nd
  for /f %%p in ("%~1") do set path_=%%~dpp
  for /f "tokens=* delims=" %%a in (
    'dir /x "%1\.."^|find "%name_%"') do set long_=%%a
  set long_=%long_:~49%
  ::
  :: Show the results

  echo %~1
  echo %path_%%long_%
  endlocal & goto :EOF

The output might be e.g.
  C:\_M>c:\_d\bas\cmdfaq "C:\_D\BAS\KOE2~1.CMD"
  C:\_D\BAS\KOE2~1.CMD
  C:\_D\BAS\KOE 2.CMD

There is an even simple solution to the problem of displaying the long-name format of the current folder as captured below
  C:\DOCUME~1>attrib %cd%
             C:\Documents and Settings

Developed from that we get:
  @echo off & setlocal enableextensions
  if "%~1"=="" (
    set dir_=%cd%
    ) else (
    set dir_=%~1
    )
  ::
  echo %dir_%|findstr /e "\\" > nul
  if %errorlevel% EQU 0 (
    echo Drop the trailing backslash \
    goto :EOF)
  ::
  if not exist "%dir_%\" (
    echo "%dir_%" no such folder
    goto :EOF)
  ::
  for /f "tokens=* delims=" %%d in (
    'attrib "%dir_%"') do set long_=%%d
  set long_=%long_:~11%
  ::
  echo %dir_%
  echo %long_%
  endlocal & goto :EOF

The output might be e.g.
  C:\_M>C:\_D\BAS\CMDFAQ C:\DOCUME~1

  C:\DOCUME~1

  C:\Documents and Settings


The problem with the above solutions is that they work one level at the time only. Thus one has to walk through, which takes some doing. A fullpath solution to the problem is given below
  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  :: Display intermediate information for debugging or not

  set debug=true
  set debug=
  ::
  :: Usage

  if "%~1"=="" (
    echo.
    echo Usage: %~0 [FolderOrFileName]
    goto :EOF
    )
  ::
  :: Give the name of the folder or the file

  set name=%~f1
  if not exist "%name%" (
    echo "%name%" not found
    goto :EOF)
  ::
  :: Parse the path elements

  set count_=0
  set rest_=%name%
  :_loop
  for /f "tokens=1* delims=\" %%a in ("%rest_%") do (
    set /a count_+=1
    set part[!count_!]=%%a
    set rest_=%%b
    if defined rest_ goto _loop
    )
  ::
  :: Display the individual parts

  if defined debug (
    for /l %%i in (1,1,%count_%) do (
      echo part[%%i]=!part[%%i]!
      )
    echo.
    )
  ::
  :: Build up the path

  for /l %%i in (1,1,%count_%) do (
    set longpath_=!longpath_!!part[%%i]!\
    if %%i GTR 1 (
      for /f "tokens=* delims=" %%f in (
        'attrib "!longpath_:~0,-1!"') do (
        set longpath_=%%f
        set longpath_=!longpath_:~11!\
        )
      if defined debug echo !longpath_!
      )
    )
  set longpath_=%longpath_:~0,-1%
  ::
  :: Display the result

  echo The given format was:
  echo %name%
  echo The long format is:
  echo %longpath_%
  ::
  endlocal & goto :EOF

Examples of the output
  C:\_D\TEST>cmdfaq %temp%
  The given format was:
  C:\DOCUME~1\ts\LOCALS~1\Temp
  The long format is:
  C:\Documents and Settings\ts\Local Settings\Temp

  C:\_D\TEST>cmdfaq %temp%\jupdate1.5.0.xml
  The given format was:
  C:\DOCUME~1\ts\LOCALS~1\Temp\jupdate1.5.0.xml
  The long format is:
  C:\Documents and Settings\ts\Local Settings\Temp\jupdate1.5.0.xml

  C:\_D\TEST>cmdfaq C:\DOCUME~1\ts\LOCALS~1
  The given format was:
  C:\DOCUME~1\ts\LOCALS~1
  The long format is:
  C:\Documents and Settings\ts\Local Settings

  C:\_D\TEST>cmdfaq TEST3~1.CMD
  The given format was:
  C:\_D\BAS\TEST3~1.CMD
  The long format is:
  C:\_D\BAS\TEST 3.CMD

There is a non-command-line solution which one can use the check one's results. Enter a SFN path as the Windown explorer address.

It will produce

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Apr 28 2006, 6:01 pm [M]
  Google Groups Feb 7 2007, 3:55 am [M]