+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, Read a from a text file, set as a variable in Coding and Web Development; Possibly a simple task but I can't seem to find out how to do this! Two files: Drive.txt Command.bat Drive.txt ...
  1. #1
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 491 Times in 375 Posts
    Rep Power
    0

    Read a from a text file, set as a variable

    Possibly a simple task but I can't seem to find out how to do this! Two files:

    Drive.txt
    Command.bat

    Drive.txt simply contains "D:"

    How can I get the batch file to read "D:" in the drive.txt file and set it as a variable?

  2. #2
    TheLibrarian
    Guest
    The for command is your friend there.

  3. #3

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,157
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    A bit more detail - you just want the single line:

    Code:
    for /f %%i in (drive.txt) do set drive=%%i

  4. Thanks to srochford from:

    Rawns (27th January 2011)

  5. #4
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 491 Times in 375 Posts
    Rep Power
    0
    Thanks guys! One last search in Google pulled this up:

    Code:
    set /p Drive=<Drive.txt
    Seems to work a treat.

  6. #5
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    2,337
    Thank Post
    83
    Thanked 388 Times in 258 Posts
    Rep Power
    112
    This is a good reference for commands like FOR or SET

    Windows CMD Commands

  7. Thanks to somabc from:

    Rawns (27th January 2011)

  8. #6
    TheLibrarian
    Guest
    Quote Originally Posted by Rawns View Post
    Thanks guys! One last search in Google pulled this up:

    Code:
    set /p Drive=<Drive.txt
    Seems to work a treat.
    That will work as long as you don't have a requirement for processing more than one line.

  9. #7
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 491 Times in 375 Posts
    Rep Power
    0
    Quote Originally Posted by TheLibrarian View Post
    That will work as long as you don't have a requirement for processing more than one line.
    Nope, it will only ever be one line, but I can see this being useful so will explore the for command a bit more too.

  10. #8
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    2,337
    Thank Post
    83
    Thanked 388 Times in 258 Posts
    Rep Power
    112
    Didn't know about set /p and reading from a file this may come in useful!

SHARE:
+ Post New Thread

Similar Threads

  1. [PHP] read from text box, append to file
    By RabbieBurns in forum Web Development
    Replies: 23
    Last Post: 22nd October 2009, 01:28 PM
  2. Copy file across variable folder name
    By SimpleSi in forum *nix
    Replies: 11
    Last Post: 10th March 2009, 06:15 PM
  3. EARTHQUAKE: READ TEXT FIRST, IT REALLY PUTS LIFE IN PERSPECTIVE
    By mattx in forum Jokes/Interweb Things
    Replies: 10
    Last Post: 28th February 2008, 09:46 AM
  4. Batch: Text file -> Environment variable?
    By PiqueABoo in forum Scripts
    Replies: 5
    Last Post: 13th December 2006, 11:09 PM
  5. %Userprofile% Variable not being set.
    By drjturner in forum Windows
    Replies: 16
    Last Post: 23rd November 2006, 09:07 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •