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

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%

Related

if statements inside command

I am trying to make a if statement for a CLI i am making in batch. this is the code
if "%command%" == "browser"(
echo Warning, this will only change the default broswer for this session
echo What would you like the default browser to be changed to?
set /p browserdefault=
)
I want it to check "browserdefault" and see if it is chrome, edge or a diffrent broswer
if it is none of those it should say "error, invalid broswer."
is there any way for there to be a if statement inside of the already existing if statement? like if %browserdefault% is "chrome" start C:%username%\filepath\tochrome?
I tried many diffrent ways but none of them seems to work
As pointed out in the other answer this is Windows batch. I do not know a way of nested if statements.
I would use call statements to get rid of the nested if statements like this:
:getCommand
set /P command=Enter command:
call :%command%
goto :getCommand
:browser
set /P browserdefault=Enter browser:
if not "%browserdefault%"=="chrome" if not "%browserdefault%"=="edge" echo error, invalid broswer.
goto :eof
What you are trying to do is right, noble and good. However, this is Windows batch. The ordinary rules of style & good taste do not apply.
You should not try to nest other statements inside a batch command. There are times this will appear to work, but (based on a number of factors, such as a poorly considered logical condition, or even different input strings!) will occasionally cause the block to fail miserably. This will set you up for a world of painful debugging.
The suggestion in the comments to search for menu is a good one. This should lead you to code that will have (what looks like) many unnecessary & unrelated blocks, all sprinkled with horrible gotos. Just... it's fine. This is Windows batch. It will work, it will be reliable. Try not to think about the style too much.
There are some example menus in this excellent Q&A:
Menus in Batch File
(as noted in at least one answer there, when using the naive if ERRORLEVEL 1, this will actually check if the number is equal OR greater than which requires starting with a higher ERRORLEVEL and working down to IF ERRORLEVEL 0, with a GOTO jump for each check -- using a statement with a % variable such as if "1" == "%ERRORLEVEL%" or similar avoids this pitfall).

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.

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

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%

Need to answer half questions automatically on script

Is it possible to answer first few or a part of questions automatically and rest manually?
I am connecting to a VPN daily which gives first prompt to say "yes" or "no", second one to choose a group and third one to input username. Upto three questions, answers will be same. Fourth prompt is a physical RSA token input and fifth one is a password.
When I script it with an input file for first three questions (eg; ./script < inputfile), the connection exits as below.
GROUP: [xxxx|abcdgroup01|sssgroup01|sssssgroup01z]:Please enter your username and password.
Username:Password:
fgets (stdin): Inappropriate ioctl for device
This is happening because the script is not getting the fourth answer from the input file.
Out of five inputs, four are static ones and the fourth one is a dynamic one.
How do I manage to enter fourth input manually ?
Please help; Thanks in advance !
There are other ways, but for your case, your approach doesn't seem to be so bad.
If you really want the user to see the original prompt from script.sh, you would have to create an expect program to drive your script; see the man-page for expect.
If you are permitted to change script.sh, you could add the possibility to supply certain parameters from the outside (a file, or an environment variable) and only ask for those parameters which are not supplied. This would IMO be the best approach.
I just found this method and it seems working !
{ echo yes; echo xxxxx; echo xxxx; read rsa; echo $rsa; echo xxxx; } | ./script.sh
Any other way ?

is there a way to stop the popping up of the md5.exe tool while it generates the hash for a file

i have written a program in vbscript for which i have used md5.exe to generate hash. since there are many files to which the hash has to be generated, the md5 hash repeatedly generates hash for each file one after the other. but while this process is in progress, i can see it popping out on the screen as it generates the hashes ( it does not pop out the hashes, the tool itself pops out on the screen repeatedly). i want to do something such that it stops popping out yet generate the hash for all the files. please help guys!
Generally, when running a command, youj can supress its output by directing it to a file, then deleting the file
md5.exe blahblah >null the >null being the critical part. Note however, that null doesn't directly it to a magical blackhole. It creates a file named null and prints the output there
This would supress the output. IF you posted some code, I could have told you how to do that there. but if you are running it as a shell exec, this should work
You can use the run method to supress the window.
Set WshShell = CreateObject("WScript.Shell")
cmd= "C:\Users\Administrator\desktop\experimenting\md5.exe"
'OR whatvere your whole command is
cmdRun = WshShell.Run(cmd,0,true);
Answer from here
Want to hide command prompt window in using WshShell.Exec method
(Upvote the guy if you find it useful)
Please note that you have tro use the output file to read. You cannot read the output from the shell anymore.

Resources