|
-
September 5th, 2000, 02:16 AM
#1
Redirecting Command Output
Does any one know any way to redirect the output of a command line utility (eg "date /t" for WinNT) to an environment variable. I have been able to redirect it to the screen and to a file but not to an environment variable.
Thanks.
-
September 5th, 2000, 02:53 AM
#2
Try the pipe symbol | the output of one command becomes the input of the other. Not sure if NT supports it though.
You also indicate that you want to hand it to an envoronment variable
see if this helps http://www3.sympatico.ca/rhwatson/dos7/
In the beginning there was the command line
-
September 5th, 2000, 03:21 AM
#3
NT handles the environment and variables differently from Dos. You better ask at the NT forum.
Ate
-
September 5th, 2000, 07:41 AM
#4
FWIW, here's one way to do it in DOS:
Code:
@echo off
ver|date>$temp$.bat
echo set date=%%4>current.bat
for %%x in (call del) do %%x $temp$.bat
del current.bat
echo The current system date, %date%, has been
echo stored in an environment variable named DATE.
echo.
ver|time>$temp$.bat
echo set time=%%3>current.bat
for %%x in (call del) do %%x $temp$.bat
del current.bat
echo The current system time, %time%, has been
echo stored in an environment variable named TIME.
echo.
echo (Tip: Type SET to view what's in the envionment).
-
September 6th, 2000, 12:25 AM
#5
Thanks for that Vernon, but your batch code does not seem to work. The output I get is as follows...
-------------------------------------
The name specified is not recognized as an
internal or external command, operable program or batch file.
The name specified is not recognized as an
internal or external command, operable program or batch file.
The current system date, , has been
stored in an environment variable named DATE.
The name specified is not recognized as an
internal or external command, operable program or batch file.
The name specified is not recognized as an
internal or external command, operable program or batch file.
The current system time, , has been
stored in an environment variable named TIME.
(Tip: Type SET to view what's in the envionment).
----------------------------------------
I have a pretty good understanding of DOS, but I am confused as to what your code is attempting to do.
Any chance that you could explain ?
-
September 6th, 2000, 12:30 AM
#6
You ask for it, you will get it!
Vern Will explain. He is brilliant
Remember, this is NT on the DOS forum!
In the beginning there was the command line
-
September 6th, 2000, 12:46 AM
#7
I don't really see how Win9x DOS or WinNT DOS makes any differance. Remember that all I did ask was how to get a command-line output be redirected to an environment variable.
If it can be done in Win9x DOS then it should also able to be done in WinNT DOS.
-
September 6th, 2000, 10:41 AM
#8
As the Windows OS improves the DOS window commands are fewer.
There are many things that can be done at a DOS prompt in WIN9x that can not be done in WINNT.
joe evans
-
September 6th, 2000, 08:26 PM
#9
Thanks jevans, I agree with you and everyone else that pointed out that you may not be able to do some things in Win9x that you can in WinNT. As I have found ....
For anyone that may be interested I have found a way of doing what I asked, problem is it only works on WinNT (how strange) and only due to the fact that it uses the command extensions of the FOR command.
http://www.ntfaq.com/Articles/Index.cfm?ArticleID=13580
I would still be interested in finding a way of doing this in Win9x DOS.
Thanks.
-
September 7th, 2000, 05:28 PM
#10
The above really isn't that complicated but, to make it a little easier to explain, let's start with just a basic BATch file (nothing fancy) that puts the current system date into an environment variable named date:
ver|date>$temp$.bat
echo set date=%%4>current.bat
call $temp$.bat
del $temp$.bat
del current.bat
What we ultimately need is a set command similar to the following in a BATch file:
set any_name=current_system_date
On the vast majority of MS-DOS/Windows9x systems, the date command displays the following two lines:
Current date is Wed 09-07-2000
Enter new date (mm-dd-yy):
and then pauses for user input. One way around this pause is to pipe the output of the ver command into the date command. Since ver doesn't output a valid date, the date command simply ignores it and exits. (Try ver|date at any DOS prompt to see).
Next we need the output from ver|date in a file instead of on the screen. To do this we can redirect it by appending a >$temp$.bat to the end of ver|date, like so:
ver|date>$temp$.bat
After DOS executes this line we'll have a BATch file named $temp$.bat in the current directory that contains the following data:
Current date is Wed 09-07-2000
Enter new date (mm-dd-yy):
If we were to try and run (execute) this $temp$.bat right now though, DOS would try to find an internal command or executable file named current because that's the first word on the first line. Note that it would also pass all the other words on that line, including the date we're after, to the current program as command line parameters.
So, what we need is a BATch (executable) file named current.bat that grabs that forth command line parameter, (the current system date), and sticks it into an environment variable. We could use anything we want for an environment variable name but since we're working with a date, let's simply use date.
echo set date=%%4>current.bat
That doubled-up percent signs forces DOS to put a %4 (instead of the contents of %4) into current.bat, like so:
set date=%4
Ah ha, the line that we were ultimately after. Now when we launch $temp$.bat it will run current.bat which will grab the forth parameter from the command line that launched it (which today will be 09-07-2000) and stick it into an environment variable named date.
Note here that the second line in $temp$.bat will never be processed by DOS because we're not using call (or command /c) to launch it. We are using call to launch $temp$.bat which means that DOS will return to our main BATch file when it has finished with $temp$.bat.
At this point we have done what we were after but, before we exit we really should cleanup:
del $temp$.bat
del current.bat
That's it. The current system date is now available for use in an environment variable named date. You can use echo %date% to see what the date environment variable contains or use the %date% portion wherever/whenever you need the current system date. (To see everything that's in your environment, type set. If the output scrolls off of the screen, use set|more).
--
You can also use this same idea to get the current system time into an environment variable named time. The only difference from the above would be to replace date with time on the first line and change %%4 to %%3 on the second. (Type ver|time to see). What we end up with is:
ver|time>$temp$.bat
echo set date=%%3>current.bat
call $temp$.bat
del $temp$.bat
del current.bat
If you want you can even combine these two, prepend it all with an @echo off (to hide the command lines as they're being processed) and also insert a command to display the current system date and time:
@echo off
ver|date>$temp$.bat
echo set date=%%4>current.bat
call $temp$.bat
ver|time>$temp$.bat
echo set date=%%3>current.bat
call $temp$.bat
echo The current system date and time is %date% %time%
del $temp$.bat
del current.bat
Not really that complicated after all, eh?
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
-
Forum Rules
|
|