I am trying to execute below commands for scaning a file using command prompt.It executes properly but sometimes it fails to execute the process and code gets hanged at process.waitfor().Below is the piece of code I am trying.Is there anything wrong?
Long startTime = System.currentTimeMillis();
Process process = Runtime.getRuntime().exec("cmd start cmd.exe /k \"C: && cd "+ANTIVIRUS_PATH+" "+WL_HOME+"\\"+ serverImagePath+ actualFileName+" && exit");
process.waitFor();
process.destroy();
Long endTime = System.currentTimeMillis();
System.out.println("Total Time:"+(endTime-startTime));
Related
I want to write a particular command in script and execute it directly in cmd from unity for example here I am writing a simple change directory command but it is not executing directly rather I have write on my own the sample code is below
string pathToExe = Application.dataPath.Replace(#"/", #"\") + "\\Plugins";
string command = "D:";
Process process = Process.Start("cmd.exe", command);
process.WaitForExit();
process.Close();
enter image description here
I have created a small program in Visual Studio - a form with a button to execute the program when clicked - and built the .exe.
This program is supposed to open PowerShell and run a ps1 file.
When I run it from within VS in debug mode, it works as expected running the ps1 script without error.
When I build the project and run the exe as an administrator, it briefly opens PowerShell but closes immediately.
The output file states that the execution of scripts is disabled on this system, although this is incorrect as I can run any number of PowerShell scripts manually by right clicking them and running them with PowerShell.
I have tried changing the execution policy from Signed, to AllSigned, as well as Unrestricted, but it did not help.
Here is my code:
if (File.Exists("LiveSiteTests.ps1"))
{
File.GetAttributes("LiveSiteTests.ps1");
string strCmdText = Path.Combine(Directory.GetCurrentDirectory(), "LiveSiteTests.ps1");
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = #"C:\windows\system32\windowspowershell\v1.0\powershell.exe";
process.StartInfo.Arguments = "\"&'" + strCmdText + "'\"";
process.Start();
string s = process.StandardOutput.ReadToEnd();
process.WaitForExit();
using (StreamWriter outfile = new StreamWriter("StandardOutput.txt", true))
{
outfile.Write(s);
}
}
I have a c# program, integrated with a command line program. I want to run the command line program twice(start, finish, start again, finish again). Now I use a timer to set a special time period for every run, for example, give first run 10 seconds, no matter it is finished or not, after 10 seconds, the program starts the second run.
I want the second run can run automatically after the fist run finshed, How to do it? How to detect the first run is finished, and then take a trigger to start the second run?
Assume you run the command line as a process, see this answer to check if the process has finished:
Find out if a process finished running
if (process.WaitForExit(timeout))
{
// user exited
} else {
// timeout (perhaps process.Kill();)
}
In a command line you can launch this command:
start "" /w will execute the command and wait until it is finished before proceeding.
for %a in (1 2) do start "" /w "programA.exe"
I have script to uninstall a package say pkg_1 using it's uninstallstring (i.e. setup.exe -remove). After that it is trying to remove another package say pkg_2. The uninstallation of pkg_1 takes around 1 to 2 minutes. the script looks like as below
rc = shellobj.Run("cmd /C " & uninstall_string_1 & " /silent",0, true)
rc1 = shellobj.Run("cmd /C " & uninstall_string_2 & " /silent",0, true)
Here what's my problem is, if you place WScript.sleep(120000) between above two statements, both packages are getting removed successfully. Otherwise, uninstaaltion of pkg_1 is halted and uninstallation of pkg_2 begins immediately. As a result only pkg_2 is getting removed(without sleep).
How can i do the uninstallation of two packages without using sleep method?
FYI
If you run uninstall_string_1 from command prompt, new prompt will be opned after 0-5 seconds and uninstallation proceeds in background
Arjun, I read this in another forum :
When we use scripts to install or uninstall packages, it depends very much on the implementation of the package. Let's say setup.exe itself run as process A. Process A does some nominal verification then spawns a process B, the real installation engine, then close itself out. In this case, the bWaitOnReturn(which you have set to true in your code) has done its work and the control will hand to the next statement. However, the process B is still running. That is out of the control of the bWaitOnReturn. So your 2nd package will start before your 1rst package is actually completed. Maybe your 1rst package is not actually halted, it might be taking time and executing in the background. Thats just a guess anyways.
If that is not the case, can you also try this :
Set obj0 = CreateObject("WScript.Shell")
Set objExec = obj0.Exec("your package../../")
' We will set a loop until finished
Do While objExec.Status <> 1
WScript.Sleep 100
Loop
So every 100ms, script will check if your package has completed execution. And if it has, it will move to next statement.
Try this one:
strCmd = uninstall_string_1 & " & " & uninstall_string_2
rc = shellObj.Run("cmd /C " & strCmd, 0, True)
I have a windows batch file that looks similar to:
C:\DoStuff.cmd
move output.bak C:\newfolder\output.bak
The problem i have is that DoStuff.cmd executes a java program that once complete exits the batch run back to the command prompt. Line 2 never gets hit.
i have tried the following instead to execute the command in a new window:
start "My program" /WAIT C:\DoStuff.cmd
move output.bak C:\newfolder\output.bak
What happens with the above is that the new command window spawns the cmd file runs and exits back to a waiting command prompt and the window never closes, leaving the first command window waiting and the second doing nothing stuck after finishing step one.
How do i execute the first command without it having control of the batch run somehow?
many thanks in advance
You can use DOS call command:
#echo off
call C:\DoStuff.cmd
echo Exit Code = %ERRORLEVEL%
After getting error code you can proceed for example with:
if "%ERRORLEVEL%" == "1" exit /B 1