Petri IT Knowledgebase Forums
 

Petri.co.il forums Home Forums Start Page Forums Frequently Asked Questions FAQ Member List Members List
Go Back   Petri IT Knowledgebase Forums > Windows Scripting > General Scripting
Petri.co.il is happy to award auglan the title of Most Valuable Member !!!
Register Calendar Calendar Search Petri IT Knowledgebase Forums Search Todays Posts Today's Posts Mark Forums Read

Notices

Use contents of a text file as parameter to command line app?

Use contents of a text file as parameter to command line app?

this thread has 7 replies and has been viewed 2559 times

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 8th December 2008, 20:18
gforceindustries's Avatar
gforceindustries gforceindustries is offline
Senior Member
Wrote the book
 
 Join Date: Sep 2008
  6 month star 12 month star
 Location: Leics, UK
 Posts: 4,342
 Reputation: gforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the rough (305)
Default Use contents of a text file as parameter to command line app?

Evening all.

Here's one for you. Friend of mine has setup a GP that uses bmail to automatically send him an email whenever one of his users logs in or logs out (logon/logoff scripts). The email he receives has the subject

%username% logon or %username% logoff

where %username% is evaluated by the client before the mail is sent. The message body contains the contents of the text file c:\hostname.txt which is created on every machine by a startup script, and simply contains the machine's hostname.

What he now wants to do is have an email sent whenever a machine is switched on or off - the subject line would read %hostname% booted or %hostname% shutdown. The trouble is that there isn't a system variable called hostname, and he doesn't want to create one in order for this to work.

Bmail can use a text file as the source of the message body, but not the subject. Therefore, we need to figure out how to have the data read in from the textfile and used as the parameter to one of bmail's arguements.

The basic syntax is as follows:

bmail -s smtp_server -t send_to -f send_from -a subject

Can anybody suggest a way to have the contents of the text file extracted and used as the data for the -a parameter?

I've tried a few things, but not had much luck. Will list what I have tried shortly, I'm being kicked out of the office now.
__________________
Gareth Howells

BSc (Hons), MBCS, MCP, MCDST, ICCE

Any advice is given in good faith and without warranty.

Please give reputation points if somebody has helped you.

"For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

"Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.
  #2  
Old 8th December 2008, 21:28
gforceindustries's Avatar
gforceindustries gforceindustries is offline
Senior Member
Wrote the book
 
 Join Date: Sep 2008
  6 month star 12 month star
 Location: Leics, UK
 Posts: 4,342
 Reputation: gforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the rough (305)
Default Re: Use contents of a text file as parameter to command line app?

Ok, so what have I tried so far?

bmail -s server -t to -f from -a c:\hostname.txt - sends a message with the subject "c:\hostname.txt" - FAIL

bmail -s server -t to -f from -a < c:\hostname.txt - bombs out with a syntax error - FAIL

bmail -s server -t to -f from -a %1 < c:\hostname.txt - sends a message with the subject "%1" - FAIL
__________________
Gareth Howells

BSc (Hons), MBCS, MCP, MCDST, ICCE

Any advice is given in good faith and without warranty.

Please give reputation points if somebody has helped you.

"For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

"Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.
  #3  
Old 8th December 2008, 21:43
udell udell is offline
Casual
Casual
 
 Join Date: Dec 2008
  6 month star 12 month star
 Posts: 3
 Reputation: udell is on a distinguished road (15)
Default Re: Use contents of a text file as parameter to command line app?

You may consider using the for command.
Here is an example of a directory command using for:
for /F "usebackq" %i IN ('c:\') do dir %i

if you find that they are too many parameters then combine everything except the text into a batch file and do something like this:

for /F "usebackq" %i IN ('This is my message') do mybatch.cmd %i

Since you want it from a text file you would probably use:
for /F "usebackq" %i IN (`@type c:\message.txt`) do mybatch.cmd %i

Last edited by udell; 8th December 2008 at 21:55.. Reason: Mistook the intent of the original author and want to correct.
  #4  
Old 8th December 2008, 23:05
gforceindustries's Avatar
gforceindustries gforceindustries is offline
Senior Member
Wrote the book
 
 Join Date: Sep 2008
  6 month star 12 month star
 Location: Leics, UK
 Posts: 4,342
 Reputation: gforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the rough (305)
Default Re: Use contents of a text file as parameter to command line app?

Couple of thoughts.

Firstly, welcome to the forum, and for showing such a positive attitude with your first post. Thanks also for coming back and revising your suggestion based on a better understanding of the problem.

Secondly, good sir, fantastic suggestion why didn't I think of that?

I look forward to seeing more of you on these forums, particularly if this is the attitude you show towards helping people

Thank you again for your help. You've earned yourself a reputation point

For reference if anybody sees this thread and wonders "how do I do that", I have created a batch file to do the work. This batch file and bmail.exe should be stored together, unless you hardcode the path to bmail in the batch.

Single line in the batch reads as follows:

for /f %%i in (c:\hostname.txt) do bmail -s server -t to -f from -a "%%i Startup"

for the startup script, and

for /f %%i in (c:\hostname.txt) do bmail -s server -t to -f from -a "%%i Shutdown"

for the shutdown script.

A word of caution. An email will be sent for every entry in hostname.txt - which in this case is created by a startup script. The startup script executes the command hostname > c:\hostname.txt and replaces the file each time, thus ensuring that changes to the hostname are reflected at the next reboot. Take care to ensure that you overwrite the file, rather than appending to it, if you decide to experiment with this yourself.
__________________
Gareth Howells

BSc (Hons), MBCS, MCP, MCDST, ICCE

Any advice is given in good faith and without warranty.

Please give reputation points if somebody has helped you.

"For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

"Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.
  #5  
Old 8th December 2008, 23:37
udell udell is offline
Casual
Casual
 
 Join Date: Dec 2008
  6 month star 12 month star
 Posts: 3
 Reputation: udell is on a distinguished road (15)
Default Re: Use contents of a text file as parameter to command line app?

Thanks for your kind words.
  #6  
Old 9th December 2008, 00:10
gforceindustries's Avatar
gforceindustries gforceindustries is offline
Senior Member
Wrote the book
 
 Join Date: Sep 2008
  6 month star 12 month star
 Location: Leics, UK
 Posts: 4,342
 Reputation: gforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the rough (305)
Default Re: Use contents of a text file as parameter to command line app?

You're welcome
__________________
Gareth Howells

BSc (Hons), MBCS, MCP, MCDST, ICCE

Any advice is given in good faith and without warranty.

Please give reputation points if somebody has helped you.

"For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

"Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.
  #7  
Old 9th December 2008, 01:09
Rems's Avatar
Rems Rems is online now
Moderator
 
 Join Date: Mar 2005
  6 month star 12 month star
 Location: NL
 Posts: 2,265
 Reputation: Rems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to beholdRems is a splendid one to behold (903)
Default Re: Use contents of a text file as parameter to command line app?

I thought that hostname.exe returns the netbios name of the computer, which is the same name as when you expand the %computername% variable ..?
Code:
@for /f %%i in ( 'hostname.exe' ) do @Set "hostname1=%%i"
@echo Hostname: %hostname1% = Computername: %computername%

:: If both names are the same, then...
@Call Set "hostname=%computername%"
@echo expand hostname variable: %hostname%
If you like, you could also create a permanent machine variable %Hostname% (requires Administrator rights):
SetX.exe Hostname %hostname1% -m
The Variable will be added to HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\
SetX.exe is a resk tool



Quote:
Originally Posted by gforceindustries View Post
A word of caution. An email will be sent for every entry in hostname.txt - which in this case is created by a startup script. The startup script executes the command hostname > c:\hostname.txt and replaces the file each time, thus ensuring that changes to the hostname are reflected at the next reboot. Take care to ensure that you overwrite the file, rather than appending to it, if you decide to experiment with this yourself.
A way to avoid that a command will be executed for every line in the hostname.txt file, is to define a variable in the For-Do loop and run the bmail command on a new line for just one variable value.

Sample how to read only the last not empty line of a txt-file into a variable.
Code:
@for /f "usebackq" %%i in ("c:\hostname.txt") do @Set "hostname2=%%i"
@echo %hostname2%
Or, how to read only, lets say the 3rd line of a txt-file into a variable.
Code:
@for /f "usebackq SKIP=2" %%i in ("c:\hostname.txt") do @Set "hostname3=%%i" & Goto :exitFor
:exitFor
@echo %hostname3%
If you do not use "SKIP=n", only the first line would be read.
Alternative code to read only the first line would be;
Code:
@>nul Set/p hostname4=<"c:\hostname.txt"
@echo %hostname4%

But, it is better to overwrite the hostname.txt file anyways, rather than appending to it. No use to create a large hostname.txt file.

\Rems
__________________

This posting is provided "AS IS" with no warranties, and confers no rights.

__________________

** Remember to give credit where credit's due **
and leave Reputation Points for meaningful posts
  #8  
Old 9th December 2008, 01:35
gforceindustries's Avatar
gforceindustries gforceindustries is offline
Senior Member
Wrote the book
 
 Join Date: Sep 2008
  6 month star 12 month star
 Location: Leics, UK
 Posts: 4,342
 Reputation: gforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the roughgforceindustries is a jewel in the rough (305)
Default Re: Use contents of a text file as parameter to command line app?

Quote:
Originally Posted by Rems View Post
I thought that hostname.exe returns the netbios name of the computer, which is the same name as when you expand the %computername% variable ..?
Gnaaaaa (very technical expression)

You are quite correct. Ok then, first task for tomorrow is for me to look into why the computers at work don't have a COMPUTERNAME variable, when my machine at home does. Weak, former IT guy, lame.

Yes, hostname.exe does return the name, but I couldn't figure out a way of getting the output from that to use as a parameter.

Thank you for posting though, you've quite correctly pointed out that we're making this much harder than it needs to be, and that there's something odd about the machines at my office
__________________
Gareth Howells

BSc (Hons), MBCS, MCP, MCDST, ICCE

Any advice is given in good faith and without warranty.

Please give reputation points if somebody has helped you.

"For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

"Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

Last edited by gforceindustries; 9th December 2008 at 01:37..
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Line Ageless Windows Vista 7 30th November 2008 12:49
Closing an open shared file in command line. adat98 Windows Server 2000 / 2003 3 16th May 2008 23:17
Windows text file parser; I need to compare two text files... how? Nonapeptide Misc 10 30th April 2008 14:21
Command line help twrizzo Exchange 2000 / 2003 3 23rd September 2005 07:01
How to create a .ISO File and burn a CD, via command-line? borup General Scripting 1 28th January 2005 04:07


All times are GMT +3. The time now is 10:49.

Steel Blue 3.5.4 vBulletin Style ©2006 vBEnhanced
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
 

Valid XHTML 1.0!   Valid CSS!

Copyright 2005 Daniel Petri