gpt4 book ai didi

variables - 批处理文件;用连字符替换变量的空格

转载 作者:行者123 更新时间:2023-12-02 22:12:23 24 4
gpt4 key购买 nike

我已经开始了另一个小型批处理项目,但是我遇到了一些困境。我使用 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:_= !" 将文件名的空格更改为下划线 (_)。但是将其更改为 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/?

关于variables - 批处理文件;用连字符替换变量的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20646860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com