Re: Persisting env vars in cmd windows
- From: "Pegasus \(MVP\)" <I.can@xxxxxxx>
- Date: Fri, 10 Nov 2006 14:40:50 +1100
Hi Richard,
It's always a little disappointing when preparing a fine batch file
solution for a poster and when that person takes no further interest
other than using the ready made solution. There is much more fun
in showing a poster what can be done, i.e. responding to a lively
interest from that person.
The help text for the command you question goes like this:
=================
%PATH:~10,5%
would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.
=================
So how does this relate to "%date:~0,3%"? Here we go:
- %date% is an inbuilt variable. You can see its value and
format by typing this: echo %date%
- The first 3 characters of %date% are "Fri".
- The instruction "%date:~0,3%" will take the first 3 characters,
starting at the very beginning (position 0), hence it will write
"Fri" to the screen.
Depending on what programming language you have used, you
might have called this a substring, leftstring or midstring.
I suggest you plug in some other numbers, e.g.
echo "%date:~4,8%" or
echo %date:~4%
Enjoy!
"Richard" <RichardDummyMailbox58407@xxxxxxxxxxxxxxxxxxx> wrote in message
news:1163127129.738522.168310@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Pegasus,message
I don't think that Set /? documents "%date:~0,3%" very well. But no
matter, because executing your examples shows that the expression is
akin to Active Server Pages or Embedded Ruby.
I fear I've taken up too much of your time. Thanks for your responses.
BTW, I'll go back and click stars for some of them.
Best wishes,
Richard
Pegasus (MVP) wrote:
The syntax for the two sample commands is visible when you type
set /?
even though it applies to any command, e.g. echo, copy,
del etc.
Similarly the syntax for the extremely powerful "for" command
is visible when you type
for /?
"Richard" <RichardDummyMailbox58407@xxxxxxxxxxxxxxxxxxx> wrote in
foldernews:1163044747.681567.64680@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Pegasus,
echo Today is %date:~0,3%
echo The time is %time::=.%
Thanks for the additional "toys". I've recorded them in my test
theK:\_Projects\CmdScripts\Time&Date and tested them.message
I've got perhaps a dozen sites (half from Microsoft) on command
scripts, but I didn't spot any documentation on this specific syntax.
Can you point me to an on-line source?
you must make sure that neither side of the = sign is ever blank.
Duly noted. Thanks.
Regarding starting up several command windows with Start commands,
being able to set the captions in those windows is very helpful. When
I set them up manually before, I only had useful captions on windows
that were active running some command, e.g. the Ruby/Rails logger or
MySQL.
Again, many thanks for your advice.
Best wishes,
Richard
Pegasus (MVP) wrote:
Thanks for the feedback. You'll probably find out a lot
more about batch files as you go on, especially when it
comes to the "set" and "for" commands. Try this from
a Command Prompt just for fun:
echo Today is %date:~0,3%
or
echo The time is %time::=.%
About testing equality without surrounding quotes or brackets
if %result%==OK
you must make sure that neither side of the = sign is ever blank.
If there is the slightest possibility, add some extra characters.
If you don't then the batch file will fail.
"Richard" <RichardDummyMailbox58407@xxxxxxxxxxxxxxxxxxx> wrote in
news:1163009213.990774.115580@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Pegasus,
Great batch template! Thanks.
I tested all the features. I was surprised by a couple of things,
like the "/i" switch and the use of OK without surrounding quotes.
But the piece de resistance was the "start" command, which you
mentioned earlier but I hadn't picked up on right away. Along with
windows Iother improvements, I got to it now and got exactly what I've got
exactly what I want:
Startup.bat
----------------
START "BAT1 Commands" Bat1.bat
START "BAT2 Commands" Bat2.bat
: etc
let's me double-click its name and get precisely the command
almostwant to support my Ruby/Rails application development efforts.
It's embarrassing to admit that I've used Windows' bat files for
decades but never noticed some of the features you've mentioned.
Again, thanks for your expert help.
Best wishes,
Richard
Pegasus (MVP) wrote:
When you ask about combining your various batch files
into the one file then you're spot on. The general idea is:
- Have all code for the one project in one single batch file.
- Make it highly structured.
- Avoid spaghetti code.
- Set global variables.
- Add lots of comments.
If you adhere to these rules then you will find that your
batch files become much more robust and are very easy
to maintain. You also skirt around the environmental variable
issue that triggered this thread.
I have seen contiguous (rather than modular) batch files that
extend over 300 lines, with numerous goto statements that
resemble the path taken by a hare in full flight. They are
inimpossible to maintain. In a structured form even a beginner
could debug and modify them.
Here is how I structure all my batch files:
=============================
@echo off
goto Start
--------------------------------------------
Program description:
Parameters:
Prerequisites:
Version:
Author:
Date:
--------------------------------------------
:Start
set SourceDir=D:\Some Folder
set TargetDir=E:\Other folder
call :Sub1
call :Sub2 %name%
if /i "%result%"==OK call :Sub3
goto :eof
--------------------------------------------
Some comments about Sub1
--------------------------------------------
:Sub1
Code for Sub1
goto :eof
--------------------------------------------
Some comments about Sub2
--------------------------------------------
:Sub2
Code for Sub2
goto :eof
"Richard" <RichardDummyMailbox58407@xxxxxxxxxxxxxxxxxxx> wrote
for %1longermessage
news:1162878332.440957.299980@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Pegasus,
OK, I've simplified these scripts substantially, e.g.
C130_StartLocalWebServer.bat
(down to 8 lines instead of 20 because the error checking no
needed)
---------------------------------------------
@echo off
call CreatePathAndSymbols.bat _Pf Prmp
echo ::
echo :: Starting WebBrickServer
echo :: in %AppDir%
echo ::
@echo on
ruby script/server
That's just what I wanted.
I appreciate the observations you made, i.e.:
1. Checking for existence of %2 obviates the need to check
at2. :EOF is built-in, obviating the need to define a symbol
testing athe
end
of the script
Mainly, I rely on 3 of my ~20 batch commands to start-up
bathduration ofRuby/Rails app. I keep those command windows open for the
a testing/developing session.
Heretofore, I opened three command windows and exectuted two
e.g.commands in each.
With your help, I've cut that down to one command in each.
Is there a way to combine that into a single batch command,
wholesimplification.
CombineCommand.bat (pseudocode)
---------------------------------
run cmd.exe; execute batch1.bat
run cmd.exe; execute batch2.bat
run cmd.exe; execute batch3.bat
and achieve the same result I'm getting now?
Again, thanks for your help.
Regards,
Richard
Pegasus (MVP) wrote:
Wow! This is one complex script!
Unfortunately it would take me far too long to analyse and
reverse-engineer it. There is certainly scopy for
Here are a few minor ones:
Of the two successive statements
if (%1) == () goto PARM_MISSING
if (%2) == () goto PARM_MISSING
the first one is superfluous. If %2 is blank then %1
must be blank too!
You have lots of "goto EOJ" statements. Replace them
with "goto EOF". This is an inbuilt label that does not
need to be defined.
You write
%RubyDisk%
cd %AppDir%
This should be simplyfied to
cd /d %AppDir%
(where %AppDir% now includes the drive letter).
However, this is just cosmetic stuff. I suspect that the
problemsset of batch files can be condensed to perhaps twenty lines
in a single batch file, which will solve your variable
mayand will make debugging and further development much, much
easier. You would get a short, sharp and robust batch file.
To suggest how it could be done requires your functional
description of what you're attempting to do. But then you
wrotedecide that "GO"s suggestion solves your problem, in which
case I will leave you to it.
"Richard" <RichardDummyMailbox58407@xxxxxxxxxxxxxxxxxxx>
news:1162789861.293438.209190@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxin
message
ofwant toHi Pegasus,
Thanks for responding.
I've got PATH= ... ; K:\_Projects\Ruby\__Links; ...
Below are four batch files. Typically, when I boot up and
succession.work
on the Prmp project, I open several Command windows in
The first thing I do with each of them is drag in the name
commandand
execute a couple of batch commands, e.g.
1a. Drag the name of C000_CreateSymbols.bat into a new
arewindow
1b. Click the command window to give it focus
1c. Press Enter to execute the command, where upon
cwd=K:\_Projects\Ruby\_Pf\Prmp
(and symbols RubyDisk, RubyProj, CliName and AppName
stepcommanddefined)
2a. Drag the name of C130_StartLocalWebServer.bat into the
window
2b. Click the command window to give it focus
2c. Press Enter to execute the command,
whereupon the ruby starts up the WEBrick web server.
Then I open a second command window, repeat the compound
any1
command,and
follow that with a command step 2 with a different batch
e.g.
C160_StartBreakpointer.bat, which starts a listener for
anbreakpoint
command executed in my Ruby/Rails application and starts
occursinteractive
ruby session initialized with the application's state.
BTW, as I was documenting my environment for you, it
startto me
that
I can install an invocation of the followin line at the
completelyof
all
my
commands (other than C000_...) and ignore C000_...
kludgy(and
eliminate all the error traps):
CreatePathAndSymbols.bat _Pf Prmp
I think that'll work. But you can probably show a less
theway--------------------------------------------------------------------------
to
do the whole thing.
Best wishes,
Richard
K:\_Projects\Ruby\__Links\CreatePathAndSymbols.bat
--------------------------------------------------------------------------characters------
@echo off
::: Call with arguments ClientName and ApplicationName
:: Note: Names may not include spaces nor most special
::
if (%1) == () goto PARM_MISSING
if (%2) == () goto PARM_MISSING
::
set RubyDisk=K:
set RubyProj=%RubyDisk%\_Projects\Ruby
::
set CliName=%1
set AppName=%2
::
set CliDir=%RubyProj%\%CliName%
set AppDir=%CliDir%\%AppName%
::
%RubyDisk%
cd %AppDir%\
::
path=%CliDir%\Cmds\;%path%
goto EOJ
::
:PARM_MISSING
echo ::
echo :: ERROR ERROR ERROR ERROR
echo :: Usage: %0 ClientName ApplicationName
echo :: ERROR ERROR ERROR ERROR
echo ::
:EOJ
K:\_Projects\Ruby\_Pf\Cmds\C000_CreateSymbols.bat
Window------
@echo off
CreatePathAndSymbols.bat _Pf Prmp
pause
C130_StartLocalWebServer.bat
---------------------------------------------
@echo off
if [%CliDir%] == [] goto ERROR
if [%AppDir%] == [] goto ERROR
if [%RubyDisk%] == [] goto ERROR
%RubyDisk%
cd %AppDir%
echo ::
echo :: Starting WebBrickServer
echo :: in %AppDir%
echo ::
@echo on
ruby script/server
@echo off
goto EXIT
:ERROR
@echo ERROR ERROR ERROR ERROR
@echo Symbol "AppDir", etc., not defined!
@echo ... run CreatePathAndSymbols.bat in your Command
Window@echo ERROR ERROR ERROR ERROR
:EXIT
C160_StartBreakpointer.bat
----------------------------------------
@echo off
if [%CliDir%] == [] goto ERROR
if [%AppDir%] == [] goto ERROR
if [%RubyDisk%] == [] goto ERROR
%RubyDisk%
cd %AppDir%
echo ::
echo :: Starting a Breakpointer
echo ::
@echo on
ruby script/breakpointer
@echo off
goto EXIT
:ERROR
@echo ERROR ERROR ERROR ERROR
@echo Symbol "AppDir", etc., not defined!
@echo ... run CreatePathAndSymbols.bat in your Command
wrote@echo ERROR ERROR ERROR ERROR
:EXIT
Pegasus (MVP) wrote:
"Richard" <RichardDummyMailbox58407@xxxxxxxxxxxxxxxxxxx>
news:1162763712.640301.320540@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxin
message
(environmentI've got a .bat file that sets a number of symbols
associatedvars)
that are useful in a number of other command windows
with
one of several projects. Right now, I operate by
1 opening a command window
2 draging the name of the symbol-setting bat file into
thenew
cmd
window and pressing Enter
3 draging the name of one of my utility bat files in
needed.cmd
window
and
pressing Enter
4 proceeding manually entering other commands as
symbol-setter
I'd like to eliminate step 2 by invoking the
byat
the
beginning of every other utility file. I tried this
session.using
because"cmd
/c
symbol-setter.bat" to no avail. I believe that fails
env.the
"cmd
/c" command starts a new session in which to set local
vars
and
then ends, never communicating with the invoking
write a
The best solution I can conceive of is to manually
techniquesenv..reg
file
appropriate to each project in which I want persistant
projectvars,
project,thus
only needing to invoke that once at the inception of a
maybe
with another one to delete all such entries should the
ultimately be destroyed.
Any comments?
Regards,
Richard
It seems you're trying to combine two different
fundamentallywith each other: Running batch files (which is
batcha Command Prompt activity) and dragging files (which is
a GUI activity). A better way would be to run a single
variablesfile and perhaps specify some parameters for it.
To obtain correct advice about your environmental
shouldyou should post the contents of your batch file. You
withinalso state if the variables are supposed to be valid
ofthe context of this batch file only, within the context
the
Command Prompt session it runs or within the context of
all programs, regardless of when they are launched.
.
- Follow-Ups:
- Re: Persisting env vars in cmd windows
- From: Richard
- Re: Persisting env vars in cmd windows
- References:
- Persisting env vars in cmd windows
- From: Richard
- Re: Persisting env vars in cmd windows
- From: Pegasus \(MVP\)
- Re: Persisting env vars in cmd windows
- From: Richard
- Re: Persisting env vars in cmd windows
- From: Pegasus \(MVP\)
- Re: Persisting env vars in cmd windows
- From: Richard
- Re: Persisting env vars in cmd windows
- From: Pegasus \(MVP\)
- Re: Persisting env vars in cmd windows
- From: Richard
- Re: Persisting env vars in cmd windows
- From: Pegasus \(MVP\)
- Re: Persisting env vars in cmd windows
- From: Richard
- Re: Persisting env vars in cmd windows
- From: Pegasus \(MVP\)
- Re: Persisting env vars in cmd windows
- From: Richard
- Persisting env vars in cmd windows
- Prev by Date: Re: Persisting env vars in cmd windows
- Next by Date: Re: SMART: HDD Failure Is Imminent
- Previous by thread: Re: Persisting env vars in cmd windows
- Next by thread: Re: Persisting env vars in cmd windows
- Index(es):
Relevant Pages
|