A simple DOS question

Mar 10, 2005 16:03

I'm looking for the DOS equivalent for this command:
foo=`cat bar`
Obviously, this sets the value of the variable foo to be equal to the contents of the file bar.

I want to do the same thing in a DOS shell.
Using Cygwin doesn't count. Perl isn't available. Bonus points for using only the tools that come with the Windows OS.
setx /f bar foo /a ( Read more... )

Leave a comment

Comments 10

7ghent March 11 2005, 01:00:20 UTC
Why aren't you using set instead of setx?

Reply

attutle March 11 2005, 05:22:30 UTC
I'd be happy to use set, but it doesn't support some of the options that setx provides.

I gave setx as an example that doesn't do what I want, but does allow setting an environment variable from a file. The problem is that it sets the global environment and that isn't available until I start a new command prompt. Using setx and starting a subshell of the existing shell doesn't work either, I tried that.

If I could set a variable based on the global environment I could use setx to read the file and set the global variable as an intermediate step.

Reply

7ghent March 11 2005, 06:14:25 UTC
As glitch has mentioned, set /p should work.

Reply


killbox March 11 2005, 01:09:17 UTC
not sure exactly what you are trying to do, i have not done dos batch programing in years (atleast more than very simple scripting)

http://gearbox.maem.umr.edu/batch/canned.responses.html
may help some,
http://gearbox.maem.umr.edu/batch/batchtoc.htm
and
http://www.batchfiles.co.nr/

Reply

attutle March 11 2005, 05:35:25 UTC
The second method for the last question on the last of those three pages does what I want. Thanks.

I guess I wasn't missing a simple solution:

@echo off
:: filename.ext is the file to be processed
copy /y filename.ext %temp%.\t1.bat > nul
echo eEA BE 00 01 46 80 3C 0A 75 FA 81 EE> %temp%.\t2.dat
echo eF5 FA 00 89 F1 53 45 54 20 25 31 3D>> %temp%.\t2.dat
echo g=EA F9>> %temp%.\t2.dat
for %%? in (wF9 q) do echo %%?>> %temp%.\t2.dat
DEBUG %temp%.\t1.bat < %temp%.\t2.dat > nul
:: Set below the variable name where the string should be saved
call %temp%.\t1.bat MyString
for %%? in (t1.bat t2.dat) do del %temp%.\%%?

Yeah, that's the ticket. Construct a .COM file on the fly and run that. I suppose this is a good indicator that better alternatives are hard to come by.

What a silly operating system.

Reply


unixronin March 11 2005, 01:11:47 UTC
What I used to do to achieve this end was to generate a batch file on the fly, then source it.

Reply

7ghent March 11 2005, 01:13:08 UTC
Not a bad idea.

Reply

attutle March 11 2005, 05:18:53 UTC
Okay.... I thought of that, but couldn't figure out how to make it happen.

If I had echo -n it would be easy.

Can you give me an example?

Starting with an unknown value contained in a single line of a known file, I want to get the value into an environment variable in the current shell (command prompt).

Reply

unixronin March 11 2005, 06:31:32 UTC
I don't remember exactly the technique that I used to use. I haven't had the scripts that did the nasty in almost fifteen years, and no longer remember how I did it. But it looks like you found a solution.

Reply


should work glitch__ March 11 2005, 05:35:37 UTC
set /p foo=discarded < bar
assuming of course that your dos shell version supports the /p switch for set. (win 2k and above ships with it, I don't know about earlier). The 'discarded' part doesn't matter, it's just an input prompt for a user, into which contents of 'bar' is being fed.

Reply


Leave a comment

Up