I am trying to pass parameters from a .Net console app to a batch file. The parameters are not coming into the batch file.
How can I properly set up passing the parameters into the bat file?
Here is the method in the console app that I'm executing.
private static int ProcessBatFile(string ifldr, string ofldr, string iext, string oext, Int16 filewidth, Int16 fileheight, Int16 ctr)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = ConfigurationSettings.AppSettings.Get("BatProcessDir") + "imagemagick.bat";
psi.Arguments = "-ifldr=" + ifldr + " -ofldr=" + ofldr + " -iext=" + iext + " -oext=" + oext + " -iwid=" + filewidth + " -ihgt=" + fileheight;
psi.UseShellExecute = false;
Process process = new Process();
process.StartInfo = psi;
process.Start();
return ctr;
}
Below, is the code in the bat file I'm trying to execute:
#echo on
echo %ofldr%
echo %ifldr%
echo %iwid%
echo %ihgt%
echo %oext%
echo %iext%
If you pass them as paramters, you can do this in the c# code:
psi.Arguments = ifldr + " " + ofldr + " " + iext + " " + oext + " " + filewidth + " " + fileheight;
and do this in the batch file:
#echo on
set ifldr=%1
set ofldr=%2
set iext=%3
set oext=%4
set iwid=%5
set ihgt=%6
echo %ofldr%
echo %ifldr%
echo %iwid%
echo %ihgt%
echo %oext%
echo %iext%
As an alternative solution, you can also directly modify the environment before executing the batch file using System.Environment.SetEnvironmentVariable:
System.Environment.SetEnvironmentVariable ("ifldr", ifldr);
....
This causes less problems if the parameters may contain spaces.
Related
I wrote batch script which executes soapui from command line. It does sth like Launch LoadTestRunner and generates reports in indicated folder.
In my Test Steps I need to change dates in specific nodes on tomorrows date. I wrote groovy script which works fine in soapui but I can't load it from batch script.
My question is: is there a possibility to run tests from the console (batch script) but first run groovy script which will change dates in the same batch script?
My groovy script:
import com.eviware.soapui.impl.wsdl.teststeps.*
def ui = com.eviware.soapui.support.UISupport;
def project = context.testCase.testSuite.project
def testSuite = project.getTestSuiteAt(0)
def testCase = testSuite.getTestCaseAt(0)
//set date
def today = new Date()
def dd = today.getDate() + 1
def mm = today.getMonth() + 1 //January is 0!
def yyyy = 1900 + today.getYear()
if (dd < 10) dd = '0' + dd
if (mm < 10) mm = '0' + mm
tomorrowsDateAndTime = yyyy + '-' + mm + '-' + dd + "+01:00"
//xml paths to change date
def somePathToChangeDate = "//somePathToChangeDate"
def somePathToChangeDate2 = "//somePathToChangeDate2"
def stepList = testCase.getTestStepList() //list of all tests in package
for (singleTest in stepList) {
log.info(" " + singleTest.getName())
def testName = singleTest.getName()
testSuite.setPropertyValue("testName", testName)
def testStep = testCase.getTestStepByName(testName)
def testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(testStep);
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(testStepContext)
def inputHolder = testName + "#Request"
def holderRawReq = groovyUtils.getXmlHolder(inputHolder)
holderRawReq[somePathToChangeDate] = tomorrowsDateAndTime
holderRawReq[somePathToChangeDate2] = tomorrowsDateAndTime
holderRawReq.updateProperty()
}
log.info " Dates are updated"
My batch script:
#echo off
cd %~dp0\SoapUI-5.2\bin
cmd.exe /C loadtestrunner.bat -s"TestSuite 1" -cmyTests -l"LoadTest 1" -uusername -ppassword -r -f%~dp0\reports %~dp0\my-soapui-project.xml
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" %~dp0\Reports\LoadTest_1-log.txt| find /C ":""
::this code checks if there are any error reports
for /f %%a in ('!cmd!') do set iterator=%%a
echo %iterator%
cls
if [%iterator%] equ [3] (echo "Tests was completed successfully") else (
echo "Tests failed"
)
pause
I have to run the following command for hundreds of .docx files in a directory in a windows in order to convert them to .txt.
java -jar tika-app-1.3.jar -t somedocfile.doc > converted.txt
I was wondering if there is any automatic way such as writing a ".bat" file to do this.
Yes you can do it in batch. An example isn't coming to mind in order to help you but since you are running java commands anyway you can do it through java too. Here is an example of how you can be running the commands on CMD from java. Hope this helps.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
try {
executeCMD("java -jar tika-app-1.3.jar -t " + <YOUR_DOC_FILE> + " > " + <TXT_NAME_OF_FILE> );
}catch(Exception e){
e.printStackTrace();
}
private void executeCMD(String command) throws Exception{
Process process = Runtime.getRuntime().exec(command);
printMsg(command + " stdout:", process.getInputStream());
printMsg(command + " stderr:", process.getErrorStream());
process.waitFor();
if(process.exitValue() != 0)
System.out.println(command + "exited with value " + process.exitValue());
}
private void printMsg(String name, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
while((line = in.readLine()) != null){
System.out.println(name + " " + line);
}
}
UPDATE
Okey here is the way simpler batch way which I couldn't think of yesterday :D
for /r %%i in (*.doc) do java -jar tika-app-1.3.jar -t %%~ni > %%~ni.txt
It reads all the *.doc files in the directory it is executed, also the "~n" is in order to list only the filenames ( in your example "somedocfile" ) because otherwise it will do something like "C://...../somedocfile.doc" and changing the extension might be annoying.
I'm trying to make a countup timer in batch and so far, I have this
:timer
set time=0
:time
set /a time=%time%+1
ping localhost -n 2 >nul
goto time
The problem is, I want this to run at the same time another event is happening in the same batch file. How can I do this?
Are you wanting to time how long it takes to execute your batch script? If you have PowerShell installed you can use the Measure-Command cmdlet.
Save this as timeit.bat:
#echo off
>~timeit-%~n1.bat echo(%*
powershell "Measure-Command {Start-Process ~timeit-%~n1.bat -Wait}"
del ~timeit-%~n1.bat
Then when you want to time the execution of something, say, systeminfo, just timeit systeminfo.
If you prefer a non-powershell solution (as powershell takes several annoying seconds to prime itself in its first run per Windows session), here's a faster batch / JScript hybrid solution (more info). It also executes the command in the same window, whereas the powershell solution spawns a new window for your command. Save this as timer.bat:
#if (#a==#b) #end /* begin multiline JScript comment
:: timer.bat command args
:: measures execution time of a command
#echo off & setlocal
if "%~1"=="" (
echo Usage: %~nx0 command args
goto :EOF
)
cscript /nologo /e:JScript "%~f0" %*
goto :EOF
:: end batch portion / begin JScript */
var osh = new ActiveXObject('wscript.shell'), cmd = [], b4 = new Date();
for (var i=0; i<WSH.Arguments.length; i++) {
var arg = WSH.Arguments(i);
cmd.push(/\s/.test(arg) ? '"' + arg + '"' : arg);
}
var exe = osh.Exec('cmd /c ' + cmd.join(' '));
while(!exe.StdOut.AtEndOfStream) WSH.Echo(exe.StdOut.ReadLine())
var x = (new Date() - b4) / 1000,
d = Math.floor(x / 86400),
d = d ? d + ' days ' : '',
h = ('0' + (Math.floor(x / 3600) % 24)).slice(-2),
m = ('0' + (Math.floor(x / 60) % 60)).slice(-2),
s = Math.floor(x % 60) + x % 1,
s = (s < 10) ? '0'+s : s;
WSH.Echo('\r\nExecution completed in ' + d + h + ':' + m + ':' + s);
I'm currently using a Process Task to try to unzip a file in SSIS with the following settings:
I'm using this statement:
e "Inventory.zip" -o"c:\broinctarget\" -y
I'm actually generating the above statement using:
"e " +"\"" + "Inventory.zip" + "\"" + " -o" + "\"" +#[$Package::targetLocation] +"\""+" -y"
#[$Package::targetLocation]
Am I doing something that is obviously wrong?
How do I unzip a *.zip file using 7zip in SSIS?
I ran into a similar issue on an SSIS package I wrote. I ended up generating a batch file in a c# script, and just calling that batch file in an execute process task.
I also needed the flexibility to add a password option based on some table parameters.
I realize it's an extra step, and if I were unzipping a ton of files it might matter, but for a For-Each container that is going to hit 5-10 files, its not a big deal - and you can easily read the batch file to see what was run too.
string exe7z = Dts.Variables["s7ZIPPath"].Value.ToString();;
string zipFile = Dts.Variables["sZIPFile"].Value.ToString();
string outPath = Dts.Variables["sFileLocation"].Value.ToString();
string password = Dts.Variables["sZIPPassword"].Value.ToString();
if (password.Length > 0)
{
password = "-p\"" + password + "\"";
}
if (!(outPath.EndsWith("\\")))
{
outPath = outPath + "\\";
}
string batchFile = outPath + "unzip.bat";
Dts.Variables["sZipBatchFile"].Value = batchFile;
using (StreamWriter writer = new StreamWriter(batchFile))
{
writer.Write("\"" + exe7z + "\" e \"" + zipFile + "\" -o\"" + outPath + "\" -y " + password);
}
Hi I would like to know as to how I can pass an array to vbscript file. I have 5text files containing integers. I read them in a batch program and have to pass those 5 integers to the vbscript file and detect them in vbscript
I don't know if this is what you want:
How to use:
Integers.vbs "number1" "number2" "number3" "number4" "number5"
Integers.vbs
' set the vars
Integer1 = Wscript.Arguments.Item(0)
Integer2 = Wscript.Arguments.Item(1)
Integer3 = Wscript.Arguments.Item(2)
Integer4 = Wscript.Arguments.Item(3)
Integer5 = Wscript.Arguments.Item(4)
' Print all the args
Set objArgs = WScript.Arguments
For Each strArg in objArgs
WScript.Echo strArg
Next