Convert long filename to short filename (8.3) using cmd.exe

情到浓时终转凉″ 提交于 2019-11-26 15:58:58

问题


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 Settings\User\NTUSER.DAT returns C:\DOCUM~1\USER\NTUSER.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 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 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.


回答1:


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



回答2:


Replace the filename.txt to the filename you want to convert to 8.3

dir /x filename.txt

You will then have to split the result with whitespace as your delimiter (\s in regex). Then the value with the ~ is your short filename. If your filename is short to begin with, then you won't find a string containing a ~.



来源:https://stackoverflow.com/questions/10227144/convert-long-filename-to-short-filename-8-3-using-cmd-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!