Giving input to /set \package at runtime - batch-file

In a batch file, I have replaced the hard coded dB name with a parameter that I am reading from a file. When I run the batch file from local, I can see the dB name getting properly replaced. However, when I am trying to run the same script on WLM, the job is getting aborted without triggering the underlying package itself.
Code for reading from file:
for /f "delims== tokens=2" %%G in (file_name.txt) do set %%dB_name=%%H
I am using DTEXEC in the batch file and the parameter is being passed to the /set \"package[variable]";dB_name

Using file_name.txt of:
ABC=DEF
GHI=JKI
And your batch line, I find the following variable getting set:
%dB_name=%H
I suspect that's not what you actually want. Normally you don't want the percent sign in your variable name.
Also, while you specify token #2, that is the only token you specify, so I suspect your target should be %%G instead of %%H.
Perhaps something like this would work better:
for /f "delims== tokens=2" %%G in (file_name.txt) do set dB_name=%%G
Also, I can no longer remember why but I'm a big fan of putting delims last:
for /f "tokens=2 delims==" %%G in (file_name.txt) do set dB_name=%%G
In any regard, the result I get when I do this is, using the same input file, is:
dB_name=JKI
Which looks a whole lot more like what you're trying accomplish.

Related

Batch File: "Environment Variable Not Defined" on a path

I have the following in a batch file
for /f "delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set %%a
net use J: https://csv/dav %p% /user:%u% /persistent:yes
I get an error:
Environment variable C:\Program Files (x86)\Wowza\creds.txt not defined
What do I need to resolve this?
Secondly, it works for all colleagues apart from one. Same laptop make, model and build. I used my details and it failed on his but worked on mine.
What fails is that it asks for the credentials to map the drive instead of taking them from the file
creds.txt
u:JoeBloggs
p:Password1234
Any idea?
Thanks
the reason for your errormessage is, your for /f loop doesn't evaluate the contents of the file. It takes a quoted string as string not as filename. Usebackq changes that behaviour.
You have another failure in your script: With your code, set %%a translates to set u:JoeBloggs, which is invalid syntax. Correct syntax requrires set u=Joebloggs. Therefore you have to split the line in a part before the colon and a part after the colon and build your set command accordingly (just set %%a would work, when the contents of the file would look like u=JoeBloggs)
Change your for loop to:
for /f "usebackq tokens=1,* delims=:" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%a=%%b"
I was going to post a similar answer to #stephan but he beat me to it. If however you have the option to change your creds.txt file to the below:
u=JoeBloggs
p=Password1234
You can shorten the for loop a bit to this:
for /f "usebackq delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%~a"
which effectively just does this:
set "u=JoeBloggs"
set "p=Password1234"

How to turn multiple parameters into a single parameter in Batch?

This topic is really hard to explain, so try to understand it. I'm working on a project called RoSOX that uses a URI to join a server (on another end)
However, this batch program requires at least 3 parameters. If you were to run it under the Windows Command Prompt, you would run it like this RoSOXLauncher.bat "IPHere" "PortHere" "UsernameHere". This is the code that obtains those values (Not the entire program though)
#echo off
set ip=%~1
set port=%~2
set username=%~3
Unfortunately, I found another post mentioning that URI Protocols (aka Registry Entries that allow starting a program from your web browser, such as "mailto:example#example.com") only accept 1 parameter regardless of the program. My theory to bypass this is to have the input connected by commas so it's considered one parameter. It would be written like this: RoSOXLauncher.bat IPHere,PortHere,UsernameHere An example would be this. The logarithm would be
Disassemble the values from the commas (The FOR command is probably used)
Take each value and put it into its own variable
Continue the rest of the script
Does anyone know how this logarithm would be wrote in Batch? This is the only thing preventing me from having my program run correctly.
It seems very unclear as to what you want, but if I understand your requirement correctly then:
#echo off
set ip=%~1
set port=%~2
set username=%~3
Will still work if you do:
RoSOXLauncher.bat IPHere,PortHere,UsernameHere
Simply because the delimiters excepted are whitespace, comma and semicolon. You can try this and see for yourself.
RoSOXLauncher.bat IPHere;PortHere;UsernameHere
RoSOXLauncher.bat IPHere PortHere UsernameHere
Will do the same.
On the other hand, your program which you send the parameters from might also delimit by whitespace, command or semicolon, then you might simply use aother common delimiter like - or : see this:
#echo off
for /f "tokens=1-3 delims=-" %%i in ("%~1") do (
echo ip %%i
echo port %%j
echo username %%k
)
Which you can run using:
RoSOXLauncher.bat IPHere-PortHere-UsernameHere
or
#echo off
for /f "tokens=1-3 delims=:" %%i in ("%~1") do (
echo ip %%i
echo port %%j
echo username %%k
)
which you can run with:
RoSOXLauncher.bat IPHere:PortHere:UsernameHere

Batch script to grab lines with findstr without filepath

I've got a log file that monitors a large system including requests and acknowledgements. My objective is to be able to:
1. Loop through the script and get the lines where requests & their acknowledgements happen
2. Pull the entire lines of importance as strings and store them as variables for string modifying to output somewhere else.
Here's what I have so far:
#ECHO off
setlocal
setlocal enabledelayedexpansion
setlocal enableextensions
:: Lets get today's date, formatted the way the ABCD File is named
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do set targetDate=%%f-%%d-%%e
:: Now we set the targetFile name
SET ABCDLogsFile=C:\Users\me\Documents\monitoring_file_for_jim\ABCDFIX*%targetDate%.log
::****Scrapped original approach*****
set "ackFoundCount=0"
set "reqFoundCount=0"
::Get lines with acks
for /f delims^=^ eol^= %%a in ('findstr /c:"\<ACK\>" "%ABCDLogsFile%"') do (
set /a "ackFoundCount+=1"
setlocal enabledelayedexpansion
for %%N in (!ackFoundCount!) do (
endlocal
set "ackFound%%N=%%a"
)
)
::Get lines with requests
for /f delims^=^ eol^= %%b in ('findstr /c:"ReqSingle" "%ABCDLogsFile%"') do (
set /a "reqFoundCount+=1"
setlocal enabledelayedexpansion
for %%N in (!reqFoundCount!) do (
endlocal
set "reqFound%%N=%%b"
)
)
setlocal enabledelayedexpansion
for /l %%N in (1,1,2 %reqFoundCount%) do echo REQ %%N FOUND= !reqFound%%N!
pause
for /l %%N in (1,1,2 %ackFoundCount%) do echo ACK %%N FOUND= !ackfound%%N!
endlocal
EDIT 2 dbenham
The roundabout way I was trying to accomplish this before was totally unnecessary.
Thanks to the questions and answer here:
'findstr' with multiple search results (BATCH)
I've got my script working similarly. However, I'm curious if its possible to get findstr output without the filepath at the beginning. I only need to substring out the timestamp in the log, which would always be the first 12 characters of each line (without the filepath). My output currently is prefixed with the path, and while I could get the path where the log would eventually be in production, it would be safer to try and do it another way. At the time that this script would eventually be run, there would only be 1 or 2 reqs and acks each, that is why I store all which are found. It's not necessary but I think it would be reassuring to see two if there are two. Here is what the output looks like for acks and reqs alike:
C:\Users\me\Documents\monitoring_file_for_jim\ABCDFIX 2015-04-01.log:2015-03-26 07:00:11,028 INFO etc...
I'm thinking that if I could strip the filepath off the start, then all I'd need to do to get just the timestamps of the events would be
for /l %%N in (1,1,1 %reqFoundCount%) do echo Req %%N occurred at: !reqFound%%N:~0,12! >> MorningAckChecks.txt
for /l %%N in (1,1,1 %ackFoundCount%) do echo ACK %%N occurred at: !ackfound%%N:~0,12! >> MorningAckChecks.txt
I suspect you could not get SKIP to work because you you were iterating the delimited list of line numbers with a FOR statement, which means the number is in a FOR variable. Problem is, you cannot include FOR variables or (delayed expansion) when specifying a SKIP value, or any other FOR option. The batch parser evaluates the FOR options before FOR variables are expanded, so it couldn't possibly work. Only normal expansion can be used when including a variable as part of FOR options.
But I don't understand why you think you need the line numbers at all. FINDSTR is already able to parse out the lines you want. Simply use FOR /F to iterate each matching line. For each line, define a variable containing the line content, and then use substring operations to parse out your desired values.
But I can offer an alternative that I think could make your life much easier. JREPL.BAT is a sophisticated regular expression text processor that could identify the lines and parse out and transform your desired values, all in one pass. JREPL.BAT is a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
If I knew what your input looked like, and what your desired output is, then I could probably knock up a simple solution using JREPL.BAT. Or you could read the extensive built in documentation and figure it out for yourself.
Documentation is accessed from the command line via jrepl /?. You might want to pipe the output through MORE so you get one screen of help at a time. But I never do because my command line console is configured with a large output buffer, so I can simply scroll up to see past output.
EDIT - In response to comment and updated question
Here are the relevant snippets of your code that are causing the problem.
SET ABCDLogsFile=C:\Users\me\Documents\monitoring_file_for_jim\ABCDFIX*%targetDate%.log
findstr /c:"\<ACK\>" "%ABCDLogsFile%"
findstr /c:"ReqSingle" "%ABCDLogsFile%
The issue is your ABCDLogsFile definition includes a wildcard, which causes FINDSTR to prefix each matching line with the full path to the file name where the match occurred.
I have a simple solution for you - Just change the definition of ABCDLogsFile as follows:
SET "ABCDLogsFile=C:\Users\me\Documents\monitoring_file_for_jim\ABCDFIX<%targetDate%.log"
Explanation
My solution relies on two undocumented features
1) Undocumented file mask wildcards.
< - Very similar to *
> - Very similar to ?
These symbols are normally used for redirection, so they must be either quoted or escaped if you want to use them as file mask wildcards.
We discuss the undocumented feature at DosTips - Dir undocumented wildcards. Sprinkled throughout the thread (and a link) are some example use cases.
I document my understanding of exactly how the non-standard wildcards work at http://www.dostips.com/forum/viewtopic.php?p=39420#p39420
2) FINDSTR works with the non-standard wildcards
FINDSTR will prefix each matching line with the file name (and possibly path) if any of the following conditions occur
The /M option is used
The /F option is used
Multiple input files are explicitly listed on the command line
Multiple input files are implied via a file mask with at least one * or ? wildcard on the command line
Your are getting the file path prefix because of the last trigger - the * in your file mask.
But you can use < instead to get the same result, except the non-standard wildcards do not trigger the file prefix in the output.
Problem solved :-)
I talk about this FINDSTR feature at http://www.dostips.com/forum/viewtopic.php?p=39464#p39464.
Some day I hope to update my What are the undocumented features and limitations of the Windows FINDSTR command? post with this tasty little tidbit.
This post has become a bit cluttered. It would be very helpful if you posted the lines of input that correspond to the output you are getting. If you can't do that then add this statement before your FOR. I am sure you will find that testReqSkip is blank.
echo.testReqSkip=%testReqSkip%

Making Batch for /f code work propely

Here is my file that is trying to read hosts file. For some reason, it's not workin properly. Try the code yourself to see the error messages.
#echo off
cd %windir%/System32/drivers/etc
for /f "eol=# tokens=* delims=," %%a in (%windir%/System32/drivers/etc/hosts) do type %%a
pause>nul
type expects a file name. you probably meant echo.
as another issue, the hosts file is not comma, but whitespace separated. so you'll need to change your delims to include space and tab, assuming you want to have only the hostname.

What does "set _" in a for loop?

I had a problem that Win7 hangs on shutdown screen for over 30 mins.....
then I found this batch file was the cause, but i dont understand what's the purpose of this.
The shutdown only hangs on after join to domain.
This batch file was in c:\ntfs\bin
After I removed this batch file, no more hangs
Issue fixed but i want to know what does this script mean. I dont know much about batch file
please give me a hint
#echo off
for /f "usebackq tokens=1-2 delims==" %%a in (`set _`) do set %%a=
use better:
for /f "delims==" %%a in ('set "_"') do set "%%a="
This code deletes all environment variables starting with _ (underscore) in the current setlocal-endlocal block. If you use this in a batch file without the setlocal command, the varialbles are removed from the current command prompt session.
The use of usebackq and tokens is not needed in this case, see help for for more help on the command prompt.
It deletes all variables that start with an underscore (for example _test or _whatever)

Resources