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.
Related
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
I want to use Python to open a directory in a Win10 cmd window, and keep the window open.
I made a batch file named: open_dir_in_cmd_window.CMD:
CD /D %1
I tested that batch file successfully, by creating another batch file named, Test.cmd:
Rem "open_dir_in_cmd_window.CMD" "f:\backup"
"open_dir_in_cmd_window.CMD" "f:\backup"
A very helpful webpage provides the following example, which I seem unable to follow correctly:
Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
I made a python script, which contains the following lines, which alas, triggers an error message:
import subprocess
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
When I open a Command Prompt window and run:
"C:\ProgramData\Anaconda3\python.exe" E:\open_dir.py
I get an error message, SyntaxError: invalid syntax, with this:
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
^
I've tried many different permutations of double quoting and can't figure out the right way to do it.
I have spent many hours hunting on the web and trying to figure this out and I do not know what to do.
Any suggestions would be appreciated.
I am running into a problem automating an installation procedure. I want to install a program, and then patch it with a custom UserPatch to remove some glitches.
The UserPatch executable must be run from the installation directory, so I must retrieve this address from the installer. Then, after the program is installed, the script must copy the UserPatch file to the installation directory, and run it from there.
I retrieve the pathname as shown below. The script copies the raw text from the window, which I strip down to just the line containing the pathname.
WinActivate("Dir browse box title")
$InstallDir = WinGetText("Dir browse box title")
$split = StringSplit( $InstallDir, "&Path", 1)
$InstallDir = $split[2]
$split = StringSplit( $InstallDir, "&OK", 1)
$InstallDir = $split[1]
ControlClick("Dir browse box title", "&Drives", "[Class:CButtonClassName; INSTANCE:2]")
Sleep(100)
When printing "$InstallDir" after the above procedure, it shows exactly what I want it to be: "C:\Program Files (x86)\Path to\Installation folder" (without '"').
The problem is this: It doesn't copy the file when I run the code snippet below. It does work when I hardcode the pathname (commented in the code below), but not when using the $InstallDir variable as retrieved by the code snippet above.
; Install the UserPatch
; $InstallDir = "C:\Program Files (x86)\Path to\Installation folder"
$UserPatchName = "SetupAoC.exe"
FileCopy($UserPatchName, $InstallDir & "\" & $UserPatchName)
Run($InstallDir & "\" & $UserPatchName)
WinWait("SetupAoC - Feature Update Tool")
ControlClick("SetupAoC - Feature Update Tool", "Install", "[Class:DirectUIHWND; INSTANCE:1]")
Send("{SPACE}")
Sleep(2000)
WinClose ("SetupAoC - Install Complete")
WinClose ("SetupAoC - Feature Update Tool")
I've got the feeling that I'm missing something obvious, but I really can't tell what the problem is? Any ideas?
Thanks in advance!
EDIT in reply to comments:
After
$InstallDir = WinGetText("Dir browse box title")
the value of $InstallDir is
&Drives
C:\ Crucial M500
&Folders
&Path
C:\Program Files (x86)\Path to\Installation folder
&OK
&Cancel
I then use StringSplit, which creates an array of strings, separated by the specified delimiter.
OK, problem eliminated.
The problem was that my "C:\path...." string was preceded and trailed by two #CRLF characters (new lines). These were also present in the raw data that I copied from the window, as I didn't include them in my delimiters.
I didn't notice them when printing the output, as they're white-space. I found out when writing the $InstallDir variable to a file, in a desperate attempt.
Anyway, Solved with
$InstallDir = StringReplace($InstallDir, #CRLF, "")
Thanks for the tips, #Matrix and #McBarby!
I'm trying to run a program with an input file.
Dim command, cfx5_exe_path, cfx_file_folder, cfx_file_name As String
command = cfx5_exe_path & " -cfx " & cfx_file_folder & cfx_file_name & ".cfx "
Shell command
so it gives an error.
the resulting value of the command in the debugger is
"c:\"Program Files"\"ANSYS Inc"\v150\CFX\bin\cfx5pre.exe -cfx c:\Users\Username\Arbeit\Programming\A321_tail_flow.cfx"
If I copy-paste that into windows cmd directly and remove first/last quotation signs, then it works perfectly.
What is the problem with Shell?
The documentation says:
If the Shell function successfully executes the named file, it returns the task ID of the started program. The task ID is a unique number that identifies the running program. If the Shell function can't start the named program, an error occurs.
and it gives a small example:
Sub test()
Dim RetVal
Dim command As String
command = "C:\WINDOWS\CALC.EXE"
RetVal = Shell(command, 1)
End Sub
Here I get Error 53: file not found as calc on Windows 7 resides somewhere else. Do you get this error?
Providing the right path to calc starts the program and returns a unique ID.
However, quoting a part of the correct path will throw the error:
command = "C:\WINDOWS\""SYSTEM32""\CALC.EXE"
but quoting the full path does not:
command = """C:\WINDOWS\SYSTEM32\CALC.EXE"""
So you must remove all embedded quotes and then quote the full path once.
I have a batch file run.bat in a network folder "L:\Common Data\myfile" and i want to execute it from an Excel's macro.
Googling around I found these sintax:
Call Shell(Environ$("COMSPEC") & " /k L:\Common Data\myfile\run.bat", vbNormalFocus)
but it fails because it reads only "L:\Common".
I tryed many suggestion found on Internet but no one succeeded.
Someone have a solution?
Path names with spaces have to be wrapped in quotes.
Call Shell(Environ$("COMSPEC") & " /k ""L:\Common Data\myfile\run.bat""", vbNormalFocus)