Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to parse part of a line of a text file into environment variable? 1

Status
Not open for further replies.

MichaelFH

Technical User
Dec 22, 2004
33
GB
I need to create a batch file (no Vb scripting permitted on our network). And I am stuck...

Background:-

We have a batch file which is run at login from the startup folder on a remote PC. At the moment when the application is run by the batch file at start up it crashes for some reason, probably because of a shortage of resources during log on. Running the batch file manually, later, works fine.

My solution is to put a delay in the batch file. Windows XP does not have a "sleep" command. I have found Sleep.bat, which uses ping to create the delay, but requires an IP address to ping. I do not wish to ping a server, as this may be detected by our IT Guys at head office, and in any case the server may be removed at some time in the future. As we use DCHP I cannot guarantee that any specific IP address will stay valid, so I intend pinging the PC the batch file is running on, having read the IP address into an environment variable. My script for obtaining the IP address, (such as it is) is:-

=========================================================

@ECHO OFF
REM
REM GetIP.bat
REM Gets the IP address of PC it is run on.

REM save the relevant line of the IPconfig output to a
REM text file
ipconfig | findstr "Address" > C:\ipfile.txt

=========================================================

The result is a text file containing the following line:-

IP Address. . . . . . . . . . . . : 255.255.255.255

(Fictitious IP address)

I now need to extract the end of that line starting with the character two to the right of the :

How do I achieve this using only batch file commands included in Windows XP Professional?

Any assistance would be most appreciated!

Michael
 
Not sure, but here might be some decent references:



and so forth...

That is something I've been looking at using to put together some basic little functions myself, but just never can get around to fiddling with it. [wink]



--

"If to err is human, then I must be some kind of human!" -Me
 
I think you're killing flies with canions.

You can always ping 127.0.0.1 (your own machine) or 1.1.1.1 (that will probably end on request timeout) but I think your truly problem is the error in the batch file: are you sure it's a lack of resources? Are you sure a delay will work?

Cheers,
Dian
 
The Windows Server Resource Kit has a sleep command that does not mess with a ping:
I would tend to agree with Dian though. I bet it's something else causing the failure. If you can, try got get task manager (or better yet, Sysinternal's Process Explorer running before the batch file fires off and watch what's going on. You might be able to see something.

_____
Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Perhaps not the most eloquent as there must be better ways to parse the output of the findstr command but:

The first part below is a slightly modified version of your GetIP.bat. Note the output file extension of the findstr command is .cmd not .txt, and the addition of the line containing ipfile

Code:
@echo off
REM 
REM GetIP.bat
REM Gets the IP address of PC it is run on.

ipconfig | findstr "Address" > C:\ipfile.cmd
c:\ipfile

Next, create a file called IP.CMD at the root of C: (or in the same directory where ipfile.cmd is created)

Code:
@echo off
REM IP.CMD
REM Target CMD file invoked byh IPFILE.CMD
REM At exit, %1 will contain the IP address of the executing PC

:loop
shift
if %1==. goto :loop
shift
echo Your IP Address is %1

Finally, execute GetIP. The IP address of the executing PC should be displayed.
 
Thank you all for your replies.

Dian, I was unaware of the significance of the IP address 127.0.0.1, which is the address pinged by the sleep.bat file I found. In my ignorance, I had presumed this to be a valid IP address on the batch file authors' network.

Jeff, I may have confused things a little in my explanation of the problem I was trying to resolve. This was an attempt at keeping the post to a reasonable length. The "application" is Powerpoint Viewer 2003, which is showing a pair of presentations alternately using a play list. One is a single slide, the other contains all of the information for our digital signage screen. The reason for the second presentation is so that any changes made are loaded each time the main presentation is run. Both presentations are saved on a file server. If the batch file that launches this is run after the PC has booted, everything runs correctly, but when run from startup, the presentation freezes after the first few slides, requiring user intervention (pressing a key to advance to the next slide) after which the presentation behaves itself. My saying the application crashed was an over simplification of the problem.

Thanks again

Michael
 
You are right. The delay has not helped. The network PC this is running on has been set up by one of our IT Gurus at head office so that it logs on automatically. They have locked the PC down so that no one can run anything else on it. Windows explorer is not run, a batch file being called in its place, which maps the network drive and runs the batch file which in turn runs Powerpoint viewer.

Having watched the presentation start up and advance through the first few slides, it now appears that Powerpoint viewer is "loosing focus" i.e. ceases to be the front window, and hence stops advancing. At the point the presentation stops advancing, the mouse pointer appears, which is what suggests to us that PowerPoint Viewer has lost focus.

Any suggestions on a cure for this, or should I create a new post on the Tek-Tips MS Office forum?
 
Ahh, I bet you're right. I've been in a similar boat and there doesn't appear that there is a way to make sure something is the last thing to load. Evidently, startup order is is unspecified and subject to change: Even if you're able to force it last, there's no guarantee everything else has gone idle first.

You might try something like this:
Otherwise, the full startup order is:
1. BootExecute
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute
2. Services
3. User enters a password and logon to the system
4. UserInit
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\UserInit
5. Shell
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell
6. All Users-RunOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
7. All Users-Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
8. All Users-RunOnceEx
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
9. All Users-RunEx
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunEx
10. Current User-RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
11. Current User-Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
12. Current User-RunOnceEx
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
13. Current User-RunEx
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunEx
14. Common Startup Folder
15. Startup Folder

If you put your batch file in the users' Startup folder and give it a long delay (like 2 minutes) to give everything else a chance to go idle, you might have a chance.

_____
Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Jeff,

Thanks for this. I have tried running a Command prompt from the both the autologin user ID's startup folder, and the "All Users" startup folder. It did not run from either. Could this be because Windows Explorer is not being used as the shell?
 
I don't know.

Another option might be to write a VBA macro in your signage presentation that runs on a timer and takes focus every few minutes.

You could probably get better help on that in the Microsoft Office forum.

_____
Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Jeff,

Many thanks for your help with this. As suggested, I will go to the Microsoft Forum for further help, however, I am not sure that vba macros run under powerpoint viewer.

Michael.
 
I don't know either.

Another thing to investigate may be to set up the XP to run in kiosk mode. Since kiosk mode changes input expectations, your presentation may not lose focus.

( I admit to grasping at straws... ;-) )

_____
Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
I now need to extract the end of that line starting with the character two to the right of the :
How do I achieve this using only batch file commands included in Windows XP Professional?

To answer the original question, this would be one way to do it...

Code:
ipconfig|find /i "address">%temp%\~IP.tmp
for /f "tokens=2 delims=:" %%a in (%temp%\~IP.tmp) do set TempIP=%%a
for %%a in (%TempIP%) do set MyIP=%%a
set TempIP=
if exist %temp%\~IP.tmp del %temp%\~IP.tmp
echo My IP Is: "%MyIP%"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top