Windows batch script to extract number from HTML code? - batch-processing

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

Related

VBA Shell Run Exit Code Capture and interpret

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.

How to use command prompt to list file names in directory but exclude 1st 3 characters

I'm trying to write some vba code that sends a line of code to the command prompt and executes it. I have that part down, but I need help getting the actual code to work.
I want to list all of the files in a specific folder that are the .doc file extension, but I want to exclude the first three characters of the filename that gets printed to my output text file. (Note: I'm using vba because this is one of several different commands I'd like to get into a single vba macro, and I cannot use batch files b/c they are blocked on my system so I'd like to work directly with the command prompt)
The following code works and gives me the file names without the file extension (ie. ABC201704.doc will return as ABC201704)
*%comspec% /c for %i in (C:\Test\ABC*.doc) do #echo %~ni >> C:\Test\Output.txt*
However, I don't know how to modify this so that it doesn't include the first 3 characters (ie. I'd like it to return 201704 instead of ABC201704).
Any help would be greatly appreciated! I tried using the following link, but I couldn't figure out how to get that to work for my situation.
Not tested:
#echo off
setlocal enableDelayedExpansion
for %%a in ("C:\Test\ABC*.doc") do (
set docname=%%~nxa
echo !docname:~3!
)
In command prompt:
cmd /v:on /c "for %a in ("C:\Test\ABC*.doc") do set docname=%~nxa & echo !docname:~3!"

.bat file to merge .txt into one file, including filename but separated by a ';'

I'm having trouble with a kinda specific problem.
The monitoring software (used for robots in the manufacturing halls) used by the company i am working for, generates a log file (.sdat) every 15 minutes. The content of a log file looks like this:
The syntax: Time;machine;status
13:53:23;KP85;ms:9999
13:53:49;KP85;ms:3
13:54:54;KP85;ms:4
14:06:04;KP85;ms:9999
13:51:38;Robot1;ms:9999
etc...
I've managed to concatenate all the log files into one big file, including the filename at the start of each new row, like this:
The syntax: Filename:Time;Machine;Status
01-03-2016-00-20.sdat:0:07:40;KP65;ms:3
01-03-2016-00-20.sdat:0:09:09;KP65;ms:4
01-03-2016-00-20.sdat:0:09:11;KP65;ms:3
01-03-2016-00-20.sdat:0:09:13;KP65;ms:4
etc..
The reason I did this is because i need the time as well as the date(which is included in the filename) a certain status for a machine has been logged. However, if i import this into SQL management studio, it recognizes 3 columns instead of 4, because the filename and first column(time) are separated by a ':' instead of a ';'. I tried solving it with an SQL Query, separating the date and time with a LEFT() and RIGHT(), but guess what: the time field format changes when the time switches from 9:59:59 to 10:00:00, creating an extra character for the time field (so the data in the column would look like this ':9:59:59', which isn't a valid time field). Perhaps it could be done with SQL but it just seems to me like it would take too much complexity in the SQL code for such a small problem.
So at this point, i thought it would be better to tackle this problem early on; within the batch file which generates the large file, so Management Studio does recognize 4 instead of 3 columns. This is how my .bat file looks like at the moment:
#echo off
findstr "^" *.sdat >output.txt
What do i have to do to get this right?
Thanks in advance,
Mike Sohns
#echo off
(for %%f in (*.sdat) do (
for /f "delims=" %%a in (%%f) do #echo %%f;%%a
))>output.txt
NOTE: this will swallow empty lines, but I think in your case that's not a problem. If yes, the code can be adapted.
Given that all the log files you are concatenating have the sdat extension, you have to replace sdat: for sdat; in order to have a ; separated CSV.
To achieve this, you can use the batch script in this other answer(How to replace substrings in windows batch file), that replaces substrings in a text file using a batch script.

How to .. batch file which executes information in a text file

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.

How would I call a dynamic variable name?

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