![]() |
|
|
|||||||
| Petri.co.il is happy to award auglan the title of Most Valuable Member !!! |
| Register | Calendar |
Search |
Today's Posts |
Mark Forums Read |
| Notices |
|
|
Use contents of a text file as parameter to command line app?this thread has 7 replies and has been viewed 2559 times
|
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||||||||
|
||||||||||
|
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
|
||||||||||
|
||||||||||
|
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
|
||||||||
|
||||||||
|
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
|
||||||||||
|
||||||||||
|
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 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
|
||||||||
|
||||||||
|
Thanks for your kind words.
|
|
#6
|
||||||||||
|
||||||||||
|
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
|
||||||||||
|
||||||||||
|
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% 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:
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%
Code:
@for /f "usebackq SKIP=2" %%i in ("c:\hostname.txt") do @Set "hostname3=%%i" & Goto :exitFor
:exitFor
@echo %hostname3%
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
|
||||||||||
|
||||||||||
|
Quote:
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.. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |