<http://www.netikka.net/tsneti/info/tscmd077.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Mon 31-May-2010 21:57:47

 
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.



77} How do I list the files in \All Users\Desktop in the short format?

There is no immediate DIR switch to do that. A brief round-about way is the one below. It omits directory entries, hidden and system files. The date, size, and the short-format name of the files are given. It is easily customized.

  @echo off & setlocal enableextensions
  for /f "tokens=*" %%f in (
    'dir "%ALLUSERSPROFILE%\Desktop\*.*" /a:-d-h-s/b/s') do (
      echo %%~tzsf)
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq
  06.11.2003 10:35 1647 C:\DOCUME~1\ALLUSE~1\Desktop\ACCESS~1.LNK
  10.02.2008 21:24 1929 C:\DOCUME~1\ALLUSE~1\Desktop\ADOBER~1.LNK
  12.03.2008 17:21 926 C:\DOCUME~1\ALLUSE~1\Desktop\EDITPA~1.LNK
  06.04.2008 13:23 2137 C:\DOCUME~1\ALLUSE~1\Desktop\iTunes.lnk
  31.05.2007 17:20 1608 C:\DOCUME~1\ALLUSE~1\Desktop\MOZILL~3.LNK
  13.06.2008 19:51 598 C:\DOCUME~1\ALLUSE~1\Desktop\Opera.lnk
  27.01.2006 17:48 1732 C:\DOCUME~1\ALLUSE~1\Desktop\POWERD~1.LNK
  21.03.2008 17:45 903 C:\DOCUME~1\ALLUSE~1\Desktop\REALPL~1.LNK
  28.01.2006 20:56 894 C:\DOCUME~1\ALLUSE~1\Desktop\SNAGIT~1.LNK
  28.01.2006 20:12 2065 C:\DOCUME~1\ALLUSE~1\Desktop\SSHSEC~2.LNK
  28.01.2006 20:12 1105 C:\DOCUME~1\ALLUSE~1\Desktop\SSHSEC~1.LNK
  02.12.2004 22:39 742 C:\DOCUME~1\ALLUSE~1\Desktop\WINZIP~1.LNK


A Visual Basic aided command-line script for a single file
  @echo off & setlocal enableextensions
  if "%~1"=="" (
    echo Usage: %~0 ["LongFileName"]
    goto :EOF)
  ::
  :: Check for the file's existence

  set found_=
  if exist "%~1" if not exist "%~1\" set found_=true
  if not defined found_ (
    echo File "%~1" not found or is a folder
    goto :EOF)
  ::
  :: Build a Visual Basic Script and run it

  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > %vbs_%
  for /f %%a in ('cscript //nologo "%vbs_%" "%~1"') do set sfn_=%%a
  ::
  :: Display the results

  echo %~nx1
  echo %sfn_%
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  'The Visual Basic Script

  WScript.Echo GetShortName(WScript.Arguments.Item(0)) 'VBS
  '
  Function GetShortName(filespec) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFile(filespec) 'VBS
    GetShortName = f.ShortName 'VBS
  End Function 'VBS

The output could be e.g.
  C:\TEST>CMDFAQ.CMD "%ALLUSERSPROFILE%\Desktop\Adobe Reader 9.lnk"
  Adobe Reader 9.lnk
  ADOBER~1.LNK

A related question: In a folder with a long name how does one get its short-format on the command line?
  for /f "tokens=*" %d in ("%cd%") do @echo %~sd

The output could be e.g.
  C:\Documents and Settings\All Users\Documents\My Pictures>for /f "tokens=*" %d in ("%cd%") do @echo %~sd
  C:\DOCUME~1\ALLUSE~1\DOCUME~1\MYPICT~1

A Visual Basic aided command-line script for the short name of a folder
  @echo off & setlocal enableextensions
  if "%~1"=="" (
    echo Usage: %~0 ["LongFolderName"]
    goto :EOF)
  ::
  :: Check for the folder's existence

  set found_=
  if not exist "%~1\" (
    echo Folder "%~1" not found
    goto :EOF)
  ::
  :: Build a Visual Basic Script and run it

  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > %vbs_%
  for /f %%a in ('cscript //nologo "%vbs_%" "%~1"') do set sfn_=%%a
  ::
  :: Display the results
BR>   echo %~1
  echo %sfn_%
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  'The Visual Basic Script

  WScript.Echo GetShortPath(WScript.Arguments.Item(0)) 'VBS
  '
  Function GetShortPath(folderspec) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFolder(folderspec) 'VBS
    GetShortPath = f.ShortPath 'VBS
  End Function 'VBS

The output could be e.g.
  C:\TEST>CMDFAQ.CMD "%ALLUSERSPROFILE%\Desktop"
  C:\Documents and Settings\All Users\Desktop
  C:\DOCUME~1\ALLUSE~1\Desktop

If you in the above substitute (and modify the precaution test etc back to the first VBS solution)
  Set f = fso.GetFolder(folderspec) 'VBS
with
  Set f = fso.GetFile(filespec) 'VBS
then you'll get the entrie filepath in the short format.
E.g.
  C:\TEST>CMDFAQ.CMD "%ALLUSERSPROFILE%\Desktop\Adobe Reader 9.lnk"
  C:\Documents and Settings\All Users\Desktop\Adobe Reader 9.lnk
  C:\DOCUME~1\ALLUSE~1\Desktop\ADOBER~1.LNK

Returning VBS-aided to the original task "How do I list the files in \All Users\Desktop in the short format?":
  @echo off & setlocal enableextensions
  ::
  :: Build a Visual Basic Script

  set vbs_=%temp%\tmp$$$.vbs
  set skip=
  findstr "'%skip%VBS" "%~f0" > %vbs_%
  ::
  :: Browse through the target folder's files

  for /f "tokens=*" %%f in (
    'dir "%ALLUSERSPROFILE%\Desktop\*.*" /a:-d-h-s/b/s') do (
      call :DisplayTheShortPath "%%f")
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  ::
  :DisplayTheShortPath
  for /f %%a in ('cscript //nologo "%vbs_%" "%~1"') do echo %%a
  goto :EOF
  '
  'The Visual Basic Script

  WScript.Echo GetShortPath(WScript.Arguments.Item(0)) 'VBS
  '
  Function GetShortPath(pathspec) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFile(pathspec) 'VBS
    GetShortPath = f.ShortPath 'VBS
  End Function 'VBS

The output could be (on a different PC than the very first example) e.g.
  C:\TEST>CMDFAQ.CMD
  C:\DOCUME~1\ALLUSE~1\Desktop\ADOBER~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\AVIRAA~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\COMODO~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\EDITPA~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\HPSOLU~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\IRFANV~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\Opera.lnk
  C:\DOCUME~1\ALLUSE~1\Desktop\PDFCRE~1.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\PDFILL~2.LNK
  C:\DOCUME~1\ALLUSE~1\Desktop\Winamp.lnk


References/Comments:
  hh ntcmds.chm::/ntcmds_shelloverview.htm
  hh ntcmds.chm::/for.htm
  Google Groups Sun, 30 May 2010 06:57:49
  Google Groups Sun, 30 May 2010 17:22:39 +1000