Computing.Net > Forums > Programming > Batch - Get variable from text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch - Get variable from text file

Reply to Message Icon

Name: onecrazycoder
Date: October 1, 2008 at 14:51:23 Pacific
OS: Win XP Pro SP 2
CPU/Ram: Pent 4 / 1 GB
Product: Dell Optiplex GX620
Comment:

I have a text file with one line of text [may contain multiple words] that I need to 'read' back into my batch file as a variable. This variable is the directory where the user's IE Favorites will be copied to.

Here is what I have and it doesn't work.

set copydir=<"%USERPROFILE%\category.txt"
xcopy "%USERPROFILE%\Favorites" "%~dp0%copydir%\"
pause

The user's profile path and copydir path may have names with spaces thus the need for the quotes [I think]

Help please!

Jesus is the Rock - ROCK ON!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 1, 2008 at 16:31:44 Pacific
Reply:

What is this trying to do:

"%~dp0%copydir%\"


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: Judago
Date: October 2, 2008 at 01:01:38 Pacific
Reply:

If it's only one line:


set /p copydir=<"%USERPROFILE%\category.txt"

If it's not then you could use the for /f command with skip, a counter, an if statement, ect.

The folowing for command will set the last line of the text file:


for /f "usebackq delims=" %%g in ("%USERPROFILE%\category.txt") do set copydir=%%g

On another note %~dp0 will give you the path of the batch file so I presume category.txt contains directory names only and they are below the location of the batch file. If this is so you should only have to specify:


"%copydir%\"

And yes you will need quotes on file and directory names containing spaces for most tasks, cd/chdir is one that doesn't.


0

Response Number 3
Name: onecrazycoder
Date: October 2, 2008 at 10:19:30 Pacific
Reply:

Works like a charm. Thanks.

It seems like I am always off just enough to make it not work. ;-)

Jesus is the Rock - ROCK ON!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch - Get variable from text file

Batch read variables from text, do robocopy www.computing.net/answers/programming/batch-read-variables-from-text-do-robocopy/19307.html

Set word from text file as variable www.computing.net/answers/programming/set-word-from-text-file-as-variable/19634.html

Batch to edit a text file www.computing.net/answers/programming/batch-to-edit-a-text-file/15894.html