Command line to kill Adobe Reader process only if it was not already running - pdf

I am trying some cmd.exe coding for the first time. I created a registry key, which contains a command called by a custom protocol handler­. What I'm trying to accomplish : Call Adobe Reader which will automatically print a pdf to a given printer. This part already works. I'm using powershell to download the pdf locally from a URL. I also want to kill the Acrobat process once i'm done, but only if it was already opened BEFORE calling my command, so I don't kill the process if the user already has a pdf opened when calling the protocol handler via Chrome.
Excuse the more than ugly formatting, a one-line command is a must in my case, I can't make a batch file or .ps1 file.
cmd /V /C set exists="0" & (tasklist | find "AcroRd32.exe" > nul) & if
(%%ERRORLEVEL%%==0 set exists="1") & set myvar=%1 & call set
myvar=%%myvar:ie:=%% & call set printer=%%myvar:*###=%% & call set
printer=^"%%printer%%^" & call set printer=%%printer:'=%% & call start
powershell.exe -ExecutionPolicy Bypass -Command $url=%%myvar%%; $pdf =
$url.split('#'); md -Force c:\TempPdf "| Out-Null"; (New-Object
System.Net.WebClient).DownloadFile($pdf,'C:/TempPdf/PatientLabel.pdf'); &
timeout /t 2 & call start acrord32.exe /n /t C:/TempPdf/PatientLabel.pdf
%%printer%% & timeout /t 2 & (if !exists!=="1" taskkill /f /im acrord32.exe)
The part which does not work is the tasklist/taskkill. I want to set the %%exists%% variable to 1 if I can find the process running before starting AcroRd32.exe and kill it after printing if %%exists%%=1. I think this might just not be working because of the execution time/parse, but I don't really know how to use /V and !var! anyway. The exists and ERRORLEVEL variables don't seem to expand.
Thanks for the help !

Related

Stop Robocopy locking files when copying

I have a little Windows VB front end to a Robocopy command. Here is the line of code that passes the parameters to Robocopy:
.StartInfo.Arguments = """" & txtCopyFrom.Text & """ """ & txtCopyTo.Text & _
""" /E /B /XJ /XF ""~*.*"" ""*~.*"" ""desktop.ini"" ""Thumbs.db"" "".lock"" "".Sync*""" & _
" /xd ""Rubbish"" "".Sync*"" "".Box Sync"" ""_private"" ""Outlook Files"" /FFT /R:2 /W:5 /V /TEE"
It all works fine except when a user wants to modify a file that is in the middle of being copied. The user gets an error from their program and then has to wait several minutes while the file is being copied (the file can be upto 1GB in size and is being robocopied to a USB stick.
Is there a switch or some way to get Robocopy to not lock the file while it is being copied? obviously Robocopy would need to abort the copy and wait until the file was available so it could try again. I've searched online for a solution but all the problems seem to be the other way round where Robocopy is not copying locked files.
Some more details regarding what I'm doing:
My code calls Robocopy every 30 seconds with above flags (I know robocopy can loop by itself but It wasn't doing quite what I wanted). I envisaged my app just being run at the start of the day and then shutdown at the end of the day. On a different machine a user will open some 3rd party Imaging viewing software which will lock the file being opened, update some of the image's meta data and then save the changes. The problem occurs is when my software locks the image file before the user and then the user can't edit their file.
Thanks
Kristian
In the code where the user wants to access the file, you could add this before the code to access the file.
If Not p.HasExited Then
p.Close()
End If
Threading.Thread.Sleep(100) ' to give the process time to close the file and close itself

Running Macro from .bat with another Access db open

I need to run a few Access macros automatically periodically throughout the day, I do this by scheduling a batch file to open the relevant Access db and run the macro.
The problem that I am having is, if I am working on another Access database and therefore already have an instance of Access open, the batch file runs but only opens the database containing the macro that is supposed to run and stops. So the macro doesn't run.
Has anyone else had this issue or know how to solve it?
The batch files that task scheduler calls look like this:
start /min "C:\Program Files\Microsoft Office 15\root\office15\MSACCESS.EXE" "Q:\TC\DNI_Updater\DNIUPDATER.accdb" /X DailyUpdate
I could not find a way to get the DailyUpdate macro to run in the second Access session when using start /min to open that session.
I'm still unsure whether it's possible, but I gave up and switched to VBScript instead. With that approach, it's easy to start the second Access session minimized and run the macro ...
Option Explicit
Dim AccessExePath, DbPath, CmdLine, objShell
DbPath = "C:\Users\hans\Documents\test_hfu.accdb"
AccessExePath = "C:\Program Files (x86)\Microsoft Office\Office14\MSAccess.exe"
CmdLine = """" & AccessExePath & """ """ & DbPath & """" & " /X DailyUpdate"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run CmdLine, 7 ' Minimize. The active window remains active.
If this approach is satisfactory, you can use the script file as your scheduled task's "Start a program" Action property ...

Execute .bat file vb.net

I need to install my win service. With installUtil it is just few lines of code.
#ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i MyService.exe
echo ---------------------------------------------------
echo Done.
pause
But my thoughts are without creating .bat file and then runing it.
Is there any way i can ".execute" those lines of code above without creating .bat file runing it and then deleting it ?.
I will need to dynamically create this code every time because i need to enter the username/password depending what user entered on .net form.
You could start cmd and doing it in one line via it's arguments:
Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & InstallUtil /i MyService.exe")
And if you want it to show the text you wrote and to "pause" (stay open):
Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & echo Installing MyService... & echo --------------------------------------------------- & InstallUtil /i MyService.exe & echo --------------------------------------------------- & echo Done. & pause")
Commands are separated by " & ".
I know it's been a month since you first asked this, but I recently came up with a pretty good solution to this - only using this simple VB.NET code:
Public Sub InstallService(ByVal ServicePath As String)
Dim InstallUtilPath As String = IO.Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "installutil.exe")
Dim InstallUtilProcess As Process = Process.Start(InstallUtilPath, """" & ServicePath & """")
InstallUtilProcess.WaitForExit()
'Service is now installed.
InstallUtilProcess = Process.Start(InstallUtilPath, "/i """ & ServicePath & """")
InstallUtilProcess.WaitForExit()
'The second action is now done. Show a MessageBox or something if you'd like.
End Sub
The ServicePath parameter is the path to the service that you want to install.
The InstallUtilPath variable will be set to the path of the installutil.exe application. It will get the path for the current framework you're running.
As I'm running .NET Framework 4 the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe
Hope this helps!

Call shell command in Excel 2010 VBA

When I call a shell command in vba, the program opens as does the command prompt but nothing happens and no results are obtained. I can not seem to figure it out and need some help please.
Command that works
C:\Program Files\BioDiscovery\ImaGene 9.0>ImaGene.exe -batch "C:\Users\cmccabe\Desktop\EmArray\Design\test_11_19_2015.bch"
My attempt in excel 2010 using VBA (which opens the program and command prompt, but does not execute the command).
Dim Par As String
Par = "dir c:\Program Files\BioDiscovery\ImaGene 9.0\ImaGene.exe -batch "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
Call Shell("C:\WINDOWS\system32\cmd.exe /c " & Par, 1)
MsgBox ("ImaGene analysis complete")
May not be the best answer but the below solution works for me for similar situation.
open a notepad "c:\Program Files\BioDiscovery\ImaGene 9.0\ImaGene.exe" -batch "C:\Users\cmccabe\Desktop\EmArray\Design\imagene.bch"
save as say "test.bat"
In excel call this .bat file
Dim wshell As Object
Set wshell = CreateObject("wscript.shell")
wshell.Run Chr(34) & "full path\test.bat"
Hope this helps.

Delete application executable on exit

So I am trying to get my application to delete itself on exit.
I am currently using this (found here):
Process.Start("cmd.exe",
"/C choice /C Y /N /D Y /T 3 & Del " + Application.ExecutablePath);
Application.Exit();
The CMD window comes up, but will not remove the application. Any ideas why not?
EDIT:
So it works sometimes... other times it does not... ideas?
I think the problem is that you cannot delete an application while it's running.
You could introduce some delay in the cmd command(I don't know how), or you could simply make another application with a timer set on 5 sec. And insert the code:
System.IO.File.Delete(Application.ExecutablePath)
Me.Close
Then just use the Process.Start("your/Delete/App/Path.exe") in the first app.