问题描述
我写一个文件,从文件名的文件夹中删除空格,然后把结果在 .TXT
文件。我刚刚得到一个结果的回音是。一遍又一遍。
I am writing a file to remove spaces from filenames in a folder and then put the result in a .txt
file. I just get a result of "Echo is on." over and over.
这是我迄今为止:
@echo ON
SET LOCAL EnableDelayedExpansion
For %%# in (*.*) do (
SET var=%%~n#
Set MyVar=%var%
set MyVar=%MyVar: =%
echo %MyVar%>>text.txt
)
谁能告诉我什么是错?
Can someone tell me whats wrong?
推荐答案
为什么你所得到的原因 ECHO处于开启状态。
,是因为没有使用拖延扩张,这导致的值%VAR%
和%为MyVar%
在将要插入
命令运行,因为他们并没有在一开始定义的空变量插入当回声%为MyVar%方式>>的text.txt
已运行,它是作为PTED 回声>>的text.txt
。当回声不带任何参数运行,它输出回波无论是打开还是关闭,这是你在获得的text.txt
。
The reason why you are getting ECHO is on.
is because delayed expansion was not used, which caused the value of %var%
and %MyVar%
to be inserted before the for
command is run, and since they were not defined at the start, empty variables were inserted in. When the echo %MyVar%>>text.txt
was run, it was interpreted as echo >>text.txt
. When echo is run without any arguments, it outputs whether echo is on or off, which is what you get in text.txt
.
要解决这个问题,你必须做两件事情:
To fix the problem, you have to do two things:
第一,有什么不对您的第二道防线。有一个在 SETLOCAL
组和地方之间没有空格。第二行应该是 SETLOCAL EnableDelayedExpansion
。
First, there is something wrong with your second line. There is no space between set and local in setlocal
. The second line should be SETLOCAL EnableDelayedExpansion
.
二,使用延迟扩展,您有来替换所有
,像%
S IN的每个变量!!无功!
而不是%VAR%
。
Second, to use delayed expansion, you have to replace all %
s in each variable with !
, like !var!
instead of %var%
.
最终结果:
@echo ON
SETLOCAL EnableDelayedExpansion
For %%# in (*.*) do (
SET var=%%~n#
Set MyVar=!var!
set MyVar=!MyVar: =!
echo !MyVar!>>text.txt
)
您其实并不需要在这种情况下,使用临时变量,你可以做设置为MyVar = %%〜N#
并跳到设置为MyVar = MyVar的:!=
You actually do not need to use a temporary variable in this case, you can just do SET MyVar=%%~n#
and skip to set MyVar=!MyVar: =!
.
这篇关于从批变量去掉空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!