【问题标题】:Alternative to command line for short path names短路径名的命令行替代方案
【发布时间】:2014-04-15 12:31:42
【问题描述】:

在特定情况下,“带空格的路径”不起作用,我需要获取 Windows 短路径名(例如 Program Files = Progra~1 和 Program Files (x86) = Progra~2)。

这是我现在正在做的事情:

[status, PATHROOT] = dos([ ...
    'for %A in ("' ...
    myPathWithSpace ...
    '") do @echo %~sA' ...
    ]);

现在,我尝试使用 regexpregexprep 格式化文件路径,但在某些情况下无法重现 dos 短名称。那么如何使用 MATLAB 命令重现 dos 命令呢?

这是我对regexpregexprep 的丑陋尝试:

PATHROOT = regexprep(regexprep(regexp(myPathWithSpace,'\w:\\\w*\s\w*\\.*','match'),'\s', ''),'(\w:\\\w{6})\w*','$1~1');

【问题讨论】:

  • 您对regexpregexprep 进行了哪些尝试,遇到了什么问题?
  • @SamRoberts 我添加了我对“regexp”和“regexprep”所做的事情。我没有在原始帖子中发布它,因为它非常丑陋并且没有给出好的短路径名称。

标签: matlab filenames filepath


【解决方案1】:

使用此功能:

function shortPath = getshortpath(longPath)

fs = actxserver('Scripting.FileSystemObject');

shortPath = fs.GetFolder(longPath).ShortPath;

fs.delete;

【讨论】:

  • 这很有趣!我使用tictoc 进行了测试,与dos 命令相比,您的解决方案几乎快了10 倍。你真的可以用 actxserver 替换所有 dos 命令吗?
猜你喜欢
  • 2014-08-29
  • 2018-09-09
  • 2019-10-25
  • 1970-01-01
  • 2019-08-03
  • 2022-01-09
  • 2013-07-13
  • 2018-05-03
相关资源
最近更新 更多