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

Author Topic: Converting all the lines of a text file into a variable.  (Read 2304 times)

0 Members and 1 Guest are viewing this topic.

ted_glen

  • Guest
Converting all the lines of a text file into a variable.
« on: May 02, 2008, 12:43:51 PM »
I have created a text file by interrogating an oracle database using sql.

This then gives me uptodate information. I need to pass all the lines from this file to a string for another programs to use.

I can read all the lines using
for /f %%i in (filename) do set str=%%i but this only sets the variable (str) to the last line of the file as expected I suppose.  How can I set the variable so that all the lines of the file appear in series see below;
My file looks like /asda@
                          /tesco@
                         /aldi@

and I want the variable to be equal to /asda@/tesco@/aldi
The final @ symbol must be omitted also and help how to do this would also be appreciated.

Dias de verano

  • Guest
Re: Converting all the lines of a text file into a variable.
« Reply #1 on: May 02, 2008, 01:55:09 PM »
joinup.bat

Code: [Select]
@echo off

REM this is crucial
setlocal enabledelayedexpansion

REM generate test file
echo /asda@>supermkts.txt
echo /tesco@>>supermkts.txt
echo /aldi@>>supermkts.txt
echo /morrisons@>>supermkts.txt
echo /lidl@>>supermkts.txt
echo /carrefour@>>supermkts.txt
echo /monoprix@>>supermkts.txt

REM count lines in file
set /a lastline=0
for /f %%S in (supermkts.txt) do set /a lastline=!lastline!+1

REM main loop

REM set line counter to zero
set line=0

REM set output variable to blank
set variable=
REM read each line in input file
for /f "delims=" %%S in (supermkts.txt) do (

        REM copy line into string
set string=%%S

REM add one to line counter
set /a line=!line!+1

REM if this is the last line remove the @
if !line! EQU %lastline% set string=!string:@=!

REM append the string to output variable
set variable=!variable!!string!

)


REM here is your output variable
echo %variable%

Code: [Select]
D:\>joinup.bat
/asda@/tesco@/aldi@/morrisons@/lidl@/carrefour@/monoprix

« Last Edit: May 02, 2008, 04:27:49 PM by Dias de verano »

ghostdog74



    Specialist

    Thanked: 27
    Re: Converting all the lines of a text file into a variable.
    « Reply #2 on: May 02, 2008, 07:43:26 PM »
    Alternatives,
    1) vbscript

    Code: [Select]
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    strMyFile = "c:\test\file.txt"
    Set objFS = objFSO.OpenTextFile(strMyFile)
    Do Until objFS.AtEndOfLine
    s=Trim(objFS.ReadLine) & s
    Loop
    If Right(s,1) = "@" Then
    WScript.Echo Mid(s,1,Len(s)-1)
    End If
    save as script.vbs and on command line
    Code: [Select]
    C:\test>cscript /nologo script.vbs
    /aldi@/tesco@/asda

    2) if you can download gawk from here and install.
    Code: [Select]
    { s=$0 s }
    END {
     gsub(/ +|@$/,"",s)
     print s
    }
    save as script.awk and on command line
    Code: [Select]
    C:\test>gawk -f script.awk file.txt
    /aldi@/tesco@/asda