[英]Batch file; Replace variable's spaces with hyphens
我已经开始了另一个小批量项目,但是我遇到了一个两难的问题。 我使用set name=
后跟set /p name=
,以允许用户提供所需的输入。 但是,我希望用空格输入的变量然后用连字符替换空格(例如“hello world”变成“hello-world” )。
我一直在搜索并发现许多情况,其中有人问类似的问题,但他们都使用ren
命令,由于不处理文件,我没有用。
我目前的代码如下:
@echo off
Cls
echo Insert a string with spaces?
set string=
set /p string=
我还找到了重命名使用ren "!file!" "!file:_= !"
提到的文件的解决方案ren "!file!" "!file:_= !"
ren "!file!" "!file:_= !"
将文件名的空格更改为下划线( _
)。 但是将它更改为set name=!name:-= !
不适合我。
如何在批处理文件中最好用连字符替换空格?
你很亲密
@echo off
cls
echo Insert a string with spaces?
set string=
set /p string=
set string=%string: =-%
echo String is now %string%
见set /?
有关变量操作的详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.