Launching Pentaho Job using a batch file and setting a parameter value - batch-file

I'm scheduling a Job using a batch file (bat) but I don't know how to set a parameter that the job needs.
hope someone can help me on this!
My Batch file is:
#echo off
set Pentaho_Dir="C:\Transformations&Jobs"
set Pentaho_Job=Interfases_MI.kjb
set path="C:\Program Files\Pentaho\data-integration";%path%
Kitchen.bat /file:%PENTAHO_DIR%\%Pentaho_Job% /log:%PENTAHO_DIR%\Logs\Interfases_MI.log /level:basic

Since you keep which parameter value is failing to yourself, we resort to guesswork.
I'd try
set "Pentaho_Dir=C:\Transformations&Jobs"
set "Pentaho_Job=Interfases_MI.kjb"
set "path=C:\Program Files\Pentaho\data-integration;%path%"
Kitchen.bat /file:"%PENTAHO_DIR%\%Pentaho_Job%" /log:"%PENTAHO_DIR%\Logs\Interfases_MI.log" /level:basic
The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var. This may not be a problem in the current case, but the resolution of your code would be
Kitchen.bat /file:"C:\Transformations&Jobs"\Interfases_MI.kjb /log:"C:\Transformations&Jobs"\Logs\Interfases_MI.log /level:basic
Whereas cmd would be reasonably happy with the quotes in odd places, kitchen.bat may not be.

In windows you can pass the parameters as
/param="_destination_email=test#test.com"

Related

Batch script to replace lines in a different batch file

I have a batch script that has some software versions that looks like this (the numbers can change):
set NewVer=123
set NewVerServ=456
set AgeNewVer=789
I need to create a different script that, when ran, looks for those specific strings, and replace the numbers with new numbers that I will input in the new script.
I couldn't really get it to work with findstr as it returns mixed results.
I'm sure I'm missing something simple here, can anyone offer any advice?
You could store the values in a separate file, then you can rewrite the complete file each time.
(
echo set NewVer=123
echo set NewVerServ=456
echo set AgeNewVer=789
) > versions.bat
and if you want to use them from another batch file, use:
call versions.bat
Still no wiser as to which specific strings will be involved, but I'd deduce that this new script is intended to replace the variables set in the existing script with new values.
IMHO, the easiest way is to remove those three lines from the existing script (and any other such set variable lines as you may consider need to be altered) and use, as your new script,
#echo off
setlocal
set NewVer=124
set NewVerServ=457
set AgeNewVer=790
call "oldscript.bat"
as the environment inherited by oldscript.bat will now contain the required variables set to their new values.

Escape characters of a file path argument for a batch file

I was making a batch file to take dragged-and-dropped folders for program input. Everything was working fine until I passed a folder, which for the sake of this post, called foo&bar.
Checking what %1 contained inside the batch file looked like C:\path\to\foo or C:\path\to\foo\foo. If the file path were in quotes it would work, so the only working code that slightly takes this into effect is :
set arg1=%1
cd %arg1%*
set arg1="%CD%"
Which changes directory to the passed argument using wildcards. However this only works once for if there is another folder with un-escaped characters inside the parent folder, passing the child folder would result in the parent folders' value.
I tried the answer of this post, which suggests to output the argument using a remark and redirection statement during an #echo on sequence. However no progress occurred in rectifying the problem. Any suggestions?
To recap, I am looking for ways to pass folders with un-escaped characters as arguments to a batch file. The implementation should preferably be in a batch file, but answers using VBScript are welcome. However the starting program must be in batch as this is the only program of the 3 that accepts files as arguments.
To test this, create a batch file with following code:
#echo off
set "arg1=%~1"
echo "the passed path was %arg1%"
pause
Then create folders called foobar and foo&bar. Drag them onto the batch file to see their output. foo&bar will only return C:\path\to\foo.
OK, so the problem is that Explorer is passing this as the command line to cmd.exe:
C:\Windows\system32\cmd.exe /c ""C:\path\test.bat" C:\path\foo&bar"
The outermost quotes get stripped, and the command becomes
"C:\working\so46635563\test.bat" C:\path\foo&bar
which cmd.exe interprets similarly to
("C:\working\so46635563\test.bat" C:\path\foo) & bar
i.e., bar is considered to be a separate command, to be run after the batch file.
The best solution would be to drag-and-drop not directly onto the batch file but onto, say, a vbscript or a Powershell script or a plain old executable. That script could then run the batch file, either quoting the argument appropriately or putting the directory path into an environment variable rather than on the command line.
Alternatively, you can retrieve the original command string from %CMDCMDLINE% like this:
setlocal EnableDelayedExpansion
set "dirname=!CMDCMDLINE!"
set "dirname=%dirname:&=?%"
set "dirname=%dirname:" =*%"
set "dirname=%dirname:"=*%"
set "dirname=%dirname: =/%"
for /F "tokens=3 delims=*" %%i in ("%dirname%") do set dirname=%%i
set "dirname=%dirname:/= %"
set "dirname=%dirname:?=&%"
set dirname
pause
exit
Note the exit at the end; that is necessary so that cmd.exe doesn't try to run bar when it reaches the end of the script. Otherwise, if the part of the directory name after the & happens to be a valid command, it could cause trouble.
NB: I'm not sure how robust this script is.
I've tested it with the most obvious combinations, but YMMV. [It might be more sensible to use delayed expansion exclusively, I'm not sure. It doesn't seem to be necessary except in the first set command. Jeb's answer here might be a better choice if you're going this route.]
For the curious, the script works like this:
Load the original command line into dirname [necessary for the reason pointed out by jeb]
Replace all the & characters with ?
Replace all the quote marks with *
If a quote mark is followed by a space, suppress the space.
NB: it is necessary to suppress the space to deal with both the case where the path contains a space (in which case Explorer adds quote marks around it) and the case where it doesn't.
Replace all remaining spaces with /
NB: ? * and / are illegal in file names, so these replacements are safe.
At this point the string looks like this:
C:\Windows\system32\cmd.exe//c/**C:\path\test.bat**C:\path\foo?bar**
So we just need to pull out the third asterisk-delimited element, turn any forward slashes back into spaces and any question marks back into ampersands, and we're done. Phew!

Windows batch file ren using date variable

Learned batch a while ago in school, haven't ever used it until now b/c I had an idea. Just for fun I wanted to mess around with powercfg batteryreport with my new laptop, but I want to archive. I was going to try and figure out how to have the file powercfg batteryreport spits out changed in some sort of numerical order but I don't even know where to begin, so I decided to just make a new line that takes the current file created and adds the date. All of this is taking place inside of a special folder I created, so pathing isnt necessary.
#echo off
powercfg batteryreport
rename "battery-report.html" "batteryreport %date%.html"
This exact script works without the date variable, but never with it in, but of course I need a variable present in order to have multiple reports saved, as opposed to it writing over itself every time. I've tried messing with all spacings, quotes vs no quotes, no luck. Help (or a better way, preferably with explanation) would be greatly appreciated.
In all likelihood, your date format contains / which is illegal in a filename.
Use %date:/=-% in place of %date%. this converts each / to - (see set /? from the prompt for docco)
Equally, you could use %time::=.% o convert the time to a version usable in a filename.
To remove dayname, you need to use an intermediate variable:
set "dateonly=%date:* =%"
ren ... "...%dateonly:/=-%..."
The * means "all of the characters up to and including the first space" and this is replaced by nothing (the string following the =)
see set /? from the prompt for details.

Windows Batch Variable inside variable

I'm not very good at code, but I'm trying to get something to work, which would make my life easier.
I need to get a variable to run from within a variable, if possible, but can't get it to work out.
set MAP1=Esseker
set %MAP1%MODS=#exile
-mod=%%MAP1%mods%
This is not the full code, but it's just a section showing what it is I am trying to do. -mod= is correct as it's part of a startup parameter.
You can do this by enabling delayed expansion.
Here's an example script:
#echo off
setlocal EnableDelayedExpansion
set MAP1=Esseker
set %MAP1%MODS=#exile
echo !%MAP1%MODS!
-mod=!%MAP1%MODS!
#echo on
Not sure if that -mod line should be a set, but I've copied what you had above, making the assumption that it's correct.

Batch File: Output variable to text file

I would like to output a variable to a text file. I have done this in my code, but I have one output within an if statement that I can't get to work.
if not exist "%TuningLog%" (
set Title=Tuning Parameters Saving Log
set LogTitle=--------- %Title% ---------
echo %LogTitle%>> "%TuningLog%"
)
All its supposed to do is check first for the existense of a log file and if it doesn't exist then I want to in the first instance append a title.
But all I get in the log file is " echo is off." If I don't use a variable and just place text there then it works fine.
Can anybody spot the problem? Thanks for reading and for any help!
UPDATE:
I changed my code to. I don't know much about delayed expansion, but I tried this and it happened to work...
if not exist "%TuningLog%" (
setlocal enabledelayedexpansion
set Title=Tuning Parameters Saving Log
set LogTitle=--------- !Title! ---------
echo !LogTitle!>> "!TuningLog!"
endlocal
)
If anyone can provide a reason as to why the first attempt didn't work and this did, then please inform this thread for the purposes of learning. Thank you!
because or early expansion. your variable gets replaced with its value at the moment the block starts. at this time, it is still undefined and thus empty. if you have echo on, you can watch this happening on screen. if you enable delayed expansion, as you did in your second example, it gets only expanded at the moment it is used, so it works as you expect variables to work in "real" programming languages.
EnableDelayedExpansion
causes Variables to be expanded in simple language it causes the system to treat the value of variable and not the variable name itself

Resources