Close cmd window with vba excel - excel

I have a SQL request in my vba code
Set WSJ = VBA.CreateObject(“WScript.Shell)
WSJ.run “cmd.exe /C chcp 0000 & cd C:\oracle... “ & myPath & “.sql && exit”, 1,1
it worked perfect but recently stopped closing cmd window and not continue the process (it doesn't happen all the time, 1 or 2 times for day). There is no error, the last line is «disconnected from the server», but the window does not close and the code does not continue to work.
is it possible to write in the code a check for closing cmd window after a minute or something like that? or configure sql to reach this line and close
Thanks

please try with below code
Sub closwWindow()
Call Shell("cmd.exe /S /c" & "cd /d C:\UTAS-SA && del /f/s/q BJSFM > nul", vbNormalFocus)
End Sub

As the return value of WScript.Shell.Run is not very useful, I've tried to do it in another way, but I only found a way to get this done from command prompt itself:
Using Start, I can start a command prompt with the title I'm giving it, like:
start "this is an interesting window" cmd.exe
As a result, a command prompt is started, having "this is an interesting window" as the title. You can then use Tasklist in order to find this, something like:
tasklist /V | findstr "this_is_an_interesting_window"
cmd.exe 8240 Console 6 3 636 K Running DESKTOP-D0GIHSU\plop 0:00:00 this_is_an_interesting_window
From there you can retrieve the process ID (8240) in this case, and you can use TaskKill in order to kill it:
taskkill /FI "PID eq 8240"
SUCCESS: Sent termination signal to the process with PID 8240.
Good luck

Related

cmd.exe stays open after start command opeing multiple files

I already googled and tried several solutions but without success...
My batch script looks the following, it opens four files but then cmd.exe window stays open and cannot be closed anymore. Even Task Manager asks for admin rights if I want to close. I am no admin, only normal user, so to force closing the cmd window I can only logoff and re-login to windows.
I already tried several options ("/b" option on start command, "exit" command at the end, also "exit 0" ... without any difference)
start "" /b "file1.xlsx"
#ping -n 3 localhost> nul
start "" /b "file2.xlsx"
#ping -n 1 localhost> nul
start "" /b "file3.xlsx"
#ping -n 1 localhost> nul
start "" /b "file4.xlsx"
I am using Windows 10 Enterprise 21H2.
At the bottom add:
:end
exit
Then most probably you can solve this 😊
#phuclv Thank you! I changed the pause from ping command to timeout command as you proposed: and this solved the problem for me.
Great! Thank you!
start "" /b "file1.xlsx"
timeout 2
start "" /b "file2.xlsx"
timeout 1
start "" /b "file3.xlsx"
timeout 1
start "" /b "file4.xlsx"

Configure the run command

I begin to use geany to play with rust under Windows 10.
I would like to configure the "run command" to read stdin from a file (e.g. my_program < in.txt)
I am only interest with "run command". I tried to write "< in.txt", "<" "in.txt" in the first or in the second box of the line "run", nothing works.
Have you a solution ?
Thank you for any tips.
I found the solution : I wrote cmd /K "%e < in.txt" in the first box of the "Run" line.

Trouble Deleting files programmatically in VBA

I had a hard time with Windows 7 OS to where it would think that certain folders are in usage (not allowing folder delete), when in fact they aren't.
After alot of research and trial and error, I was able to use a command that worked well on Windows 7:
rmdir /S /Q "S:\Allied MTRS\Not Scanned\FITTINGS AND FLANGES\RG AR 2686 MOVED FOR AUTO INDEXING"
When I try to run this programmatically via shell command (see code below), it gives me: "File Not Found" message.
So if you try to run it programmatically first, it won't work. Then try to run the same thing, via command line, it works fine. Of course, if you try to run it grammatically again, after that, it will give you "File Not Found" (naturally, since the folder is already deleted). If you want to retry the experiment, you have to try on another folder....
Any ideas?
Sub tryitz()
Dim s As String
Dim ReturnCode As Integer
s = "S:\Allied MTRS\Not Scanned\FITTINGS AND FLANGES\RG AR 2686 MOVED FOR AUTO INDEXING"
s = "rmdir /S /Q " + Chr(34) + Trim(s) + Chr(34)
ReturnCode = Shell(s)
End Sub
Here is a possible answer, but I am looking for something better.
But hey, if not, at least this works, just a little bit more labor in terms of writing a bit of code...
Create a batch file
DelFile.Bat, say.
The DelFile.Bat is edited by my program (programmatically).
I edit it so that it has my desired "rmdir /S /Q? statement.
Then, I run it through shell.

How to open a *.xlsx file containing a space in file name found by command DIR in Excel?

I have a batch file that opens an Excel file with a space as well as date in its file name.
For example: Book 1-27Aug2016
Currently, I am having trouble disabling the delimiters so that command start doesn't try to open two files: Book.xlsx and 1-27Aug2016.xlsx
Here is my code:
for /f "delims=*" %%# in ('dir /tw /o-d /b "Book 1-*"') do (start excel %%#& exit)
I referenced the web page in SS64 Windows CMD Shell forum below for disabling/modifying delimiters, but I still have yet to experience success.
For /f documentation
Lastly, once I remove the space from the file name, the batch runs without any issues.
What do I need to modify in the single line batch code to open also an Excel file with a space in file name in Excel?
Open a command prompt window, run in this window for /? and read very carefully all output help pages.
Command FOR with option /F splits up a string by default on spaces/tabs. It can be either used "tokens=*" or "delims=" to avoid this string splitting. The usage of "delims=*" works also for file names because the name of a file without or with path can't contain an asterisk. But "delims=*" is usually not used to prevent splitting up a string into tokens because a string read from a text file, output of an application or an environment variable could contain 1 or more asterisks as well.
Next run in a command prompt window cmd /? and read at least last output help page on which is explained on which characters in name of a file/folder without or with path the file/folder name string must be enclosed in double quotes. In general it is advisable to enclose file/folder names without/with path always in double quotes.
The command DIR returns with the used options just the names of the files without path and always without surrounding double quotes as it can be seen on running in a command prompt window in directory with Book 1-* files
dir "Book 1-*" /A-D /B /O-D /TW
after first running dir /? to get displayed the help for command DIR.
So used should be:
for /F "delims=" %%# in ('dir "Book 1-*" /A-D /B /O-D /TW 2^>nul') do ( start "" excel.exe "%%#" & exit /B )
2^>nul is 2>nul whereby the redirection operator > is escaped with ^ to apply this redirection on running command DIR instead of being interpreted as redirection of command FOR at an invalid position in command line. The command DIR outputs the error message File not found to handle STDERR if it can't find any file in current directory matching the pattern Book 1-*. This error message is suppressed by redirecting it to device NUL.
It is advisable to specify the application to start with file extension if well known even if the path is not known. Read answer on Where is “START” searching for executables? for an explanation.
In the batch command line above there are after command START also two double quotes before the name of the executable to start. Usage of "" is highly recommended as command START interprets often first double quoted string in arguments list as optional title string. By specifying explicitly an in this case empty title string helps to avoid unexpected application execution. For details on command START run in command prompt window start /?.
And last it is better to use command EXIT with parameter /B to exit just processing of current batch file and not exit entire command process. If this batch file is called with command CALL from another batch file or started from within a command prompt window and command EXIT is used without parameter /B, the processing would not continue on parent batch file respectively command prompt window would be closed by EXIT, too. There is no difference between usage of exit and exit /B if this batch file is executed by a double click on the batch file. For details on command EXIT run in a command prompt window exit /?.
By the way: The command START uses file association registration data if it can find an executable or script with given name to open the specified file with the registered application for the file extension.
So it would be also possible to use:
for /F "delims=" %%# in ('dir "Book 1-*" /A-D /B /O-D /TW 2^>nul') do ( start "" "%%#" & exit /B )
Now command START opens the *.xslx file with whatever application is associated with this file extension as default application for opening it.
Add double quotes around the last variable.
for /f "delims=*" %%# in ('dir /tw /o-d /b "Book 1-*"') do (start excel "%%#"& exit)

Batch Show a text or a line under a set

This is something that i searched for very long and never even found another that asked the same !
Is it possible to have a variable input with text under it !
Because, in the code, the text beneath is shown only on ending of the input.
Basicly, i'd like to make a thing like this :
######################
INPUT (the set /p cmd)
######################
The thing i want is to make the line under the set command visible
No!
It's not possible with pure batch.
There is a simple reason,
as there is no way to go up with the cursor, it's only possible to go back in the same line (with the CR or BACKSPACE characters).
Only CLS can bring the cursor up, but then the screen is empty again.
I know two commands with the possibility to go lines up, but I don't know a way to use them from a batch file.
set and cmd in a double threaded command window.
#echo off
if "%~1"=="intern" (
prompt %2$G
call %2
exit
)
start /b "" "%~f0" intern :thread2
:thread1
prompt :thread1$G
call :cls
echo Use ESC to go up lines
for /L %%n in (1 1 10) DO (
ping localhost -n 2 > nul
echo( %%n
)
exit /b
:thread2
set /p var=Press ESC ... NOW!
exit /b
But there exists many external tools to set the cursor position (CursorPos.exe from Aacini)

Resources