Okay, so I'm trying to make a program that "understands" user input and does what they tell it to do. People usually just use specific commands such as "open this file" and it only works if the user types EXACTLY that. I'm trying to give my users a little bit of leeway, so that they can type something like what they want to happen, and the computer will get the general idea. With that block of rambling aside, I've run into a problem.
set word%wordNum%=%word%
:fileExtension
set extChk= %letterNum% - 2 REM Includes the period of the extension
call set extension=%%_albaiRec:~%extChk%,4%%
::extChk is checking for a period so the program will recognize a file extension
set file=
That last line is where I get stuck...
I'm trying to use that last recorded word variable.
set var=7
set word7=Wanted text
echo %word%var%%
Sorta like that?
Add setLocal enableDelayedExpansion to the start of your script.
Then replace echo %word%var%% with echo !word%var%!.
For more information - http://ss64.com/nt/delayedexpansion.html
Related
In my log file, I want to capture exit code and the meaning of the code.
Is there a reference to this?
ReturnCode = Shell(ThisWorkbook.Path + "\DELETE_A_FILE.bat")
My code (below) seems to work, but ruturns 14400.
I didn't know how to interpret the code.
I found this, but I don't understand, because my number (14400) is not in the list...
http://www.febooti.com/products/automation-workshop/online-help/events/run-dos-cmd-command/exit-codes/
Is there a reference to properly interpret different codes so that I know how to handle them in my code?
Are negative numbers errors and positive numbers successes?
THESE EDITS were applied based on comments to original question....:
MY Batch file is something like this:
rmdir /S /Q c:\temp\abc.pdf
So what the commenter(s) below seem to be saying is that the return code depends on what is in the .bat file. The .bat file can have multiple statements in it. Is the return code based on the last statement in the bat file?
Assuming the batch file didn't come with some readme.txt, the reference would be the batch file itself.
If there are no comments explaining each exit code, then you'll have to infer their meaning from the script.
So I am trying to ask the user if they want to overwrite a current virtual machine if one already exists. I have extracted the problem part of this .bat file into a separate file to replicate the problem.
The code from this file is:
#echo off
#setlocal enabledelayedexpansion
SET test=blah
SET existingMachines=blahsdg
:checkOverwrite
if not "%test%"=="%existingMachines%" (
SET /P machineOverwrite="A machine containing this name already exists. Do you want to overwrite it (y/n)? "
if /I "%machineOverwrite%"=="y" (
echo overwrite
) else (
if /I "%machineOverwrite%"=="n" (
echo You will need to choose a new name for your virtual box or overwite the existing one.
) else (
echo WARNING: You did not enter y or n
GOTO :checkOverwrite
)
)
)
pause
The concept is the first if will always come back as true (as these variables are never equal in this case) and from there it would ask if they want to overwrite the machine. If they say "y", you should see "overwrite" and then press any key to continue...
The issue is it's not doing that! Its doesn't seem to be setting the machineOverwrite variable, and so it is going through to the "you did not enter y or n" section. From here it goes back to the start, and goes through again.
The REAL strange thing is that the next time you go through, if you choose "y", it does what its meant to! BUT, if you choose "n", it still does what it should with "y"!
Every time I enter anything, it always get the input from before it went back to :checkOverwrite and not the latest input. WHY??
Your problem is that despite attempting to enable delayed expansion (with setlocal enabledelayedexpansion) you aren't actually using it.
%var% is normal variable expansion, even with delayed expansion turned on.
You need to use !var! for delayed variable expansion. See (for example) the "documentation" here.
I am looking for some code to extract a number which is expected to be from one to four digits in length from some HTML code. I have pulled the web page using cURL and used FIND to extract the single line of information I need, but I would like the output to be much cleaner.
Here is an example of the HTML line I'm dealing with:
constant_text_here 123 constant_text</h2></td>
Does that html code mess things up for batch scripting? I know nothing about scripting with Windows Batch code. The number can remain as a string, but it is expected to be between one and four digits.
#ECHO OFF
SETLOCAL
FOR /f "tokens=2" %%a IN (q25614455.txt) DO SET var=%%a
ECHO var=%var%
pause
GOTO :EOF
I used a file named q25614455.txt containing your data for my testing. Sadly, you don't reveal what constant_text_here actually is, so honing the procedure is in your bailiwick. The number following tokens= will pick a string from the text supplied in the file; had your text been constant text here for instance, you'd need to change tokens=2 to tokens=4.
It would also likely be possible to include your curl/find into the procedure and extract your target data in one line, but without sufficient details, a vague general question can at best
garner a vague response.
The < and > will give you trouble as they're used to redirect output.
Is using VBscript a possibility?
From the command line:
C:\>set myvar=constant_text_here 123 constant_text</h2></td>
will output:
< was unexpected at this time.
but this will work:
C:\>set myvar="constant_text_here 123 constant_text</h2></td>"
C:\>echo %myvar%
"constant_text_here 123 constant_text</h2></td>"
C:\>echo [%myvar:~20,4%]
[123 ]
Assuming what interests you can always be found at position 20
Sorry not much experience with batch files hence help needed please! ;-)
I'm working in a DOS box on a Windows 7 64 bit system.
I want to run an application as a batch file, but reading the information it needs from a text file which can be updated/amended regularly.
The syntax of the basic application is:
appname "variable" (the variable MUST be enclosed in quotes)
Successive variables can be concatenated to the following single line format:
appname "var1" "var2" "var3" "var4" ... etc
So I've created a batch file containing the above. However, this is unweildy when it comes to updating. Sometimes I omit the delimiting quotes which creates problems in the execution of the batch file.
It seems to me that from an updating/amending point it would be easier to set up a text file, say text.txt which would contain the following information:
"var1"
"var2"
"var3"
"var4"
etc. on successive lines.
This would make it easier for me to update and also to ensure I don't omit the delimiting quotes.
The batch file would get the application to "read" the text file, execute the first variable, then the second etc all the way through to the end. But I'm not sure if this is possible and if so, how to get the batch file to read successive lines in the text file and use those variables.
As I said earlier, I've not much experience with batch files and don't have a clue how to do this! :-(
Help please, thanks
Alan
Like this :
#echo off
set $textFile="test.txt"
for /f "delims=" %%a in ('type %$textFile%') do appname.exe %%a
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "batchline=appname"
FOR /f "delims=" %%a IN (q25193799.txt) DO SET "batchline=!batchline! "%%~a""
ECHO(%batchline%
GOTO :EOF
I used a file named q25193799.txt containing your data for my testing.
The application line is merely echoed for verification. Remove the ECHO( after verification to execute your application.
The data in the file need not have "enclosing quotes".
Thanks to both responses, both different but good in their own way. I use a few different instances and I've decided to use the first response for the longer lists and the second one for shorter lists (that way I can check see if something is going wrong, because I might have missed the second delimiting quote in the text file.
Superb, thanks to you both.
This question seems to be (very) stupid be I can't deal with it :(
When I tried this batch code:
if "%1" == "-i" (
set is = %2
echo. %is%
shift
)
called with 2 (or more) arguments, it does NOT work. It actually prints a blank. The "shift" command is not done either. When I watch the executed code (without the #echo off at the beginning), I can see that the "set" command is completed.
What's wrong with it?
Example of calling:
c:\script.bat -i test -d bla
You have two issues. By default group of statements in parens will have variable expansion done all at once, that is before your set command. Also the semantics for set is wrong, you don't want spaces around the =.
Add this to the top of your file:
setlocal ENABLEDELAYEDEXPANSION
and remove the spaces around = in set:
set is=%2
Finally used delayed expansion:
echo. !is!
A possible third issue is you may need two SHIFTs, one for -i, one for it's is argument.
Update
Thanks to #dbenham for pointing out that it wasn't a syntax error with set, it's just surprising behavior that deserves a little explanation. If you execute these commands:
set a=one
echo "%a%"
The result is:
"one"
That makes sense, but try:
set b = two
echo "%b%"
And you get:
"%b%"
What? This is what you would expect when environment var b is unset. But we just set it. Or did we:
echo "%b %"
Displays:
" two"
For the Windows set command, unlike any other language or environment I'm aware of, the spaces are significant. The spaces before the = become part of the environment var name, spaces after become part of the value. This uncommon behavior is a common source of errors when writing Windows batch programs.