Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

A batch-file with a command line argument works as intended:

short.bat:

@echo OFF
echo %~s1

calling short.bat C:Documents and SettingsUserNTUSER.DAT returns C:DOCUM~1USERNTUSER.DAT

However, I don't like having an extra .bat-file for this. I would rather call cmd.exe with the whole command from a ruby script. How can I do this?

As an intermediate step I tried to hardcode the path in the batch-file, but that does not work:

short1.bat:

@echo OFF
SET filename="C:Documents and SettingsUserNTUSER.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 this into a one-liner that can be called with cmd.exe c ...?

There is another question (how to get DOS path instead of Windows path), however that one is specifically asking for the path of the current directory.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
631 views
Welcome To Ask or Share your Answers For Others

1 Answer

cmd /c for %A in ("C:Documents and SettingsUserNTUSER.DAT") do @echo %~sA

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...