Convert long file name to short file name (8.3) with cmd.exe

I am trying to convert a long file name to a short file name (8.3) on Windows.

The batch file with the command line argument works as intended:

short.bat:

@echo OFF echo %~s1 

calling short.bat C:\Documents and Settings\User\NTUSER.DAT returns C:\DOCUM~1\USER\NTUSER.DAT

However, I do not like to have an extra .bat file for this. I would prefer to call cmd.exe whole command from ruby ​​script. How can i do this?

As an intermediate step, I tried hard-coded the path in the batch file, but this does not work:

short1.bat:

 @echo OFF SET filename="C:\Documents and Settings\User\NTUSER.DAT" echo %filename% echo %~sfilename% 

echo %filename% works, but echo %~sfilename% gives the following error:

 The following usage of the path operator in batch-parameter substitution is invalid: %~sfilename% For valid formats type CALL /? or FOR /? 

If short1.bat works, how can I convert it to a single line, which can be called using cmd.exe \c ... ?

Another question arises ( how to get the path to DOS instead of the path to Windows ), however, the one who sets the path to the current directory asks a special question.

+29
command-line windows batch-file short-filenames long-filenames
Apr 19 '12 at 11:35
source share
2 answers
 cmd /c for %A in ("C:\Documents and Settings\User\NTUSER.DAT") do @echo %~sA 
+39
Apr 19 '12 at 12:08
source share
β€” -

Replace filename.txt with the name of the file you want to convert to 8.3

 dir /x filename.txt 

Then you need to split the result into a space as a delimiter (\ s in regex). Then the value with ~ is your short file name. If the name is short for your name, then you will not find a line containing ~.

+5
Apr 19 2018-12-12T00:
source share



All Articles