Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Multi-Line file to Variable ...  (Read 2512 times)

0 Members and 1 Guest are viewing this topic.

CameronY

    Topic Starter


    Intermediate

    Multi-Line file to Variable ...
    « on: February 18, 2007, 08:33:38 PM »
    Hello all,

    Am trying to get the contents of a file into a variable.
    FILE CONTAINS: (but could be anything)
    [highlight]sql-job-1.sql
    sql-job-2.sql
    sql-job-3.sql[/highlight]

    Need to get it into a Variable like ... [highlight]sql-job-1.sql,sql-job-2.sql,sql-job-3.sql[/highlight] ... if possible.

    The purpuse is to include the list from the file into the body of an email ....
    set MailBody=The %Product% Instance `%Instance%` has performed one or more SQL updates/query's against the database `%SQLDatabase%`.  Please refer to the individual logfiles for each of the following ... [highlight]%SQLLOGLIST%[/highlight] with regards to their success.

    Hope I've provided enough of an explaination.
    Note:  DOS batch script indicdently uses SETLOCAL ENABLEDELAYEDEXPANSION .
    Many thanks in advance.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Multi-Line file to Variable ...
    « Reply #1 on: February 19, 2007, 05:12:50 AM »
    This might help:

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set str=
    for /f %%i in ([highlight]input.txt[/highlight]) do set str=!str! %%i,
    echo %str%

    The highlighted portion is a arbitrary file name.

    Good luck. 8-)

    « Last Edit: February 19, 2007, 05:13:28 AM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    CameronY

      Topic Starter


      Intermediate

      Re: Multi-Line file to Variable ...
      « Reply #2 on: February 19, 2007, 06:06:44 AM »
      Many thanks Sidewinder - figured you'd be the one to respond.
      I'd actually had everything ~but~ the preceeding 'set str=', thinking it was not necessary - why, oh why.
      The script I've written for work has been an eye opener (using `setlocal enabledelayedexpansion`) and have had to remember at what level of the script the executing code is to determine which operators (% / !) (if that's what you call them) to use.

      Again, many thanks.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Multi-Line file to Variable ...
      « Reply #3 on: February 19, 2007, 06:45:10 AM »
      The set str= is defensive coding. You never know what user environment variables have been defined previously.

      Glad it it worked for you. Stop by any time.  8-)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein