窗口批处理脚本 - 里面设置变量循环 [英] windows batch script - setting variable inside for loop

查看:138
本文介绍了窗口批处理脚本 - 里面设置变量循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我不会再presenting清楚我的问题,这里的实际code我所做的:

maybe i'm not representing my question clear, here's the actual code i did:

@echo off

set /p keywords="Enter keywords to search: " %=%

dir /b *.dat > filelist.txt

for /f "delims=." %%f in (filelist.txt) do (
   for /f "delims= " %%g in (%%f.dat) do (
      7z e %%g *sec.evtx
      dir /b *.evtx > evtfile.txt
      set /p tmpvar1=<evtfile.txt
      del *.evtx
   )
)

Filelist.txt中

tsnint1.dat
webint1.dat

tsnint1.dat

TSNINT1-201312091700.zip
TSNINT1-201312091600.zip
TSNINT1-201312091500.zip
TSNINT1-201312091400.zip
TSNINT1-201312091300.zip
TSNINT1-201312091200.zip

webint1.dat

WEBINT1-201312091300.zip
WEBINT1-201312091200.zip

我现在面临的问题是,evtfile包含内容的正确,但tmpvar1没有正确预期分配的,什么是我的错误以及如何纠正呢?非常感谢

the problem i'm facing is, evtfile consists of the right content but tmpvar1 is not assigned correctly as expected, what is my mistake and how to correct it? many thanks

推荐答案

您需要延迟扩展使用同一区块内,当这个变量已经被设置(或改变)一个块中的变量。但你可以的设置无延迟扩展变量。

You need delayed expansion to use a variable inside a block when this variable has been set (or changed) inside the same block. But you can set a variable without delayed expansion.

看到这个小演示(我用一个简单的如果结构,而不是,但效果是一样的(不与如果,而是用的的(在)。

See this little demonstration (I used a simple if construct instead of for, but the effect is the same (not with if or for, but with blocks (inside ( and )).

@echo off
REM SETTING a variable inside a block
set "var=ONE"
echo start: %var%
if 1==1 (
  echo   inside block: %var%
  set var=TWO
  echo   var has a new value:
  set var
  echo   inside block is still old: %var%
)
echo after block: %var%

echo ----------

REM USING a variable inside a block
setlocal enabledelayedexpansion
set "var=ONE"
echo start: %var%
if 1==1 (
  echo   inside block: %var%
  set var=TWO
  echo   var has a new value:
  set var
  echo   new value inside block: !var!
  echo   just to demonstrate: %var% 
)
echo after block: %var%
endlocal
echo ----------
echo working fine: %var%
echo not available after endlocal: !var!

这篇关于窗口批处理脚本 - 里面设置变量循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆