Advanced Help Needed!!! Loops Values Vars

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
*SCRIPTER*
Posts: 18
Joined: 16 Mar 2009 08:18
Location: N/A
Contact:

Advanced Help Needed!!! Loops Values Vars

#1 Post by *SCRIPTER* » 31 Mar 2009 04:37

This is a 3 part script to launch this script
Save the 1st script as "SCRIPT.BAT"
Save the 2nd script as "LOOP.BAT"
Save the 3rd script as "LOOP2.BAT"
Put them all in the same directory
From the command line type "SCRIPT"
Everything else needed to know is
Located inside the "SCRIPT.BAT"
If there is a way to combine these
Methods any ideas would be appreciated.
The system I tested This Command On
Reported As So...

1000=1sec+2000=7sec
10000=10sec+20000=12sec

1000=7sec
10000=12sec

Ultimately all the echo commands can be
Replaced And Depending
On the number value those commands could
be launched as needed before the next block
of code executes in the main script

Code: Select all

@ECHO OFF

:: ASSIGNS THE VAR NAMED "VALUE" TO TWO DIFFERENT USER DEFINED VALUES
:: BASED ON TWO USER DEFINED TIME RELEASED SWITCHES THAT RUN INSIDE
:: THE LOOP THEN RETURNS THE VARIABLE NAMED "VALUE" BACK TO "0", ONLY
:: IF SETLOCAL IS ATTACHED TO THE HEADERS OF THE LOOP SCRIPTS
:: OTHERWISE THE VALUE RETURNS TO THE FIRST USER STATED VARIABLE.
:: CHANGING THE TIME RELEASE VALUE NUMBERS WHILE APPENDING THE
:: "/SYNC" SWITCH TO THE END OF THE COMMAND WILL SET THE "VALUE"
:: NAMES ACCORDING TO THEIR VALUE NUMBERS GENERALLY THE IDEA BEHIND THIS
:: COMMAND WOULD BE TO ASSIGN BOTH USER DEFINED VALUES TO THE "VALUE"
:: VARIABLE SIMOTANIOUSLY INSIDE THE EXTENDED LOOP. INHERENTLY IF THE NUMBER
:: VALUE FOR THE FIRST VAR "VarName001" IS LESS THAN "VarName002" THE NEXT
:: COMMAND IN THIS SCRIPT WILL EXECUTE BEFORE THE SECOND LOOP IS REACHED.
:: IF THE "/SYNC" SWITCH IS NOT PRESENT THE FIRST VARIABLE WILL BE SET
:: ACCORDING TO ITS NUMBERED VALUE THE SECOND VARIABLE WILL SET AND THEN
:: EXECUTE ONLY AFTER THE FIRST VAR HAS PASSED SUCCESSFULLY

               REM BUGS:
               REM WHEN THE "/SYNC" SWITCH IS PRESENT AND BOTH NUMBERED
               REM VALUES ARE EQUAL THE VALUES ARE SET IN REVERSE
               REM BEING "VarName002" THEN "VarName001"
               REM WHEN IT SHOULD BE THE OTHER WAY AROUND WHEN
               REM THE NUMBERED VALUES ARE EQUAL IT SHOULD READ
               REM "VarName001" FIRST
               
         :: PROBLEMS:
         :: ANY VALUE BELOW "2" OUTPUTS AN ERROR
         :: LARGER VALUES INCREASE CPU USAGE


:: SCRIPT~COMMAND ::
   :: EXAMPLE ::
   
   CALL LOOP VarName001 100 VarName002 9000 /SYNC
   
   ECHO.
   
      
   ECHO Commands That Would Be Launched While
   ECHO "VarName002" Is Resolving Are Put Here
   ECHO Once VarName002 Has Resolved And CMD
   ECHO Exits, Both "VarName001" ^&^ "VarName002"
   ECHO Will Both Equal The Variable "VALUE"
   ECHO But Only From The Command Prompt
   ECHO They Will Not Act As They Do In A Script
   ECHO To Test This Theory Once "VarName002"
   ECHO Has Resolved Type ECHO !VALUE! And
   ECHO Hit Enter Then Type ECHO !VALUE! Again
   ECHO And Hit Enter Both User Vars Will Now Equal
   ECHO The Variable "VALUE" Make Sure To Enable
   ECHO Delayed Expansion First Or Type VALUE
   ECHO As Normal With %%%%% To Expand The Variable



Code: Select all

@ECHO OFF
TITLE RunLoop

:: The %Value% Variable Is The Variable
:: That We Will Assign Different Values To

   IF /I "%5" == "/SYNC" (SET SYNC=1)
   IF "%SYNC%" == "1" (SET SYNCCMD=%3) & (SET SYNCVAL=%4) & (START /B LOOP2) ELSE (GOTO :BEGIN*LOOP)

:BEGIN*LOOP

   SET VAR01=1

:LOOP001

   SET /A VAR01=%VAR01%+1
   IF "%VAR01%" == "%2" (GOTO :END001)
   GOTO :LOOP001

:END001

   SET SplitVar=%1
   SET Value=%SplitVar%
   ECHO.
   ECHO INSIDE OUR EXTENDED LOOP THE VARIABLE "VALUE"=%VALUE%
   
   IF "%SYNC%" == "1" (GOTO :EOF)

   SET VAR02=1

:LOOP002

   SET /A VAR02=%VAR02%+1
   IF "%VAR02%" == "%4" (GOTO :END002)
   GOTO :LOOP002

:END002

   SET SplitVar=%3
   SET Value=%SplitVar%
   
   ECHO.
   ECHO INSIDE OUR EXTENDED LOOP THE VARIABLE "VALUE"=%VALUE%



Code: Select all

@ECHO OFF
TITLE RunLoop
ENDLOCAL

:BEGIN*LOOP

   ECHO.

   SET VAR02=1

:LOOP002

   SET /A VAR02=%VAR02%+1
   IF "%VAR02%" == "%SYNCVAL%" (GOTO :END002)
   GOTO :LOOP002

:END002

   SET SplitVar=%SYNCCMD%
   SET Value=%SplitVar%
   
   ECHO.
   ECHO.
   ECHO INSIDE OUR EXTENDED LOOP THE VARIABLE "VALUE"=%VALUE%

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 01 Apr 2009 11:19

I'm not completely sure I understand what you're doing yet, but here's at least one thing I found:


SET Value=%SplitVar%

I think that line needs to be:

SET Value=!SplitVar! (with enabledelayedexpansion) OR

call SET Value=%%SplitVar%%

I think either of those options would work without the splitvar variable:

SET Value=!%1!

call SET Value=%%%1%%

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 01 Apr 2009 11:22

Is the purpose of this code:

Code: Select all

:LOOP001 

   SET /A VAR01=%VAR01%+1
   IF "%VAR01%" == "%2" (GOTO :END001)
   GOTO :LOOP001

:END001

and similar code just to have a time delay? You could use this instead:

Code: Select all

ping /n 5 127.0.0.1 >nul 2>nul


That will give you an approximate delay of 4 seconds (the "/n" value minus 1).

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 01 Apr 2009 11:37

If there is a way to combine these
Methods any ideas would be appreciated.

Do you mean combine the 2 files loop.bat and loop2.bat? You could do this:

Code: Select all

@ECHO OFF 
TITLE RunLoop

:: If %5 or %6 is 2NDLOOP, then go on to the 2nd part of the script
   IF "%6"=="2NDLOOP" GOTO :2NDLOOP
   IF "%5"=="2NDLOOP" GOTO :2NDLOOP


:: The %Value% Variable Is The Variable
:: That We Will Assign Different Values To

   IF /I "%5" == "/SYNC" (SET SYNC=1)
   IF "%SYNC%" == "1" (SET SYNCCMD=%3) & (SET SYNCVAL=%4) & (START /B LOOP %1 %2 %3 %4 %5 2NDLOOP) ELSE (GOTO :BEGIN*LOOP)

:BEGIN*LOOP

   SET VAR01=1

:LOOP001

   SET /A VAR01=%VAR01%+1
   IF "%VAR01%" == "%2" (GOTO :END001)
   GOTO :LOOP001

:END001

   SET SplitVar=%1
   SET Value=%SplitVar%
   ECHO.
   ECHO INSIDE OUR EXTENDED LOOP THE VARIABLE "VALUE"=%VALUE%
   
   IF "%SYNC%" == "1" (GOTO :EOF)

   SET VAR02=1

:LOOP002

   SET /A VAR02=%VAR02%+1
   IF "%VAR02%" == "%4" (GOTO :END002)
   GOTO :LOOP002

:END002

   SET SplitVar=%3
   SET Value=%SplitVar%
   
   ECHO.
   ECHO INSIDE OUR EXTENDED LOOP THE VARIABLE "VALUE"=%VALUE%
goto :eof

:2NDLOOP
ENDLOCAL

:2NDBEGIN*LOOP

   ECHO.

   SET VAR02=1

:2NDLOOP002

   SET /A VAR02=%VAR02%+1
   IF "%VAR02%" == "%SYNCVAL%" (GOTO :2NDEND002)
   GOTO :2NDLOOP002

:2NDEND002

   SET SplitVar=%SYNCCMD%
   SET Value=%SplitVar%
   
   ECHO.
   ECHO.
   ECHO INSIDE OUR EXTENDED LOOP THE VARIABLE "VALUE"=%VALUE%
goto :eof

*SCRIPTER*
Posts: 18
Joined: 16 Mar 2009 08:18
Location: N/A
Contact:

#5 Post by *SCRIPTER* » 01 Apr 2009 14:13

Ok the reason I did not use the ping was because genarally
This is meant to be a command with changeable parameters.
So,

LOOP VarName001 100 VarName002 9000 /SYNC

Is the command, the numbered values represent how long to take before
Setting or doing the statements inside that loop so the ping would not
be valid in this situation, or rather harder to use replaceable parameters
for the ping command inside a loop.
The problem Im having with the script as far as combining is with the
values. The scripts have been separated so as to allow them to run
Synchronized verses executing one block of code than another
This is also the reason for the /SYNC switch, otherwise varname001
counts down first than sets than varname002 counts down and sets.
At the same time, when I combine the methods and use a start /b
command my second loop is passed up if they both reside in the same script. :?
So basically,
I type from the command line

C:\>LOOP VarName001 1000 VarName002 9000 /SYNC
C:\>LOOP VarName001 9000 VarName002 1000 /SYNC
C:\>LOOP VarName001 5000 VarName002 5000 /SYNC
C:\>LOOP VarName001 5000 VarName002 5000

I am working on adding commands to the var names to extend
Commands within the initial command to execute on the user
Vars.

And I dont know if this is natural behavior but after I execute this
script or command rather when the command line returns home if
I type in:
ECHO %VALUE%
And hit enter then type the same thing again:
ECHO %VALUE%
I get out put like this

C:\>ECHO %VALUE%
VarName001

C:\>ECHO %VALUE%
VarName002

Then when I type:
C:\>ECHO %VALUE%
VarName001

It returns me to my original variable all
While using the same %VALUE% :?:
It seems to act like a paging system for
Variables.

To give you an idea of where I am headed with
this.
When this command is completed
We should be typing in something like

LOOP START/VarName001 100 START/VarName002 9000 /SYNC

Broken Down Sounds like:
Start varname001 at 1sec then start varname002 at 9secs and sync
both commands to start at the same time

The START command is the integration I am talking about as far
As adding onto so instead of setting we could start call to whatever
We want to execute on those vars, or...
Perhaps ASSIGN some FILE NAMES to the same values...
Within a loop then execute those statements when needed.
Last edited by *SCRIPTER* on 01 Apr 2009 19:16, edited 1 time in total.

*SCRIPTER*
Posts: 18
Joined: 16 Mar 2009 08:18
Location: N/A
Contact:

#6 Post by *SCRIPTER* » 01 Apr 2009 18:48

avery_larry wrote:I'm not completely sure I understand what you're doing yet, but here's at least one thing I found:

SET Value=!SplitVar! (with enabledelayedexpansion)


This is one thing that encounters an error.
Setting any delayed expansion or misplacing,
The SetLocal commands seems to counter act
against the cmd processors response after
execution of the command.

C:\>ECHO %VALUE%
VarName001

C:\>ECHO %VALUE%
VarName001

Outputs the same result aside from...

*SCRIPTER* wrote:ECHO %VALUE%
And hit enter then type the same thing again:
ECHO %VALUE%
I get out put like this

C:\>ECHO %VALUE%
VarName001

C:\>ECHO %VALUE%
VarName002

Then when I type:
C:\>ECHO %VALUE%
VarName001

It returns me to my original variable all
While using the same %VALUE%


Further analysis would propose on down the line
Maybe having something like . . .

LOOP START/{VarName001}-[TEXTFILE1.TXT] 100 START/{VarName002}-[TEXTFILE1.TXT] 9000 /SYNC /FORMAT /IMPORT-{SHELL}

Would:
Start VarName1 & Send The Output to TEXTFILE1.TXT after 100
Start VarName2 & Send The Output to TEXTFILE1.TXT after 9000
/SYNC the commands so that both vars start counting down to execution
/FORMAT (Output)
/IMPORT (To current Shell Environment)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#7 Post by avery_larry » 06 Apr 2009 15:20

I'm quite curious about what you're really trying to accomplish -- so let's start there with some questions.

Are you basically wanting to time delay and/or sync an execution of 2 different commands?

What is the purpose of changing the value of the 2 variables? Are they intended to be used simply to store the 2 commands you want to run, or do you have a function where you want to use the variable as 1 value for a while, and then as another value for a while, and then to set it back to what it was?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#8 Post by avery_larry » 06 Apr 2009 15:26

*SCRIPTER* wrote:
avery_larry wrote:I'm not completely sure I understand what you're doing yet, but here's at least one thing I found:

SET Value=!SplitVar! (with enabledelayedexpansion)


This is one thing that encounters an error.
Setting any delayed expansion or misplacing,
The SetLocal commands seems to counter act
against the cmd processors response after
execution of the command.
I'm not sure I follow you here. If you enabledelayedexpansion as necessary, it'll be fine. Perhaps you need to change your start command as I'll speak about farther down . . .
C:\>ECHO %VALUE%
VarName001

C:\>ECHO %VALUE%
VarName001

Outputs the same result aside from...

*SCRIPTER* wrote:ECHO %VALUE%
And hit enter then type the same thing again:
ECHO %VALUE%
I get out put like this

C:\>ECHO %VALUE%
VarName001

C:\>ECHO %VALUE%
VarName002

Then when I type:
C:\>ECHO %VALUE%
VarName001

It returns me to my original variable all
While using the same %VALUE%

Which I think is my point -- that you need to use delayedexpansion in order to get the value of the variable, not just the variable name. Or maybe I'm wrong here? Don't you want VarName001 to be evaluated as %VarName001% and then that value to be assigned to the variable VALUE? If so, then ECHO %VALUE% should NOT return VarName001, rather it should return whatever %VarName001% is . . ?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#9 Post by avery_larry » 06 Apr 2009 15:46

*SCRIPTER* wrote:Ok the reason I did not use the ping was because genarally
This is meant to be a command with changeable parameters.
So,

LOOP VarName001 100 VarName002 9000 /SYNC

Is the command, the numbered values represent how long to take before
Setting or doing the statements inside that loop so the ping would not
be valid in this situation, or rather harder to use replaceable parameters
for the ping command inside a loop.
I think you can accomplish this by using a number of seconds as the parameter you pass like this:

Code: Select all

loop varname1 1 varname2 9 /sync

Which would give the intention of a 1 second delay for varname1 and a 9 second delay for varname2 using this:

Code: Select all

echo.Delay %2 seconds
set /a numsec=%2+1
ping -n %numsec% -w 999 127.0.0.1>nul 2>nul

Is that what you want? the -n parameter to ping will with halfway decent precision, cause a delay of n-1 (so if you want to delay 5 seconds, you have to ping -n 6). It's much more consistent from machine to machine, where a for loop or goto loop that increments a variable is completely dependent on hardware.
The problem Im having with the script as far as combining is with the
values. The scripts have been separated so as to allow them to run
Synchronized verses executing one block of code than another
This is also the reason for the /SYNC switch, otherwise varname001
counts down first than sets than varname002 counts down and sets.
At the same time, when I combine the methods and use a start /b
command my second loop is passed up if they both reside in the same script. :?
Well, if I understand, that is actually what I was trying to accomplish in the way I combined the 2 scripts into 1. Using the parameters that you pass on the command line, you can combine multiple script files into 1 by using goto. Once you think about it, it's probably obvious. I can explain further with examples if you'd like . . .
So basically,
I type from the command line

C:\>LOOP VarName001 1000 VarName002 9000 /SYNC
C:\>LOOP VarName001 9000 VarName002 1000 /SYNC
C:\>LOOP VarName001 5000 VarName002 5000 /SYNC
C:\>LOOP VarName001 5000 VarName002 5000

I am working on adding commands to the var names to extend
Commands within the initial command to execute on the user
Vars.

And I dont know if this is natural behavior but after I execute this
script or command rather when the command line returns home if
I type in:
ECHO %VALUE%
And hit enter then type the same thing again:
ECHO %VALUE%
I get out put like this

C:\>ECHO %VALUE%
VarName001

C:\>ECHO %VALUE%
VarName002

Then when I type:
C:\>ECHO %VALUE%
VarName001

It returns me to my original variable all
While using the same %VALUE% :?:
It seems to act like a paging system for
Variables.
I think all of this can be explained by understanding your "START /b" statement. When you use START to begin a scipt file, it's actually starting a new cmd shell. You need to exit that shell to return to the original, which is effectively an endlocal. If you don't exit, then you stay in the new cmd shell. It's more flexible to actually START the cmd and tell it to execute the script file like this:

Code: Select all

start /b "title" cmd /c "script.bat"


and then you can use the various cmd switches as needed -- for instance, to enabledelayedexpansion:

Code: Select all

start /b "title" cmd /v:on /c "script.bat"

To give you an idea of where I am headed with
this.
When this command is completed
We should be typing in something like

LOOP START/VarName001 100 START/VarName002 9000 /SYNC

Broken Down Sounds like:
Start varname001 at 1sec then start varname002 at 9secs and sync
both commands to start at the same time

The START command is the integration I am talking about as far
As adding onto so instead of setting we could start call to whatever
We want to execute on those vars, or...
Perhaps ASSIGN some FILE NAMES to the same values...
Within a loop then execute those statements when needed.


Let me take a swing at something for you and see if it's what you're looking for . . .

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#10 Post by avery_larry » 06 Apr 2009 16:09

OK -- is this anything like what you're wanting or am I completely misunderstanding? . . .

(Completely untested)

Code: Select all

    ::  This file takes 4 required parameters and 1 optional parameter:
    ::  delayexec "command1" delay1 "command2" delay2 [/sync]
    ::  where:
    ::  command1 is a command that you want to execute
    ::  delay1 is how long you want to wait in seconds before executing command1
    ::  command2 is a 2nd command that you want to execute
    ::  delay2 is how long you want to wait in seconds before executing command2
    ::  The presence of the optional /sync parameter means that delay1
    ::  and delay2 occur simultaneously.
    ::      In this case, delay2 MUST be greater than delay1
    ::  The absense of the optional /sync parameter means that command2 will execute
    ::  delay2 seconds AFTER command1 begins.


@echo off
setlocal enabledelayedexpansion
::numsec1 is the number of pings to get a delay of %2 seconds
set /a numsec1=%2+1
if /i "%5"=="/sync" (
   set /a numsec2=%4-%2+1
   rem should put an error check here to make sure %4 is greater than %2 OR you could
   rem add code to figure out which one's first based on the delay value.
   ) else (
      set /a numsec2=%4+1
)

::Now we will delay for the first command
ping -n %numsec1% 127.0.0.1 -w 999 >nul 2>nul

::and then start the first command
start "Command1" %1

::You can put whatever start parameters you want -- in it's own window, in this window,
::wait for it to finish, /b to run in this window in the background, /i to reset the command environment etc.


::Now we will delay for the 2nd command
::we already did the math as to whether it should be /sync or not
::Note that for this method of /sync to work, you must not execute the first
::command with start /wait
ping -n %numsec2% 127.0.0.1 -w 999 >nul 2>nul
start "Command2" %3

::and we're all done.

*SCRIPTER*
Posts: 18
Joined: 16 Mar 2009 08:18
Location: N/A
Contact:

#11 Post by *SCRIPTER* » 07 Apr 2009 23:59

Avery you hit the nail on the head
I tested the code and it works like a gem I threw
The error in there to compensate for the change
and all runs smoothly.
Thank you for your assistance.

Post Reply