How to modify a numeric variable value with Windows batch [duplicate] - windows

This question already has answers here:
Calculating the sum of two variables in a batch script
(6 answers)
Closed 6 years ago.
I have a script file script_A.cmd containing a lot of commands, including the following:
set NUMBER_RUN=1
This script calls another script called stript_B.cmd.
During the run of script_B.cmd, I want to update the script_A.cmd and increment the value of the NUMBER_RUN value by 1. In other words, after the first run, it should change that text in script_A.cmd to
set NUMBER_RUN=2
and so on for subsequent runs. So this requires both batch arithmetic and some kind of search/replace to change the actual text in script_A.cmd accordingly.
How do I do that, without using any tools downloaded from the internet, just Windows native batch?

Automatic change of code is a bad idea. Better use a file to store values, like:
script_B.cmd (reading the number from the file, incrementing it and writing it back)
<count.txt set /p Number_Run=
set /a Number_Run +=1
>count.txt echo %Number_Run%
First line reads the counter from a file, second line increases it by one, and the third line rewrites it to the file again.
script_A.cmd (just read the counter from the file)
<count.txt set /p Number_Run=
echo %Number_Run%

Related

Stopping an executable running if vbs loop if outside criteria is met

I'm using a VBScript to write into a configuration file config.txt, then run an executable SomeName.exe with the configuration I set.
The excecutable is not mine, I cannot interact with it.
The result is written in another text file Result.txt.
In the end, it looks like this
Set objShell = CreateObject("WScript.Shell")
For MyParameter = mystart to myend
'here I overwrite conf.txt with MyParameter value
Rt = objShell.run(SomeName.exe, 1, True) '--> True means "wait until the end before processing"
Next
What I'd like to do is to check the result.txt file and, if it's ok, stop the .exe and resume the loop with next value of MyParameter.
I already know how to read the file and decide whether the result is good enough or not (basically I read the last line and compare it with something else, very easy stuff) with a second script.
What I don't know is how to make this two scripts work together.
For now the only way I have is to run manually my second script and make it check from time to time (with Sleep function) if the result is good. In that case, I use taskill /im "SomeName.exe". But it's quite ugly and I have to run it with an infinite loop since I don't know how long it will take to SomeName.exe to reach the result (it's a simulation, it can be very very long !).
Have you got any idea on how to do that ?
Thanks a lot in advance for any help you can give me,
Why use WaitOnReturn=True?
Change that to False and then add your script code loop that checks for final line right after it.

Batch script the same on both PC's but not working?

Hello I have 2 batch files, it works perfectly on one machine but not so perfect on another.
Here is the code.
set /p "ln=" <"C:\LoginSystem\userl.txt"
set "%ln:&="&set "%"
set realuser=%user:"=%
echo %realuser%
So on my machine it shows like this:
echo Liam
Liam
On the other machine it shows like this:
echo "=
"=
It's the exact same machines only difference is one is running Windows 8 (working) and the other windows 7 ("=)
EDIT:
Thank you all for the answers, I managed to solve this by editing the way the userl.txt file is generated to make it display just the name, e.g "Liam" without quotes. Then use this
set /p user=
That seems to work for what I need s there is only 1 value ever going to be in that file.
Thank you all!
A simple echo %thisvariabledoesnoexist:"=% will show the same result.
The reason for the observed output is that the variable %user%, that seems that have to been assigned a value in the set "%ln:&="&set "%" line, did not get any value.
The problem is probably that the input line does not contain the required value or the format of the input line is different from the expected one.
As MC ND has pointed out, the critical point is that variable user must be defined to work properly. If it is not defined, then you get your problem result.
I don't see how identical user1.txt files being processed on two machines with identical batch scripts can possibly give different results as you describe.
Strike that, I can think of one way, but it is a long shot. The assumption is the first two lines are supposed to define the user variable, perhaps along with other variables. But suppose the first two lines do not define the user variable on either machine. Perhaps user is already defined on the machine that "works" before the script is even run, and it is not defined on the other machine. That is the only thing I can come up with that would yield the result you describe.
I do see one thing that concerns me in your code. The following line of code implies that sometimes you get quotes in your input.
set realuser=%user:"=%
You state that quotes in your value interfere, so you remove them. But you are removing them too late! The prior line may not set all the values properly if there are quotes in the value of ln.
Try the following:
set /p "ln=" <"C:\LoginSystem\userl.txt"
set "ln=%ln:"=%"
set "%ln:&="&set "%"
echo %user%

How do I get these results in separate automator variables?

I have made a small automator script that runs a bash shell script and gets two outputs... On viewing results it appears like this below...
I want them in two automator variables
Assume I used a script like
echo "200"
echo "19 hours, 4 minutes and 42.765 seconds"
and on viewing the results it shows this (and I want each of these as automator variables called count and duration). I want it to be sent to a display notification with subtitle as "count files processed" and message as "duration elapsed". How can I achieve this?
You can modify Automator variables by applescript. The variables must exists in the workflow, so you first should add two variables, to get something like on the next image:
You can set anyting as their initial value...
After the above, you can use the next applescript, right after your shell script
on run {input, parameters}
set value of variable "Count" of front workflow to item 1 of input
set value of variable "Duration" of front workflow to item 2 of input
return input
end run
It is not fully correct, (for example it isn't check the number of arguments on the input), but you get an idea.
So after the next:
Your automator variable Count will contain 200, and the variable Duration will contain the text.

VBScript readline from file and check equal to [duplicate]

This question already has an answer here:
VBScript equals problems
(1 answer)
Closed 8 years ago.
I am working on a VBScript login/signup program I already have the signup part done but then while logging in it has to read a line from a file with ReadLine() but the file must see if the line read and the text typed are equal variables and I don't know how to do this
For simple cases, the = operator
If sInput = sRead Then
...
Else
...
End If
works well; if you have to care for case(in)sensitivity, use StrComp().
The comparison is not affected by the way you obtained the strings. If your file justs contains the string that has to be matched,
sRead = tsIn.ReadLine()
before the comparisons will 'work'; if your file contains more than that, you'll have to publish (relevant parts of) its content and how the relevant data can be identified.

Saving Variables Permanently

I have a variable called random number that needs to be stored when the application has be shutdown or the computer has been shutdown. Every time this number is used I also need to +1 to it.
I have a few variables in my current vb6 application that need to be saved when the app is closed and loaded when the app is launched. Is this possible? I could use a text file or a config file to store the variables?
EDIT -------------------------------------------------------------------------------
I managed to fix this problem had just using a simple input and output text file. Please read my answer below if you have the same problem and need assistance.
The standard way to save values in VB6 apps was to use INI files. If I remember there are a couple of Win32 functions to read/write them.
They are GetPrivateProfileString and WritePrivateProfileString.
Using the registry is the correct way to do it.
VB has built in functions SaveSetting and GetSetting for writing to and reading from the registry.
See registry tutorial or Stack Overflow question to help you out.
I managed to complete the task by creating a file in my C Drive and putting in the number "123" to the top line of the text file. I then wrote the following code:
Function GetPOIRandomNum()
Dim LineA As String
'Collect stored variables
Open "C:\TestPartner\Config\POIRandomNum.txt" For Input As #1
While Not EOF(1)
Line Input #1, LineA 'Read the first line in the file
POIRandomNum = LineA + 1 'Give POIRandomNum the integer from line 1 and add 1 to it
Wend
Close #1
'Save the new random number variable to the file
Open "C:\TestPartner\Config\POIRandomNum.txt" For Output As #1 'Open for output to replace the old number
Write #1, POIRandomNum 'Input the new number to the text file
Close #1
End Function
Now whenever the Random number Variable is needed I call the above function.

Resources