Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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, 19:55. Reason: Mistook the intent of the original author and want to correct.

      Comment


      • #4
        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.

        Comment


        • #5
          Re: Use contents of a text file as parameter to command line app?

          Thanks for your kind words.

          Comment


          • #6
            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.

            Comment


            • #7
              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



              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

              Comment


              • #8
                Re: Use contents of a text file as parameter to command line app?

                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
                Last edited by gforceindustries; 8th December 2008, 23:37.
                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.

                Comment

                Working...
                X