Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# How to import a text file to a environment variable in a batch file

Posted on 2011-02-22
Medium Priority
593 Views
I am trying to use a path previously stored as a text file to set an environment variable in a windows batch file. I had thought that type file.txt >> %var% would work but it didn't. This should be simple but I need help figuring it out.
0
Question by:ProTek2
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 16

Accepted Solution

sjklein42 earned 2000 total points
ID: 34952758
File containing path:

path.txt
c:\foo\bar


command:
for /F %i in (path.txt) do set mysymbol=%i


inside a batch file setPathToFileContents.bat (note doubled %):
@for /F %%i in (%2) do @set %1=%%i


call this way

setPathToFileContents mysymbol path.txt

set
...
mysymbol=c:\foo\bar
...


0

LVL 3

Expert Comment

ID: 34952763
Hi, I think you should use "Echo" instead of "Type" if you'd be doing it that way or instead use the "SET" command inside of the batch file instead.
0

Author Closing Comment

ID: 34955398
I'm sure that my inexperience with scripting was the only reason that I didn't follow it exactly. However, I realized after that problem was solved that I hadn't asked the right question. The answer saves the path but it gives no way to find WHERE it is saved when needed.
0

LVL 16

Expert Comment

ID: 34955744
Yes.  You can use the "path" by referring to it as %mysymbol%

For example, after calling

setPathToFileContents mysymbol path.txt

You can then use that path:

dir %mysymbol%

Of course, you don't need to call it "mysymbol".
0

Author Comment

ID: 34956399
Not the way I'm using it. The initial batch file will be started by a downloaded setup package and in that .cmd file, PowerShell is used to restart it with the "runas" parameter because administrative privileges are required for the other activities, not the least of which is moving a file into the system path for subsequent use. When the cmd shell is invoked, it is running in c:\windows\system32 instead of the download folder and the normal variables become null. The path may have been saved in a file but the path to the file (which holds the path to the file) is also lost. It turns out that a PushD created variable survives the elevation process and I used that to concatenate the "move" directive that I needed.

But thank you for your information. I'm sure that it will come in handy at some point.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month9 days, 5 hours left to enroll