#1 03 Oct 2009 04:11

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Variables batch

Is there a way to get all the variables in one batch then call it into all the scripts you want? I got the time script from these forums, but I want to make the script it's own and have another file just call the time script to get the time. The reason I want this is because I want to make a script that will get the time before executing something, execute, then get the time again so it can subtract from the first one to get how long the script took to execute. I don't want to have to paste the time script into everything I do; that would make all the files that much bigger.

Thanks.

Offline

#2 04 Oct 2009 02:09

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: Variables batch

retro-starr wrote:

Is there a way to get all the variables in one batch then call it into all the scripts you want? I got the time script from these forums, but I want to make the script it's own and have another file just call the time script to get the time. The reason I want this is because I want to make a script that will get the time before executing something, execute, then get the time again so it can subtract from the first one to get how long the script took to execute. I don't want to have to paste the time script into everything I do; that would make all the files that much bigger.

Thanks.

If I understand -- I think you just want to create a separate .cmd file, and then in the other files you create, just use the call function to call that script.  If you're modifying/using variables between the 2 .cmd files, make sure you DON'T use any setlocal commands in the other script.

Make sense?

Offline

#3 04 Oct 2009 05:30

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

So I create let's say "var.cmd" and it has all the variables then I have another cmd (or batch file) that can't have setlocal? If so, could I use the setlocal in the "var.cmd"? Will I be able to do both ![var]! and %[var]%?

Scripter, that looks a lot more complex than the script I got from this site lol.

@echo off
setlocal
For /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") Do set "delims=%%a%%b%%c"
For /f "tokens=1-4 delims=%delims%" %%G in ("%time%") Do (
set _hh=%%G
set _min=%%H
set _ss=%%I
set _ms=%%J
)
:: Strip any leading spaces
set _hh=%_hh: =%

:: Ensure the hours have a leading zero
if 1%_hh% LSS 20 set _hh=0%_hh%

Echo The time is:   %_hh%:%_min%:%_ss%:%_ms%
endlocal
set _time=%_hh%:%_min%

I thought setting the exitlevel was dangerous or something...?

Offline

#4 04 Oct 2009 11:47

rumata
Member
Registered: 22 Sep 2009
Posts: 15

Re: Variables batch

The command interpreter is implemented so that it shares it's owned variables to the actual session. This means that if you define some vaiables within your script then these variables will be available out of the script. To prevent the propagation of internally used variables or it's overwriting i recommend you to use the couple of commands setlocal/endlocal. The first one stores previously defined variables and allows to overwrite variables safely. Before ending your script you have to restore variables by command endlocal. It restores values of variables previously stored by the command setlocal and clean up all variables that you used before. In the other words, the couple of commands setlocal/endlocal makes your variables as local and makes your scripting safely and easier. Try the code below as an illustration of my words:

@echo off

set var=123
set var

echo enter to :sub
call :sub
echo return from :sub

set var

goto :EOF

:sub
setlocal

echo before change
set var
set var=456
set var
echo after change

endlocal
goto :EOF

You have to obtain the following text:

var=123
enter to :sub
before change
var=123
var=456
after change
return from :sub
var=123

Offline

#5 05 Oct 2009 01:21

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

Thanks for the help with understanding the set/endlocal, but is there a way to get variables from another script? Call var.cmd then in test.cmd do "%_hh%:%_min%:%_ss%:%_ms%"?

Offline

#6 05 Oct 2009 16:57

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

I think the talk of "setlocal/endlocal" is getting you off track.  Consider these two scripts:

@echo off
echo:start of parent.cmd
set var=set by parent.cmd

set var

call child.cmd

set var

echo:end of parent.cmd
@echo off
echo:start of child.cmd

set var=changed by child.cmd
set var1=set by child.cmd

echo:end of child.cmd

Is this is the behaviour you are seeking?


cmd | *sh | ruby | chef

Offline

#7 05 Oct 2009 21:39

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: Variables batch

retro-starr wrote:

So I create let's say "var.cmd" and it has all the variables then I have another cmd (or batch file) that can't have setlocal? If so, could I use the setlocal in the "var.cmd"? Will I be able to do both ![var]! and %[var]%?

The opposite.  var.cmd cannot have a setlocal command.  The reason -- if it had a setlocal command, then when the script ends there is an automatic endlocal -- which returns the state of all variables back to what it was before you called the setlocal command -- which is, of course, exactly opposite of what you want to do.


Basically do what blues said.  Example:


var.cmd

@echo off
set datetime=%date%.%time%

script1.cmd

@echo off
setlocal enabledelayedexpansion
call var.cmd
echo It works -- datetime= !datetime!

script2.cmd

@echo off
set datetime=FAILURE!
call var.cmd
echo Success?  datetime=%datetime%
call var.cmd
echo updated datetime=%datetime%

Offline

#8 05 Oct 2009 22:06

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,108
Website

Re: Variables batch

"By putting ENDLOCAL and SET commands on one line you are able to SET a variable outside the SETLOCAL-ENDLOCAL block that refers to a variable created inside the block."
http://ss64.com/nt/endlocal.html

In other words

endlocal
set _time=%_hh%:%_min%

will give a very different result to

endlocal & set _time=%_hh%:%_min%

This gives you have the advantage of being able to share variables (as if no SET/ENDLOCAL was in force at all), but you also have the advantage that the only variables shared are the ones you specify in that last ENDLOCAL line - theres no chance of some other variable accidentally being clobbered because two of your scripts happen to use the same variable name.

It also makes it very easy to see or change variable names later, just look for the ENDLOCAL line

ENDLOCAL & SET _var1=blah & set _var2=stuff & set _var3=more

Offline

#9 06 Oct 2009 12:49

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

Simon Sheppard wrote:

endlocal & set _time=%_hh%:%_min%

Or like this, to make a long list easier to read/maintain:

(
    endlocal
    set "var1=%var1%"
    set "var2=%var2%"
    set "var3=%var3%"
    set "varN=%varN%"
)

cmd | *sh | ruby | chef

Offline

#10 07 Oct 2009 04:27

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

To answer my own question, yes you can have a variables only batch. Just create a batch full of variables (i.e. set "[name]=[value]") then just call it into another batch to get all the shit you need. Thank you all for the provided help. I like Scripters way of executing the timer batch (though it seems it or he deleted it).

Offline

#11 07 Oct 2009 04:50

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

The question now is that if a simple batch calls the variables batch to only use one or two things, will it eat up memory since it has to load all the other variables as well?

Offline

#12 07 Oct 2009 16:11

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: Variables batch

retro-starr wrote:

The question now is that if a simple batch calls the variables batch to only use one or two things, will it eat up memory since it has to load all the other variables as well?

Not enough to care -- unless you have hundreds (thousands?) of variables and/or you have a terrible computer.

Offline

#13 07 Oct 2009 18:03

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,108
Website

Re: Variables batch

retro-starr wrote:

I like Scripters way of executing the timer batch (though it seems it or he deleted it).

Yes, unfortunately a few useful posts were deleted when I deleted scripters account, (though he had already deleted 90% by that point) he was watching who was online, posting snarky comments and then deleting them 5 minutes later.

The next time something like this happens I will try disabling the account rather than completely deleting it, so the useful posts can be retained.

Offline

#14 08 Oct 2009 04:30

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

Dammit Scripter! lol. Well I remember enough to get me to recreate it. Also didn't know you could delete your own posts...Thanks for letting me know Sheppard.

Offline

#15 08 Oct 2009 10:33

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

If it's of any consolation, SCRIPTER's method (assuming I remember it correctly) looked to be quite badly flawed.

It was essentially breaking the time into its components (a good start) and then subtracting start-hour from end-hour, start-minute from end-minute, start-second from end-second, start-100ths from end-100ths and combining it all back together.  That's not going to give you the correct answer.  Consider a start time of 09:25:24.11 and an end time of 10:05:15.30 using that method.

You'd need to turn start and end times into a value in 100ths (or seconds/minutes if you don't need that sort of precision) -- remembering to remove zero padding -- subtract the start value from the end value and turn the result back into "real" time.  Of course, if you're crossing midnight, you'll need to take date changes into account too.

All in all, not a simple proposition -- though using Simon's DateMath.cmd script would take most of the pain away.

Last edited by bluesxman (08 Oct 2009 14:06)


cmd | *sh | ruby | chef

Offline

#16 08 Oct 2009 21:58

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,108
Website

Re: Variables batch

retro-starr wrote:

Also didn't know you could delete your own posts...

Not any more, I've turned that option off now.

I could also disable editing so that theres no 'rewriting of history' possible, but thats probably going too far.

Offline

#17 09 Oct 2009 13:52

jaffamuffin
Member
Registered: 21 Mar 2009
Posts: 19

Re: Variables batch

Here's an example, using not  data and time but the same principles of the 'function'-like ability of the CMD interpreter.

This one takes a path and either adds a slash or removes a slash and handles there being a slash there already  or not.......

Code to call it - this changes the actual variable as param 1 and 3 are the same variable name. (like PHP passing by reference (?))

REM remove trailing slash from pathtosearch if it has one, else leave it as is
SET PATHTOSEARCH=C:\temppath\with\a\slash\
CALL :fixSLASH PATHTOSEARCH REMOVE %PATHTOSEARCH%

call slash function:

@ECHO OFF
:fixSLASH

REM fixSLASH.bat
REM Tool to add a trailing slash to a path only if the path doesn't have one already and vice-versa.
REM %1 IS VARIBALE TO RETURN PATH IN
REM %2 IS ADD or REMOVE - required out put
REM %3 IS the ACTUAL PATH

SETLOCAL
SET VER=1.0
SET TMP1=%3
SET VARIABLE=%TMP1%
IF "%3"=="" GOTO :failedExit
SET DIDIT=No action was required.

IF "%2%"=="REMOVE" (
    IF "%TMP1:~-1%"=="\" SET VARIABLE=%TMP1:~0,-1%& SET DIDIT=Slash was removed.
)

IF "%2"=="ADD" (
    IF NOT "%TMP1:~-1%"=="\" SET VARIABLE=%TMP1%\& SET DIDIT=Slash was added.
)

(ENDLOCAL & REM
    SET RETURN=0
    SET %1=%VARIABLE%
    ECHO fixSLASH: Path %3 requested trailing slash %2.
    ECHO fixSLASH: %DIDIT%
    ECHO fixSLASH: Result: %VARIABLE%
)
GOTO :EOF

:failedExit
(ENDLOCAL & REM
    ECHO fixSLASH: Not all params supplied. Exiting fixSLASH.bat
    SET RETURN=1
)
GOTO :EOF

Last edited by jaffamuffin (09 Oct 2009 13:54)

Offline

#18 12 Oct 2009 00:36

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

To expand on my idea of calling the variables batch, is it possible to call a certain section inside the second script? Inside the var.cmd I have this section:

:wait
set /p "userinp=[time in milliseconds]: "
ping 123.45.67.89 -n 1 -w %userinp% > nul

So in another script can I do:

call var.cmd :wait

and still get the prompt to ask for the amount of milliseconds to wait?

Offline

#19 12 Oct 2009 09:13

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

Instead of asking if it's possible, why don't you just try it and see?  smile

(Yes, you can do what you ask, you'll just need to add "call %1" at the top of the script containing ":wait".)


cmd | *sh | ruby | chef

Offline

#20 16 Oct 2009 06:00

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

I did attempt. I did it a few different ways (the ":wait" seemed the most likely which is why I stuck with it), so I poised the question here. So it'll be:

call %1
...
:wait
set /p "userinp=[time in milliseconds]: "
ping 123.45.67.89 -n 1 -w %userinp% > nul

Then:

call var.cmd :wait

Offline

#21 16 Oct 2009 15:40

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

I reckon that should pretty much do it.


cmd | *sh | ruby | chef

Offline

#22 19 Oct 2009 03:36

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

Will this work for an number of sections I make? I want to make another section that pulls the system uptime. Could I just do

call var.cmd :uptime

Offline

#23 19 Oct 2009 09:11

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

I don't see why not.

Going one step further, you could even stack a bunch of commands by doing something like this:

@echo off
for %%a in (%*) do (call %%a)
goto :EOF

:command1

echo:%0 commands here

goto :EOF

:command2

echo:%0 commands here

goto :EOF

:command3

echo:%0 commands here

goto :EOF

:command4

echo:%0 commands here

goto :EOF

And in the "calling" script:

call var.cmd :command1 :command3 :command4

Last edited by bluesxman (19 Oct 2009 09:12)


cmd | *sh | ruby | chef

Offline

#24 20 Oct 2009 03:33

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

I called the section with the colon in front, but it didn't work. Did when I took the colon off. I had the idea of "goto :eof" (which is weird that you use the colon here, but not when calling) before coming here.

The next step for me would be setting it up so I don't have to call it in the cmd prompt. What I have is a shortcut to cmd.exe and arguements with it calling a script. The script sets a path so I can use cmdline tools. In that script will be the sections we are doing. So when the cmd prompt comes up, I'd have to do

call init.bat uptime

I want to avoid having to write the "call init.bat ..." and just do "uptime". Any suggestions?

Correction: I can't do the "call init.bat uptime" actually, just found out it's because I had the uptime.cmd in the folder. Once I took it out, no longer worked, but hopefully you get my point!

Last edited by retro-starr (20 Oct 2009 03:38)

Offline

#25 21 Oct 2009 01:06

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: Variables batch

So I think it sounds like, instead of having all the different little parts inside one big batch file, where you can call pieces of the file like we've been talking about here -- that instead you want to have each little piece of that file as its own batch file.

So take the :wait section and, instead, create wait.cmd.  Instead of the :uptime section, create uptime.cmd

You could still have a var.cmd file, which in turn could actually call both (or more) of these other little script files.  With all the individual little script files, then you could, at the command prompt, just type:

uptime

or

wait

Offline

#26 21 Oct 2009 01:38

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

I already have the scripts. The reason I wanted to make it in one script was to make a singular key script, but looks like I need the scripts to run via the cmdline...which sucks. If anybody has a solution to the question still, that's super!

Offline

#27 21 Oct 2009 11:01

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Variables batch

If I understand you correctly, you want to combine a whole bunch of scripts into one script but you want them to still have individual names?  You can't have it both ways!  You could perhaps create a whole bunch of .lnk files to point at the bundled up script, but then you'll have a bunch of .lnk files cluttering up the place instead of a bunch of .cmd files.


cmd | *sh | ruby | chef

Offline

#28 21 Oct 2009 18:19

avery_larry
Member
Registered: 11 Jul 2007
Posts: 266

Re: Variables batch

At the command line, you don't have to use the "call" command.  Just:

var uptime

or

var wait


And again, you can have the little script files and still have var.cmd as a larger file that calls the individual little files.

Offline

#29 22 Oct 2009 03:50

retro-starr
Member
Registered: 03 Oct 2009
Posts: 87

Re: Variables batch

Aww! I want it both ways! I guess I'll just have the cmds and vars.cmd will remain only vars.cmd (not variables + cmds).

Offline

Board footer

Powered by FluxBB