How to Release File-Lock on File Created by Shell in VBA? - vba

This command generates an output file:
Update: See new command
Shell "cmd /c fc file1 file2 > result.txt"
i initially didn't use cmd /c at the front of the command:
Shell "fc file1 file2 > result.txt"
That failed to generated the expected result.txt file. i don't know why. Including cmd /c at the front of the command makes it work.
The subsequent Open statement gets "Path/File access error"
iFile = FreeFile
Open "result.txt" For Input As iFile
Neither the file nor parent directory are open in Windows Explorer.
The following changes don't help (TaskKill and AccessRead):
vPID = Shell("cmd /c fc file1 file2 > result.txt", vbHide)
Shell "TaskKill /F /PID " & CStr(vPID), vbHide
iFile = FreeFile
Open "result.txt" For Input Access Read As iFile
The Open statement works if it's run as a separate, subsequent process.
How to do the Open in the same process?

Related

Batch file to open .sql file and "execute as Script"

I am trying to create a Batch file to open .sql file and execute the queries in .sql file but instead of execute each query, I want to run all at once "execute as Script".
Below is the Code which is executing only one query
echo start
cls
C:
cd C:\Program Files (x86)\Quest Software\Toad for Oracle\
%systemroot%\Syswow64\cmd.exe /C toad.exe -c User_Name/password#Database -f "C:\My Code\Stored SQL\example.sql" /exec
CLOSEToad
Exit
:END
CLS

command window not closed after restart PC Remotly using vb.net

I want to run two cmd commands in vb.net
Dim command As String ="Net USE \\serverip\IPC$ password /USER:username"
Dim command2 As String ="shutdown /r /t 3 /m \\serverip"
Process.Start("cmd.exe", "/k" + command)
Process.Start("cmd.exe", "/C" + command2)
Command window is opened how can we close this.
I try process.kill() it is not working.

Exit processes launched from cmd with cmd exit

I wrote a script that launches a cmd window. Cmd window further launches several processes. What I want is that on closing the cmd window manually or using exit command, all processes launched via cmd also get killed.
Technically it is not possible to change how the exit command works. What you could do is make your own custom start and exit commands by having something like this:
Start Command Batch File: (context would be start2 program.exe)
start "" "%1"
if exist onExit.txt echo %1>> onExit.txt
if not exist onExit.txt echo %1> onExit.txt
Save that as start2.bat
Exit command BatchFile: (context would just be Exit2)
for /F "tokens=*" %%A in (onExit.txt) do (taskkill /f /im "%%A"
exit
Save that as Exit2.bat
What those scripts do is keep track of each program you start with the custom command start2 and then taskkill them on your custom exit. You could have these files in the same directory of the batch file, or you could even have your batch file create these files with echo file contents> file.bat and echo more file contents>> file.bat. This is the best you could probably do without placing the actual cmd Executable in system32, which I would certainly not recommend since many other programs use it. Hope this helps

WMIC How to pass arguments/ parameters with exe

I want to call exe from remote system using WMIC direct command. for that I prepare one command here
WMIC /NODE:"RemoteSys" /USER:"domain\username" /PASSWORD:"XXXXXXXXX" PROCESS CREATE "C:\Program Files (x86)\Company\Product\productapp.exe"
But issue here is this productapp.exe is expecting an arguments/ parameters like " -p PlantA "
Question here is How to pass it ?
I tried many alternate ways but all fails like:
WMIC /NODE:"RemoteSys" /USER:"domain\username" /PASSWORD:"XXXXXXXXX" PROCESS CREATE "C:\Program Files (x86)\Company\Product\productapp.exe" -p PlantB
Output: Invalid Verb Switch
WMIC /NODE:"RemoteSys" /USER:"domain\username" /PASSWORD:"XXXXXXXXX" PROCESS CREATE "C:\Program Files (x86)\Company\Product\productapp.exe" "-p PlantB"
Output: Invalid format. Hint: = [, ].
Don't know what is paramlist & How to use that.
Can anyone help me here ?
I've had to do something similar with MATLAB. What you could try is opening a command prompt and executing the program with parameters from that command prompt.
WMIC /NODE:"RemoteSys" /USER:"username" /PASSWORD:"XXX" PROCESS CALL CREATE "cmd.exe /c cd C:/Program Files (x86)/Company/Product/ & productapp.exe -p PlantB"

Running Command Line Arguments

I'm trying to run a command line argument through VB.NET using the Shell() command.
I'm trying to use this piece of code:
FOR /R %I in (*.pdf) DO #pdf2swf.exe "%~fI" -o "%~dpI%~nI.swf" -f -T 9
-t -G
Using this:
Shell("FOR /R %I in (*.pdf) DO #pdf2swf.exe "%~fI" -o "%~dpI%~nI.swf" -f -T 9
-t -G ")
However, the interpreter is giving me this error:
Character is not valid. (BC30037)
For the %~ part.
I also tried created a string and passing the argument to the Shell() command by using Shell(StringName) but I still get the same error in the string.
How could I fix this issue?
This is not proper use of the Shell Method:
Public Shared Function Shell (PathName As String, [...]) As Integer
Parameters
PathName
Type: System.String
Required. String. Name of the program to execute, together with any required arguments and command-line switches. PathName can also include the drive and the directory path or folder.
The first parameter is supposed to be the name of a program to execute. FOR is not a program, it's a built-in feature of the cmd.exe command line interpreter.
As far as I can see, you have the following options:
Option 1: Explicitly call cmd.exe and pass the string that you want to execute with the /c parameter:
Shell("cmd.exe /c for /R %I ...")
Don't for get to duplicate quotation marks (") to escape them.
Option 2: Create a batch file and call the batch file using Shell.
Option 3: Don't use FOR to find the files you need, but use the methods of the System.IO namespace, e.g. Directory.EnumerateFiles, instead.
Escape your internal quote marks like this.
Shell("FOR /R %I in (*.pdf) DO #pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
As I recall, in VB.Net you escape double quote marks by doubling them.
EDIT:
It might help if you do the iteration outside of the Shell. (Certainly to debug)
Dim sourceFolder As String = "c:\Your call"
Dim sourceFiles As String[] = Directory.GetFiles(sourceFolder, "*.pdf")
ForEach file As String In sourceFiles
Dim justName As String = Path.GetFileNameWithoutExtension(file)
Dim shellCall As String = _
String.Format("pdf2swf.exe ""{0}"" -o ""{1}.swf"" -f -T 9 -t -G", _
file, justName)
Shell(shellCall)
EndFor
You could also cosider using System.Diagnostics.Process instead of Shell
Try escaping the quotes (2 quote marks - "" - in VB.NET, IIRC):
Shell("FOR /R %I in (*.pdf) DO #pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
If pdf2swf.exe is not in the same folder your program's executable is running from, that could be a reason you're getting the error.
Also, you'll have the same issue with the *.pdf files if they are in a different folder other than where your executable is. You can specify the drive and path to search:
Shell ("FOR /R C:\SomeFolder %I in (*.pdf) DO #pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")