?
Solved

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

Posted on 2011-02-22
5
Medium Priority
?
567 Views
Last Modified: 2012-05-11
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
Comment
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
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

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

path.txt
c:\foo\bar

Open in new window


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

Open in new window


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

Open in new window



call this way

setPathToFileContents mysymbol path.txt

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

Open in new window


0
 
LVL 3

Expert Comment

by:MARTiN
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

by:ProTek2
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

by:sjklein42
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

by:ProTek2
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

First some basics on Windows 7 Backup.  It has 2 components one is a file based backup which is stored in .zip files each zip is split at around 200 Megabytes and there is the Image Backup which is as the name implies a total image of the partition …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question