Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Search Search
Search for:
Tech Support Guy > > >

Batch Script - Variable issue from parsing an input file


(!)

Vicrater's Avatar
Vicrater Vicrater is offline
Computer Specs
Junior Member with 1 posts.
THREAD STARTER
 
Join Date: Dec 2009
Experience: Intermediate
02-Dec-2009, 02:11 PM #1
Exclamation Batch Script - Variable issue from parsing an input file
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
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
02-Dec-2009, 06:09 PM #2
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 %%_var!cnt!%=="" GOTO CLEANUP
Should be
IF %_var!cnt!%=="" GOTO CLEANUP
But these use a leading underscore:
Code:
ECHO first variable is: %_var!_cnt!%
SET _LocalDrv=_var%_cnt%
SET /a _cnt2=_cnt+1
SET _NetDrv=_var%_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=%_var!_cnt!%
SET _NetDrv=%_var!_cnt2!%
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

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


Tags
batch, for logic flow, set associations, variables

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑