473,796 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FOR loop variables - not utilized immediately

12 New Member
Greetings,

I'm using WindowsXP Pro v5.1and am writing a BAT file to loop through a list of files (contained in a .txt file) and pass them (actually variables derived from them) to another program (ERDAS Imagine). What appears to be happening is that the FOR loop isn't utilizing the variables until the 2nd iteration after the variable is set. Since one variable is dependent on another, I have to run the program 3 times before the variables are correctly utilized. The 3rd time I run it the program works fine.

My original input list that I was using for testing had only two entries. To try to isolate what was going on, I made two separate FOR "loops", each with one entry. I've written in lots of echoes but still don't understand what's going on.

This is a subset of a larger program so let me know if you need more context. I'll include the relevant code below plus the echoes and results from the "set" command at the c:> prompt.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. ECHO OFF
  2.  
  3. REM Testing var settings w/in imagine line
  4. REM     and issuing import command for H3 and H1 header files
  5.  
  6. set intar=C:\WorkSpace\LTC\MRLC_test\NZT
  7. echo *****         intar = %intar%
  8.  
  9. set outtar=C:\WorkSpace\LTC\MRLC_test\NZT\tmp2
  10. echo *****        outtar = %outtar%
  11.  
  12. set tlist=tarList1.txt
  13. echo *****         tlist = %tlist%
  14. echo.
  15.  
  16. REM Create separate text files containing name of H1 and H3 header files
  17. dir /b %outtar%\*.H1 > %outtar%\hdr1Name.txt
  18. dir /b %outtar%\*.H3 > %outtar%\hdr3Name.txt
  19.  
  20. REM Establish variable for text file containing H3 header file name
  21. set hlist3=%outtar%\hdr3Name.txt
  22.  
  23. for /f %%h in (%hlist3%) do (
  24.    set headr3=%%h
  25.    set hbase3=%headr3:~0,18%
  26.    set  hex3=%headr3:~19,2%
  27.    echo ***** Header3 file is %headr3%
  28.    echo ***** Basename3 is %hbase3%
  29.    echo ***** Extension3 is %hex3%
  30.    echo.
  31.    REM Open Imagine and call the import NLAPS script
  32.    start "" "C:\Program Files\Leica Geosystems\IMAGINE and LPS 9.0\bin\ntx86\importndf.exe" -inputfilename "%outtar%/%headr3%" -outputfilename "C:/WorkSpace/LTC/MRLC_test/IMG/%hbase3%_%hex3%.img" -pixeltype u8
  33. )
  34.  
  35. REM Establish variable for text file containing H1 header file name
  36. REM Originally this loop was incorporated in the loop above, but I separated 
  37. REM      them for diagnostic purposes.
  38. set hlist1=%outtar%\hdr1Name.txt
  39.  
  40. for /f %%h in (%hlist1%) do (
  41.    set headr1=%%h
  42.    set hbase1=%headr1:~0,18%
  43.    set   hex1=%headr1:~19,2%
  44.    echo ***** Header1 file is %headr1%
  45.    echo *****    Basename1 is %hbase1%
  46.    echo *****   Extension1 is %hex1%
  47.    echo.
  48.    REM Open Imagine and call the import NLAPS script
  49.    start "" "C:\Program Files\Leica Geosystems\IMAGINE and LPS 9.0\bin\ntx86\importndf.exe" -inputfilename "%outtar%/%headr1%" -outputfilename "C:/WorkSpace/LTC/MRLC_test/IMG/%hbase1%_%hex1%.img" -pixeltype u8
  50. )
  51.  
Here's what the echoes look like for the first three runs:

1ST RUN:
C:\>C:\WorkSpac e\LTC\MRLC_test \scripts-bats-docs\setvarInIm agine2.bat
C:\>ECHO OFF

***** intar = C:\WorkSpace\LT C\MRLC_test\NZT
***** outtar = C:\WorkSpace\LT C\MRLC_test\NZT \tmp2
***** IMG directory = C:\WorkSpace\LT C\MRLC_test\IMG
***** tlist = tarList1.txt

***** Header3 file is
***** Basename3 is
***** Extension3 is
***** Header1 file is
***** Basename1 is
***** Extension1 is

Set:
hbase1=~0,18
hbase3=~0,18
headr1=LE708301 4000019251.H1
headr3=LE708301 4000019251.H3
hex1=~19,2
hex3=~19,2
hlist1=C:\WorkS pace\LTC\MRLC_t est\NZT\tmp2\hd r1Name.txt
hlist3=C:\WorkS pace\LTC\MRLC_t est\NZT\tmp2\hd r3Name.txt

2ND RUN:
C:\>C:\WorkSpac e\LTC\MRLC_test \scripts-bats-docs\setvarInIm agine2.bat
C:\>ECHO OFF

***** intar = C:\WorkSpace\LT C\MRLC_test\NZT
***** outtar = C:\WorkSpace\LT C\MRLC_test\NZT \tmp2
***** IMG directory = C:\WorkSpace\LT C\MRLC_test\IMG
***** tlist = tarList1.txt

***** Header3 file is LE7083014000019 251.H3
***** Basename3 is ~0,18
***** Extension3 is ~19,2

***** Header1 file is LE7083014000019 251.H1
***** Basename1 is ~0,18
***** Extension1 is ~19,2

Set:
hbase1=LE708301 4000019251
hbase3=LE708301 4000019251
headr1=LE708301 4000019251.H1
headr3=LE708301 4000019251.H3
hex1=H1
hex3=H3
hlist1=C:\WorkS pace\LTC\MRLC_t est\NZT\tmp2\hd r1Name.txt
hlist3=C:\WorkS pace\LTC\MRLC_t est\NZT\tmp2\hd r3Name.txt

3rd RUN:
C:\>C:\WorkSpac e\LTC\MRLC_test \scripts-bats-docs\setvarInIm agine2.bat
C:\>ECHO OFF

***** intar = C:\WorkSpace\LT C\MRLC_test\NZT
***** outtar = C:\WorkSpace\LT C\MRLC_test\NZT \tmp2
***** IMG directory = C:\WorkSpace\LT C\MRLC_test\IMG
***** tlist = tarList1.txt

***** Header3 file is LE7083014000019 251.H3
***** Basename3 is LE7083014000019 251
***** Extension3 is H3
***** Header1 file is LE7083014000019 251.H1
***** Basename1 is LE7083014000019 251
***** Extension1 is H1

Thanks in advance,

Thom Curdts
Jul 16 '07 #1
6 3037
tcurdts
12 New Member
One more thing...

Line 24 of the code is where it starts to fall apart. That variable seems to be set, but not utilized until the next time the program is run.

Thanks again...
Jul 16 '07 #2
epots9
1,351 Recognized Expert Top Contributor
i'm hoping that this can be useful for u http://www.robvanderwoude.com/ntfor.html

good luck
Jul 17 '07 #3
bartonc
6,596 Recognized Expert Expert
i'm hoping that this can be useful for u http://www.robvanderwoude.com/ntfor.html

good luck
Great link (I've bookmarked it), thank you.
Jul 17 '07 #4
epots9
1,351 Recognized Expert Top Contributor
Great link (I've bookmarked it), thank you.
np, glad i could help :D
Jul 17 '07 #5
tcurdts
12 New Member
Thanks. I've seen that page but still can't figure out why my variables don't seem to be set immediately...
Jul 17 '07 #6
tcurdts
12 New Member
I'm still not sure why the variables weren't getting set right away but I figured out an alternate method.

Instead of setting variables on lines 24, 25 and 26 (code from original post) I used "tokens" in the FOR statement to parse the filenames, for use on line 7 below. The relevant portion of the new code looks like:
Expand|Select|Wrap|Line Numbers
  1. set hlist=%outtar%\hdrList.txt
  2. FOR /F "tokens=1,2* delims=." %%y in (%hlist%) DO (
  3.     echo ***** Current Header file is %%y.%%z
  4.     echo *****       File Basename is %%y
  5.     echo *****      File Extension is %%z
  6.     echo IMPORTING %%y.%%z.  Please be patient...
  7.     "C:\Program Files\Leica Geosystems\IMAGINE and LPS 9.0\bin\ntx86\importndf.exe" -inputfilename "%outtar%/%%y.%%z" -outputfilename "C:/WorkSpace/LTC/MRLC_test/IMG/%%y_%%z.img" -pixeltype u8
  8. )
  9.  
Jul 24 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

16
9916
by: TheKeith | last post by:
I'm writing a script with a while loop in it. How can I get it to go slower so that it doesn't appear to happen all at once--so that it looks animated--basically. I tried the setTimeout(500) in the last line of the loop, but that just messes things up. Any help would be appreciated--thanks.
32
3821
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
4
2818
by: hazz | last post by:
given namespace WindowsService1 { public class Service1 : System.ServiceProcess.ServiceBase......... private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { ArrayList myAL = new ArrayList(); CustomClass objCont = new ControllerClass; CustomClass objInfo = new InfoClass;
32
2609
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my memories are of but I think I just said loop somewhere inside the loop and it immediately jumped back to the start of the loop and began again. I can't seem to do that in .net. I this functionality available?
51
3966
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and output to be within one directory, and I want to be able to combine files from different directories into another directory of my choice). My program seems to work fine, but I'm wondering about this loop: for (int i = 0; i < numFiles; i++)
18
3446
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that File-New-Window creates an instance of IE in the same process with the same SessionID as the parent window is in big trouble. This fundamentally restricts the usefullness of using session state management. I probably missed it somewhere - can...
16
1672
by: johnf | last post by:
Hi, When I use dir() I don't see the __ underscore items. Is there anything that will show all the private vars and functions? johnf
9
7525
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
5
4050
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.