batch-file - 如何使用cmd替换部分路径?

标签 batch-file cmd sublimetext3

嘿 stackoverflow 社区,
我尝试在 Sublime Text 3 中为 xampp/htdocs 目录中的 .html 文件设置构建系统。

因此我需要替换部分文件路径并添加
“http://localhost/”
开头,然后在浏览器中调用它。欢迎提出任何建议。

到目前为止我从其他线程中发现了什么:

SETLOCAL ENABLEDELAYEDEXPANSION
-- represtened by the $file variable in Sublimetext 3 - filepath would look like "${file}" --
SET filepath="C:\xampp\htdocs\test.html" 
SET searchstring="C:\xampp\htdocs\"
SET replacestring="http://localhost/"
SET modifiedpath=%searchstring%=%replacestring%!

-- not sure about this one but it should work as it does for hardcoded sites --
"C:\Program Files\Opera\launcher.exe" !modifiedpath!

我从这些命令中得到的结果只是我输入的在浏览器中打开的第一个文件路径。

我做错了什么?我找不到任何有关使用 cmd 操作路径的主题。 系统 = W7/32 位和 W8/64 位结果相同

提前致谢, 彼得

最佳答案

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: -- represtened by the $file variable in Sublimetext 3 - filepath would look like "${file}" --
SET "filepath=C:\xampp\htdocs\test.html" 
SET "searchstring=C:\xampp\htdocs\"
SET "replacestring=http://localhost/"
CALL SET "modifiedpath=%%filepath:%searchstring%=%replacestring%%%"
:: Belt-and-braces
SET "modifiedpath=%modifiedpath:\=/%"

:: -- not sure about this one but it should work as it does for hardcoded sites --
ECHO "C:\Program Files\Opera\launcher.exe" "%modifiedpath%"

GOTO :EOF

这应该可以工作 - 实际上这里不需要 enabledelayeexpansion

不确定 Opera - 可执行文件名称周围的引号是必需的,但参数可能是,也可能不是。由于我还没有安装它,所以我只是对其进行了 ECHO 编辑 - 您需要删除 ECHO 才能使其正常工作。事实上,CMD 将仅显示一个应调用 Opera 的命令行。

关于batch-file - 如何使用cmd替换部分路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199316/

相关文章:

linux - 如何在 git pull 命令中输入密码?

windows - 如何||和 && 使用括号括起来的复合命令?

javascript - Sublime Text 中的 node.js 输出中的输出被 chop

sublimetext3 - 如何更改 Sublime Text 3 侧边栏的字体?

c++ - 使用 MinGW 的非标准位置构建 Boost 1.52

java - System.exit(int code) 和 Runtime.getRuntime().exit(int code) 的区别

batch-file - 批处理 : Simply Syntax Error : Goto was unexpected at this time

php - 替换 sublime text 中类和第一个函数之间所有出现的 $ 符号

php exec() 挂起

windows - Windows cmd 中的操作完成警报