2013-12-17 134 views
9

我已經開始了另一個小批量項目,但是我遇到了一些困難。我使用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:-= !對我無效。

如何在批處理文件中最好使用連字符替換空格?

回答

10

你很近。

@echo off 
cls 
echo Insert a string with spaces? 
set string= 
set /p string= 
set string=%string: =-% 
echo String is now %string% 

有關變量操作的詳細信息,請參閱set /?

+0

謝謝,作品像魅力。另外,我現在看看如何在':'之後設置變量,而不是猜測。再次感謝! – Destrotant