没有一个循环内评估的变量

没有一个循环内评估的变量

本文介绍了CMD:没有一个循环内评估的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使.BAT脚本,需要得到一些字符串正常工作。

trying to make a .bat script, and need to get some strings working properly.

这是我有这么远

@echo off
for /r %%i in (*.csv) do (
set str=%%i
set str=%str:csv=rar%
echo %%i
echo.%str%
)

说我有这个运行在C:\\,并得到了5 CSV,1.csv,2.csv ... 5.csv

Say I've got this running in C:\, and got 5 csv, 1.csv, 2.csv... 5.csv

我第一次运行它,我得到的输出:

First time I run it, I get output of:

C:\1.csv

C:\2.csv

C:\3.csv

C:\4.csv

C:\5.csv

第二次我得到:

C:\1.csv
csv=rar
C:\2.csv
csv=rar
C:\3.csv
csv=rar
C:\4.csv
csv=rar
C:\5.csv

然后,所有后续调用我得到:

Then all subsequent calls I get:

C:\1.csv
rar=rar
C:\2.csv
rar=rar
C:\3.csv
rar=rar
C:\4.csv
rar=rar
C:\5.csv

当我将期待得到的是直通:

When what I would be expecting to get is straight through:

C:\1.csv
C:\1.rar
C:\2.csv
C:\2.rar
C:\3.csv
C:\3.rar
C:\4.csv
C:\4.rar
C:\5.csv
C:\5.rar

所以我删除替代:

So I remove the substitution:

@echo off
for /r %%i in (*.csv) do (
echo %%i
set str=%%i
echo.%str%
)

首先运行:

C:\1.csv

C:\2.csv

C:\3.csv

C:\4.csv

C:\5.csv

第二次运行:

C:\1.csv
C:\5.csv
C:\2.csv
C:\5.csv
C:\3.csv
C:\5.csv
C:\4.csv
C:\5.csv
C:\5.csv
C:\5.csv

这就像它没有设置str变量,直到循环的最后运行,即使它试图回声出变量,行其中的设置后发生,然后保存在该下一个循环?是否有什么我失踪它processess循环的方式吗?

It's like it's not set the str variable until the last run of the loop, even though it's trying to echo out the variable, the line of which occurs after the setting, and then saved this for the next loop? Is there something I'm missing on the way it processess loops?

感谢

推荐答案

您应该使用。其实,内容%力量:CSV = RAR%运行循环前,只计算一次。所以:

You should use setlocal enabledelayedexpansion. Actually, the content of %str:csv=rar% is evaluated only once before the loop runs. So:

@echo off
setlocal enabledelayedexpansion
for /r %%I in (*.csv) do (
set str=%%i
set str=!str:csv=rar!
echo %%i
echo.!str!
)
endlocal

这篇关于CMD:没有一个循环内评估的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:16