我正在尝试使用正则表达式创建一个命令行(或 .bat 文件),该正则表达式必须用(单个空格字符)替换 *(多个空格字符)。仅用一个空格字符替换多个空格字符。在 Unix 中,我们可以使用 sed。但是在我们的情况下这是不可能的,并且安装 sed 不是一种选择。有任何想法吗?
问问题
1737 次
1 回答
0
@echo off
set file_name_with_spaces="_ file name _.txt"
rem --- '?' is forbidden in file names ---
set file_name_with_no_spaces=%file_name_with_spaces: =?%
echo %file_name_with_no_spaces%
rem --- we need to to replace the quewstion marks that have primer numbers count and their modulus --
set file_name_with_no_spaces=%file_name_with_no_spaces:??=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:???=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:?????=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:???????=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:????????????=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:???????=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:???????=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:???=?%
set file_name_with_no_spaces=%file_name_with_no_spaces:??=?%
set file_name_with_single_spaces=%file_name_with_no_spaces:?= %
rem --- ---
echo %file_name_with_single_spaces%
ren %file_name_with_spaces% %file_name_with_single_spaces%
这在大多数情况下应该可以工作。如果你的文件有很多空格,你需要添加更多?
的表达式(也可以插入 for 循环,但这更容易:-))。
于 2012-11-29T08:21:00.343 回答