1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch Script - Variable issue from parsing an input file

Discussion in 'Software Development' started by Vicrater, Dec 2, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. Vicrater

    Vicrater Thread Starter

    Joined:
    Dec 2, 2009
    Messages:
    1
    Need aid in figuring out how to set the _Var(x) variables read in from the text file (which contains directory listings) file to either new variables or a way to incrementally reference (_var1, _var2, _var3...) in an IF or FOR statement later in the batch file. ISSUE - the second IF will not iterate properly the string saved from the input file in _var1, _var2, _var3....

    CODE SNIPET:
    SET cnt=1
    SET cnt2=1
    SET SYNCCmd="C:\Program Files\SyncLaptop\SYNC.CMD"
    .
    .
    .
    SetLocal EnableDelayedExpansion
    ::This line is just in case n has been defined before the batch file is run
    ::This FOR loop works flawlessly - reads in all file paths to _var variables
    Set n=
    Set _InputFile=c:\TEMP\SYNCLOG\SyncDirList.txt
    For /F "tokens=*" %%I IN (%_InputFile%) DO (
    Set /a n+=1
    Set _var!n!=%%I
    )
    :: Line will display the variables just assigned - delete when not needed
    Set _
    Pause

    :: LOOP through directory listing variables _var(x) until there are no more
    :SYNCRO
    IF %%_var!cnt!%=="" GOTO CLEANUP
    ECHO first variable is: %_var!_cnt!%
    SET _LocalDrv=_var%_cnt%
    SET /a _cnt2=_cnt+1
    SET _NetDrv=_var%_cnt2%

    :: Check if real directory values where set to the variables - delete later
    ECHO LocalDrv is: %_LocalDrv%
    ECHO NetDrv is : %_NetDrv%
    pause
    :: Open net CMD window, set color etc & call the XCOPY SNYC.CMD file to sync the two designated HD folders
    START /W CMD /C "ECHO OFF & CLS & MODE CON: COLS=60 LINES=15 & COLOR 74 & %_SYNCCmd% "%_LocalDrv%" "%_NetDrv%" /S & pause"

    :: Write to log file the directories that have been synchronized
    ECHO %time% %date% - Done with %_LocalDrv% directory sync. >> C:\temp\SyncLog\SyncLogs.txt

    SET /a _cnt+=1
    ECHO %cnt% - counter after if loop & GOTO SYNCRO
    EndLocal

    :: Clean up variales
    :CLEANUP
    set _cnt=
    set _cnt2=
    set _var=
    GOTO SETVARS
    ...


    NOTE the file SYCN.CMD contains two lines of code for XCOPY that check if each of the folders reference paths have any files newer than the other, if so it copies them to the respective folder. This part works well.

    The main problem is the variables:
    _LocalDrv acutally echos to be "_var1" not it contents (i.e, C:\DATA\MyData) that was read in from the input text file. The same for the _NetDrv variable.

    Thanks
    VicR
     
  2. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Welcome to TSG!

    You initialize these
    Code:
    SET cnt=1
    SET cnt2=1
    
    Note there is no leading underscore.

    This line uses the correct name, but should not have the doubled % symbol, just a single one:
    Code:
    IF [COLOR=Red]%%[/COLOR]_var!cnt!%=="" GOTO CLEANUP
    Should be
    IF %_var!cnt!%=="" GOTO CLEANUP
    But these use a leading underscore:
    Code:
    ECHO first variable is: %_var![B][COLOR=Red]_[/COLOR][/B]cnt!%
    SET _LocalDrv=_var%[B][COLOR=Red]_[/COLOR][/B]cnt%
    SET /a [B][COLOR=Red]_[/COLOR][/B]cnt2=[B][COLOR=Red]_[/COLOR][/B]cnt+1
    SET _NetDrv=_var%[B][COLOR=Red]_[/COLOR][/B]cnt2%
    Change the variables so all are consistant, either using an underscore or not.

    Also, you are setting the _LocalDrv and _NetDrv variables to _var+the number in _cntX. These lines should be:
    Code:
    SET _LocalDrv=[COLOR=Red][B]%[/B][/COLOR]_var[COLOR=Red][B]![/B][/COLOR]_cnt[COLOR=Red][B]![/B][/COLOR]%
    SET _NetDrv=[COLOR=Red][B]%[/B][/COLOR]_var[COLOR=Red][B]![/B][/COLOR]_cnt2[COLOR=Red][B]![/B][/COLOR]%
    Set _var= only clears one variable named _var. It won't clear _var1 or any others.
    If you want to clear all of the _varX variables use this loop:
    Code:
    For /F "Tokens=1 Delims==" %%I In ('Set _var') Do Set %%I=
    Use ('Set _') to clear all variables that start with an underscore.

    HTH

    Jerry
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/882313