none
Reading Lines from a Text File with a Command Line Batch File

    Question

  • Hi,

    I have a text file that has four lines of text as follows:

    Hello
    Planet
    Welcome
    Goodbye

    I have a batch file that I would like to read in each line and set each line to a variable.  For example my batch file is as follows:

    set /p hostname=<variables.txt
    set /p backupdrive=<variables.txt
    set /p backupdir=<variables.txt
    set /p xbackupdir=<variables.txt

    This does not work because it is only reading in the first line each time, so each variable is:
    hostname = Hello
    backupdrive = Hello
    backupdir = Hello
    xbackupdir = Hello

    I would like it to read:
    hostname = Hello
    backupdrive = Planet
    backupdir = Welcome
    xbackupdir = Goodbye

    I know I can manually change these variables in the batch file, but I have a bunch of other batch files that have the same variables.  And I would like to automate this process and only have the user edit one file (variables.txt). 

    I have searched for hours and days on this topic but I cannot seem to find anything.  If anyone can help, it would greatly be appreciated.  Thanks







    Thursday, August 14, 2008 12:29 PM

Answers

  • The for loop with the /f option is the ticket here.  In a batch file try:

    for /f %%a in (file.txt) do somecool logic you will have to write!


    It would be easier if all of the items were on a single line seperated by something like a comma.  Then you do:

    for /f "delims=," %%a in file.txt do set hostname=%%a & set backupdrive=%%b & backupdir=%%c & xbackupdir=%%d

    Have fun!

    C Shane Cribbs
    www.georgiatechnologies.com
    www.learningtree.com
    Friday, August 15, 2008 8:04 PM

All replies

  • The for loop with the /f option is the ticket here.  In a batch file try:

    for /f %%a in (file.txt) do somecool logic you will have to write!


    It would be easier if all of the items were on a single line seperated by something like a comma.  Then you do:

    for /f "delims=," %%a in file.txt do set hostname=%%a & set backupdrive=%%b & backupdir=%%c & xbackupdir=%%d

    Have fun!

    C Shane Cribbs
    www.georgiatechnologies.com
    www.learningtree.com
    Friday, August 15, 2008 8:04 PM
  • try this:

    set /p hostname=<variables.txt

    set /p backupdrive=<<variables.txt

    set /p backupdir=<<variables.txt

    set /p xbackupdir=<<variables.txt

    a double << works when you write to the file .

    Sunday, December 09, 2012 12:44 AM
  • html,body{padding:0;margin:0;font-family:Verdana,Geneva,sans-serif;background:#fff;}html{font-size:100%}body{font-size:.75em;line-height:1.5;padding-top:1px;margin-top:-1px;}h1{font-size:2em;margin:.67em 0}h2{font-size:1.5em}h3{font-size:1.16em}h4{font-size:1em}h5{font-size:.83em}h6{font-size:.7em}p{margin:0 0 1em;padding:0 .2em}.t-marker{display:none;}.t-paste-container{;left:-10000px;width:1px;height:1px;overflow:hidden}ul,ol{padding-left:2.5em}a{color:#00a}code, pre{font-size:1.23em}

    try this:

    set /p hostname=<variables.txt

    set /p backupdrive=<<variables.txt

    set /p backupdir=<<variables.txt

    set /p xbackupdir=<<variables.txt

    a double << works when you write to the file .


    It does not!

    >> works when you write to a file.  There is no << operator in Windows.

    Don'twasteany timeonthis. Anyone seeing this - the above is just more spam.

    I don't understandwhy people insist on posting nonsense.

    --------------------------------------------------------------------------------

    ¯\_(ツ)_/¯

     

    • Edited by jrv Sunday, December 09, 2012 10:15 PM
    Sunday, December 09, 2012 3:10 AM
  • please pay no attention to my first comment.

    Sunday, December 09, 2012 9:51 PM
  • please pay no attention to my first comment.

    If you were being serious and not just adding noise you can fix the post by editing it or just delete it if you cannot see how to fix it.

    Comments are welcome along with mistakes.  Your comment seemed a little like an attempt to be misleading.

    Sometimes it helps to test the idea before posting,  We all post with brain suspended at times.  That is OK if intended as a serious post.


    ¯\_(ツ)_/¯

    Sunday, December 09, 2012 10:20 PM