Until a specific substring - variables

I need a batch command to return everything until after a certain substring.
What I mean, is when I have a string like this: "Hi! How are you doing? I don't care!!!!!" I can execute a command that gives me everything until after "?".
I looked around the web and didn't find anything that I wanted. I found one method that took everything until after a substring and changed it:
set name=123456789
set blablabla=%name:*5=5%
This returns "56789" to the variable blablabla. The strings in my program are not going to be specific, so this won't work.
Thank you for any help!

Use sed and regular expressions:
$ string1='Hello my name is Foobar? What do I care!'
$ string2=$(echo $string1 | sed 's/^.*\? //')
$ echo $string2
What do I care!

I have not understood your requirements fully. But I think PowerShell is a better option for this. However, you can use following script to get all characters upto given delimiter.
#echo off
set delim=%1
set input=%2
for /f "delims=%upto%" %%i in ("%input%") do (
echo %i
goto :eof
)
Sample run command:
stringupto.bat 5 123456789
Output:
1234

Related

Filter word in a string in batch script

I created a batch script for windows that I use for mux mkv files.
When launch this command:
ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=default -of compact=nk=0 file.mkv | findstr disposition:default=1
Output is:
stream|index=3|disposition:default=1
How can filter and print only number "3" and put it in a variable?
I submit a new command that simplify output:
ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=forced:stream_tags=language -of csv=nk=1:p=0 file.mkv | FINDSTR /C:"1,ita"
Output is:
3,1,ita
"3" is track id, "1" is forced flag, "ita" is track language. To create a variable that contains only the track id (e.g. 3) to be inserted in a mkvmerge command, I ran this command:
FOR /F "delims=, tokens=1" %%# IN ('ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=forced:stream_tags=language -of csv=nk=1:p=0 file.mkv ^| FINDSTR /C:"1,ita"') DO SET subid=%%#
But nothing happens! Mkvmerge report this error: Error: Invalid track ID or language code in '-s '.
I don't really know where the mistake is!
Batchfile approach
You need to execute your command inside a for statement inside a batch file to be able to capture the output lines and process them further. Check for /? on the command line and the part with for /f and learn about "usebackq".
The key point is, that you need to escape several special characters from your command, if it is executed in the for statement and not on the command line prompt directly.
Try getting this piece to work and post your solution as update to your answer if you like. Then we can get to the second part of extracting the number.

Put a WMIC query result into a variable

I'm trying to put the result of:
"WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /value | find "displayName=Symantec""
into a variable so that I can then ECHO the result.
Could you please advise?
After I spent some more time on this I found the answer. Only a "^" was needed. So command looks like:
FOR /F "tokens=*" %A IN ('WMIC /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct get /value^| find "displayName=Symantec"') DO (SET HOST=%A)
Then if you echo HOST -> you got it (or I got it).
Thanks anyway :D

storing current path in variable and reusing it

I know similar questions have already been asked, but somehow I am unable to figure out the mistake in my code.
I'm making a .bat file with the following code
echo off
echo %cd%
set curr_directory = "%cd%"
echo $curr_directory
pause
OUTPUT is :
C:\Users\MyDesktop>echo off
C:\Users\MyDesktop>
$curr_directory
Press any key to continue . . .
So what I dont get is why the value of variable curr_directory is not being printed.
What i eventually want to do is use the variable to change the directory something like this: cd $curr_directory
Thanks
I don't know where to start. EVERYTHING about your code is wrong. This should work:
#echo off
echo %cd%
set curr_directory=%cd%
echo %curr_directory%
pause
In batch you access variables via %var% and not $var. Further, DO NOT PUT SPACES behind =. SET x=123 will store 123 in x but SET x= 123 will store _123 (_ means space) in x.
EDIT: As SomethingDark stated, the first line should be #echo off except you actually want the message echo off to be printed. And yes, SET x = 123 means %x % is 123
use %curr_directory% instead of $curr_directory
Avoid spaces inbetween like the below one
"set curr_directory = %cd%"
below should work
echo off
echo %cd%
set curr_directory=%cd%
echo curr_directory is %curr_directory%
pause

Batch - How to Echo %%Variable%%

I have a small problem with a batch file I'm working on.
Here's a simple sample:
I would like to get the string "THERE" as my result.
But the result I get is just "HELLO"
set hello=there
set a=h
set b=ello
set result=%a%%b%
echo %result%
I already tried something like this:
Echo %%result%%
And Sadly, it just gets me the result %HELLO%
Any help would be great. Thanks!
The reason that %%result%% gives you %result% on the output is that the %% tokens are interpreted first, and further interpretation is not done.
However, you can use that to your advantage, doing a second level of indirection with the following trick:
#echo off
set result=hello
echo %result%
call :iset second %%result%%
echo %second%
goto :eof
:iset
for /F "usebackq tokens=*" %%i in (`echo %%%2%%`) do set %1=%%i
goto :eof
The secret lies in passing the thing you want interpreted (in this case, %%result%% passes in the literal %result% as per the rules stated in the first paragraph, not the interpretation of it).
The for loop then echos the interpretation of that (hello) surrounded by %...% (again, the double %% reduces to %), so that it is also interpreted, and it uses that to set the target variable you also passed in.
The upshot is that it effectively gives you:
%(%result%)%
which is what you're after.
Might I suggest, however, that you start looking into Powershell, so that you don't have to perform these batch-gymnastics in future :-)

Correct Output to File When Using SQLCMD

I've been playing around with SQLCMD to output a .SQL results to a file. It is working but the actual SQL statements are also being displayed in the output file which I obviously don't want. I am using a .bat file to run the SQL File. The following command is what I have in my .bat:
SQLCMD -S MyServerName -d MyDBName -i C:\test\start.SQL -o C:\test\out.txt -e
In my start.SQL I have the following:
set nocount on
SELECT '<HEADER>', getDate(), '<HHEADER>'
SELECT UPPER(ACCTNUM),
CONVERT(VARCHAR(10),DATEEXP,126),
UPPER(OPERID),
CONVERT(VARCHAR,CREATE_TMSTMP,120)
FROM RETURNS
WHERE CREATE_TMSTMP < getDate()
AND CREATE_TMSTMP > getDate() - 1
SELECT '<TRAILER>', getDate(), '<HHEADER>'
set nocount off
In the output file the correct information is shown but it's a little ugly with dashes and such and even shows the SQL command that was executed. Is there any way to fix this problem. Am I doing something wrong here? Any help would be greatly appreciated.
Why are you using the -e option?
-e
Writes input scripts to the standard output device (stdout).
The dashes are part of the header. Use -h-1 to specify no header. If that is not acceptable use something like FINDSTR /B /V "----" to exclude that header line... assuming none of the lines you want to keep start with dashes.