简体   繁体   中英

Not defined variable carrying info from FOR LOOP batch

AIMS: Learn how to use empty variables in a FOR LOOP routine in a batch file.

PROBLEM: I am using whether a variable is defined or not to determine the appropriate subroutine in my script; 2 variables defined == go do something. One is defined but not the other == do something else. Both not defined == something else again.

The script checks for C$ access and the presence of a certain file within the C$ of a text file list of networked PC's. If both criteria are met by 2 variables having data set, a simple xcopy updates the file in question by going to a certain subroutine and then the LOOP moves to the next PC on the network to try and do the same.

If there is an issue I want to use the variables in an empty state to then do something else; for example report to text file that c$ was not accessible or the file was missing meaning bad install etc.

However on my 2 test machines I am breaking the folder paths to trip the error reporting routines and finding something strange I can't fix without writing more lines of code. In my text file I have 2 PC's listed a few times eg

PC1 PC2 PC1 PC2 PC1 PC2

PC1 has a broken file path to test error logging PC2 All fine to test file update process

When I run the script PC1 gets reported as having a problem and logs correctly. PC2 all is fine and the update happens fine. BUT THEN it hits PC1 again, but seems to think [even though file path still broken] that it is OKAY --as if the variable is remembered from the previous loop and of course tries to update and has problems.

Here is the code I was trying to get to work using empty variable

@echo off
color 0E
pushd %~dp0
setlocal EnableDelayedExpansion

for /f "usebackq tokens=*" %%i in ("%~dp0hostnames.txt") do (

rem Test Access Admin Shares C$
if exist "\\%%i\C$\Windows\System32" set dollar=yes

rem Test Installation Integrity
if exist "\\%%i\C$\ProgramData\config.cfg" set install=ok

echo %%i
echo !dollar!
echo !install!
pause

IF !dollar!==yes IF !install!==ok (call :updatecfg %%i)

IF !dollar!==yes IF [!install!]==[] (call :installerror %%i)

IF [!dollar!]==[] (call :errorshare %%i)
)
echo THE END
pause
exit

:updatecfg
CLS
XCOPY "%~dp0config.cfg" /Y "\\%1\C$\ProgramData" & echo %1 Update Config.cfg Succeeded! & echo %1 Update Succeeded! >>"%~dp0logpass.txt" 
ping 127.0.0.1 -n 3 >nul
goto :eof

:errorshare
CLS
echo.
echo    %1 Has C$ Access Issues [Logging] & echo %1 Has C$ Access Issues >>"%~dp0logfail.txt"
ping 127.0.0.1 -n 3 >nul
goto :eof

:installerror
CLS
echo.
echo    %1 Cannot Find Config.cfg^!^! [Logging] & echo %1 Cannot Find Config.cfg^!^! Not Installed or Configured^? >>"%~dp0logfail.txt"
ping 127.0.0.1 -n 4 >nul
goto :eof

If I add if not exist entried to the 2 at the start and have them set something when there is a problem then this works fine. But I'd like to know if this the right way to do this or should I also be able to use empty variables. I am nearly there it's just that they are not clearing properly per loop.

Many thanks.

....
for /f "usebackq delims=" %%i in ("%~dp0hostnames.txt") do (
    rem Clear variables for each iteration
    set "dollar="
    set "install="

    rem Test Access Admin Shares C$
    if exist "\\%%i\C$\Windows\System32" set "dollar=yes"

    rem Test Installation Integrity
    if exist "\\%%i\C$\ProgramData\config.cfg" set "install=ok"

    if defined dollar (
        if defined install (
            call :updatecfg %%i
        ) else (
            call :installerror %%i
        )
    ) else (
        call :errorshare %%i
    )
)
....

or

....
for /f "usebackq delims=" %%i in ("%~dp0hostnames.txt") do (

    rem Test Access Admin Shares C$
    if exist "\\%%i\C$\Windows\System32"  ( set "dollar=yes" ) else ( set "dollar=" )

    rem Test Installation Integrity
    if exist "\\%%i\C$\ProgramData\config.cfg" ( set "install=ok" ) else ( set "install=" )

    if defined dollar (
        if defined install (
            call :updatecfg %%i
        ) else (
            call :installerror %%i
        )
    ) else (
        call :errorshare %%i
    )
)
....

In any case, you should ensure the variables have the adecuated value before taking a decision based on their content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM