how to convert string into an integer in a batch file ? - c++

I have this piece of batch file :
#echo off
set xx=7
echo the version is %xx%
I wand to use it in a pre-build event in VS2010 - as an integer :
MY_INT = $(xx)
but it's value is a string , how can I convert the string value into an integer value in the batch file?
thanks!

Environment variables (batch variables) are always strings; they cannot be stored as integers.
The SET command with the /A option can parse integral numbers from strings, perform arithmetic operations, and store the integral result in an environment variable. But the final result is still a string representation of the final number.
Type help set or set /? from a command prompt for more info about the SET /A option.

Related

Operator IF doesnt check condition of values in cmd

Hi everyone I writing little .bat file and seemed for I was ended but I got error. Operator IF does not checking Permission value and Free space conditions of values and are not compering. All the time I get "false" even when the free space on disc D is less then 25Gb (26,843,545,600 bits)
My code looks such:
#echo off
#setlocal enableextensions enabledelayedexpansion
set permissiblevalue=26,843,545,600
set permissiblevalue=%permissiblevalue:,=%
for /f "tokens=3" %%a in ('dir d:\') do (
set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
if %permissiblevalue% leq %bytesfree% (
The disk D is checking.....
) else (
echo msgbox "Lacks of free space on disk D. Klick OK for delete files!!!" > %tmp%\tmp.vbs
wscript %tmp%\tmp.vbs
del %tmp%\tmp.vbs
%SystemRoot%\explorer.exe "d:\"
)
exit
Please explain to me Where is the mistake
If the two arguments to if are both pure numeric strings, cmd will convert them to integers and compare them as integers. BUT cmd is limited to 2^31 and mechanically processes the string character-by-character, left-to-right, multiplying by 10 and adding the next value, so any value greater than 2147483647 will be processed incorrectly (but won't generate en error).
Consequently, if you are using large numbers, you need to force cmd to interpret them as strings. This is easily done by "quoting each string".
BUT a string comparison is performed character-by-character, so you need to leading-0-fill each so that they are the same length.
To do this :
set "zeroes=000000000000000000000000000000000000000000000000000000000"
set "var=%zeroes%%var%"
set "var=%var:~-20%"
Which prepends a series of 0 characters to the current value of var, then sets var to the last 20 characters of the result.
You can then safely use if "%var%" leq "%someothervar%"
Tip : Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.

SAS sftp sends file with LF delimiter instead of CRLF

I am completely new to SAS programming hence pardon if the question is very basic. I am trying to send a file using SAS sftp from Linux to windows server. I am able to transfer the file but the destination file has a row delimiter LF whereas our job is expecting the file to have CRLF delimiter. I tried using the termstr option but it fails with error "invalid option termstr". Below is my code
filename out-file sftp 'file.txt' cd='/project/dir'
host='hostname' recfm=v
user=user1;
data _null_;
file out-file TERMSTR=crlf;
do i=1 to i=10;
put i=;
end;
run;
Your program is using an invalid value for the fileref. You cannot use a hyphen in a SAS name.
You can use the TERMSTR= option on either the FILENAME or FILE statement to change the end of line characters.
I thought that SFTP always moved files as binary. You could try changing your SFTP option to make sure it is doing that. Try removing the recfm=v option.
Note: Text files have end-of-line characters, not record delimiters. If you are writing some type of proprietary binary file format you might consider the characters between rows of data a record delimiter, but it just leads to confusion if you think of the lines in text files as being separated instead of terminated.
TERMSTR is an option for the INFILE statement, there is no corresponding option for the FILE statement.
Try using PUT with a hexadecimal string and held output (#)
PUT I= '0d0a'x #;
From SAS documentation
Specifying Hexadecimal Values
Hexadecimal values for (system) option values must begin with a number (0–9) and must be followed by an X. For example, the following OPTIONS statement sets the line size to 160 using a hexadecimal number:
options linesize=0a0x;
Character assignments for hexadecimal numbers require quotation marks:
options formchar='a0'x;
Additional reading at SAS Constants in Expressions will reveal
Character Constants Expressed in Hexadecimal Notation
SAS character constants can be expressed in hexadecimal notation. A character hexadecimal constant is a string of an even number of hexadecimal characters enclosed in single or double quotation marks, followed immediately by an X
and
Numeric Constants Expressed in Hexadecimal Notation
A numeric constant that is expressed as a hexadecimal value starts with a numeric digit (usually 0), can be followed by more hexadecimal characters, and ends with the letter X. The constant can contain up to 16 valid hexadecimal characters (0 to 9, A to F)

Batch: getting numbers between x and y from string "x123y"

I need to extract the numbers between su11b_ and .agm using batch script.
In the example below the result should be 733.
su11b_ is always the same, the numbers here will never change
733 can change, ranging from 1 - eternity (and will thus have a variable length)
Here's what I tried (and it doesn't work at all):
set "str=d:\agrcc\agrtest\server logging\su11b_733.agm"
set /A "number=str"
I'm thinking perhaps a regex thingy could extract the digits between su11b_ and .agm but I don't know how to do this in batch scripting?
You actually don't need regex if you're sure the numbers are always between su11b_ and .agm. You can just delete everything before su11b_ and delete the .agm part afterwards:
set "var=d:\agrcc\agrtest\server logging\su11b_733.agm"
set number=%var:*su11b_=%
set number=%number:.agm=%
echo %number%
This link has some more info about replacing substrings in variables in batch

Batch Script: Extract number from a string

I'm writing a batch script where I need to extract numbers from a string (which indicates the version of a file), so that I can compare it with another number.
Below is my script written so far
:: Over here I'm trying to extract number from the string , this isn't working
for %%F in ("!name!\.." ) do (
::set "number=%%~nxF" |findstr /b /e /r "\"[0-9]*\""
set res=%name:findstr /r "^[1-9][0-9]*$"
echo !res!
)
In the last two for loop I have implemented the extract number logic, but it just prints The system cannot find the drive specified.
If anybody could help me with this issue that would be a great help.

RegEx in Batch File

Hey I'm trying to create a function that parses a string passed via a browser protocol. It's a "callto://" protocol and it is in this format: "callto://5551234567/" with the persons phone number inside there. I need to extract the number and pass it to another program that dials the number. The syntax for that other program is like this: "CallClerk.exe dial=5551234567=".
I'm a beginner to batch however, and can't figure out exactly what to do. Here's my current code:
#echo off
set var=%1
set number=theirphone
FindStr /R "callto://(..........)/" %var% > %number%
start C:\Program Files (x86)\CallClerk\CallClerk.exe dial=%number%=
Exit /B
Thanks for the help!
#echo off
FOR /f "tokens=2 delims=/" %%i IN ('echo %~1') DO start "" "C:\Program Files (x86)\CallClerk\CallClerk.exe" dial=%%i=
Exit /B
should work for you (untested) - assuming your input parameter is callto://5551234567/
Note the use of quoting - the .exe needs to be quoted since it contains a space in the path. The extra pair of quotes in the window-name. If you like, you could replace that pair with "Calling %%i". This parameter is optional, but inserting it ensures that START doesn't get confused between window-title, executable-name and parameter-to-executable.
This works to extract numbers from a string.
It uses two for loops, the first one gathers all the non-numeric characters and they are used as delimiters in the second for loop to gather the numerics and dial the number.
Strings of variable lengths can be handled, as long as all numbers are used in the desired telephone number.
If you want to keep the + as a valid telephone character then include it in the first for command in the delims with the numbers.
#echo off
set "var=callto://5551234567/"
for /f "delims=0123456789" %%a in ("%var%") do set "delims=%%a"
for /f "delims=%delims%" %%a in ("%var%") do (
start "" "C:\Program Files (x86)\CallClerk\CallClerk.exe" dial=%%a=
)
You should be able to use a regex along the lines of (?<=callto:\/\/)[\d]+(?=\/) to grab the number itself. This uses a positive look ahead and look behind to make sure you are matching at least one number that is preceded by the callto:// and followed by a /.
If you left it as something like callto:\/\/[\d]+\/, then it is matching the entire string and will return back with the callto text included. If you are intending to pass just the numbers along to the next part of you code, extract them using the look ahead to guarantee the before and after conditions are met.
I did a quick test using the strings you used in your example. You can see the regex in action here.