为什么在FOR循环中不更新环境变量? [英] Why are environment variables not updating within FOR loop?

查看:180
本文介绍了为什么在FOR循环中不更新环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件正在扫描 URLs.txt 文件,并为每个网址运行并下载文件。我有的问题是FOR循环内的环境变量。我使用 cat sed awk 来获取url的最后两部分,以便提供文件名。问题是环境变量在第一次运行后永远不会更新。我可以看到 tmp2.txt 只是为每个url正确更新,但批处理文件并没有更新 outfile ,因此我一直覆盖第一个文件。



我尝试简化测试的批处理文件,并且for循环中的任何变量都不会更新。

< $($。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$'$ b echo %% A> tmp.txt
cat tmp.txt | seds / \(。* \)\ // \1_ /| awk -F/{print $ NF $}> tmp2.txt
set / p outfile =< tmp2.txt
echo Varible A
echo %% A
echo。
echo Varible outfile
echo%outfile%
call curl.exe -o%outfile%-u username:password --insecure %% A
pause

outfile ?


解决方案

  echo!outfile! 

在一个块语句中(括号内的一系列语句)整个块被解析,然后被执行。块中的任何%var%将被在块解析时的值替换 - 在块执行之前 - 相同东西适用于 FOR ... DO(block)



因此, IF (something)else(somethingelse)将在 IF 时使用%variables%遇到code>。

解决这个问题的两种常用方法是使用 setlocal enabledelayedexpansion 和使用!var!来代替%var%来访问 var 或者2)调用一个子程序使用改变的值执行进一步的处理。

请注意使用 CALL ECHO %% var %% ,它显示 var 的更改值。 CALL ECHO %% errorlevel %% 显示,但不幸的是,然后RESETS errorlevel。

在你的情况,因为 outfile 被赋值为 %% A ,您可以替换%outfile% %% A - 这将是一个想法引用字符串无论如何,因为引用一个字符串使它成为一个单一的标记,而不是一个系列 - 以防万一您的文件名包含分隔符如空格


I have a batch file that is scanning a file URLs.txt and for each url run it and download the file. The issue I have is the environment variable within the FOR loop. I am using cat, sed and awk to get the last two parts of the the url so I can provide the filename. The issue is the environment variable is never updated after the first run. I can see that tmp2.txt just updated correctly for every url, but the batch file is not updating outfile and thus I keep overwriting the first file.

I tried to simplify the batch file for a test and any variable within a for loop never seems to update.

@echo off
setlocal enabledelayedexpansion

for /F "tokens=*" %%A in (URLs.txt) do (
    echo %%A > tmp.txt
    cat tmp.txt | sed "s/\(.*\)\//\1_/" | awk -F "/" "{print $NF}" > tmp2.txt
    set /p outfile=<tmp2.txt
    echo Varible A
    echo %%A
    echo.
    echo Varible outfile
    echo %outfile%
    call curl.exe -o %outfile% -u username:password --insecure %%A
    pause
)

Why is environment variable outfile not updated within FOR loop?

解决方案

echo !outfile!

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

Note therefore the use of CALL ECHO %%var%% which displays the changed value of var. CALL ECHO %%errorlevel%% displays, but sadly then RESETS errorlevel.

In your case, since outfile is assigned the value %%A, you can replace %outfile% with %%A - and it would be an idea to "quote the string" anyway since "quoting a string" makes it a single token rather than a series - just in case your filenames contain separators like Space

这篇关于为什么在FOR循环中不更新环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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