Call shell with spaces in file path - shell

The code below unzips a .zip file using winzip. However, if the FolderPath contains a blank, I get non-trappable error "Failed to open document.", without an error number. How can I unzip this file with Shell if the FolderPath contains a blank? Thank you,
FolderPath = ThisWorkbook.Path & "\"
Unzipfile = Dir(FolderPath & "*.zip")
While UnzipFile <> ""
if InStr(1, UnzipFile, ".zip") > 0 Then
ShellStr = "C:\Program Files\WinZip\winzip32 e- " & FolderPath & UnzipFile & " " & FolderPath
Call Shell(ShellStr, vbHide)

You need to quote paths with spaces
ShellStr = """C:\Program Files\WinZip\winzip32.exe"" e- """ & _
FolderPath & UnzipFile & """ """ & FolderPath & """"

Related

vbscript output to text on wndows startup

I'm looking for vbscript that do the following tasks
Script Tasks
execute on startup of the computer,
the way is being executed is via putting it in startup folder of windows in
C:\Documents and Settings\Admin\Start Menu\Programs\Startup
if output text file is exist, write down some text and exit
if the text file is not exist then it echo out full destination
the code is as provided, any help would be appreciated
'create txt.vbs
'vbscript
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = objFSO.GetAbsolutePathName(".")
FilePath = CurrentDirectory & "\test.txt"
Existcode = objFSO.FileExists(FilePath)
' wscript.echo "FileExists code:" & Existcode
if Existcode = False then
Existcode = objFSO.FileExists(FilePath)
'for debugging
wscript.echo "file not exist" & vbCrLf _
& "FileExists code:" & Existcode
Set objFile = objFSO.CreateTextFile(FilePath,True)
strtext = "file created:" & vbCrLf & chr(34) & "New Line" & chr(34)
objFile.Write strtext & vbCrLf
objFile.Close
else
'for debugging
wscript.echo "file exist" & vbCrLf _
& "FileExists code:" & Existcode & vbCrLf & vbCrLf _
& FilePath & vbCrLf _
& CurrentDirectory & vbCrLf
end if
wscript.echo "end"
when get executed by clicking on it, either via a batch file, the script works with no errors and the output is as expected
while it's executed from startup folder by
windows, it show all echo that i set for debugging but doesn't
create the output file neither write to text in it, but also it read it as exist i'm not sure why
the vbscript cannot write text to startup folder,
however changes are the following
CurrentDirectory = objFSO.GetAbsolutePathName(".")
FilePath = CurrentDirectory & "\test.txt"
become
DesktopDirectory = WshShell.SpecialFolders("Desktop")
FilePath = DesktopDirectory & "\test.txt"

Run Rscript.exe with VBScript and spaces in path

I have the followaing run.vbs script
Rexe = "R-Portable\App\R-Portable\bin\Rscript.exe"
Ropts = "--no-save --no-environ --no-init-file --no-restore --no-Rconsole "
RScriptFile = "runShinyApp.R"
Outfile = "ShinyApp.log"
startChrome = "GoogleChromePortable\App\Chrome-bin\chrome.exe --app=http://127.0.0.1:9999"
strCommand = Rexe & " " & Ropts & " " & RScriptFile & " 1> " & Outfile & " 2>&1"
intWindowStyle = 0 ' Hide the window and activate another window.'
bWaitOnReturn = False ' continue running script after launching R '
' the following is a Sub call, so no parentheses around arguments'
CreateObject("Wscript.Shell").Run strCommand, intWindowStyle, bWaitOnReturn
WScript.Sleep 1000
CreateObject("Wscript.Shell").Run startChrome, intWindowStyle, bWaitOnReturn
It works pretty well in most cases except when the user puts the run.vbs script in a folder with spaces in its name: e.g. if run.vbs is in folder "foo bar", the user gets the error : "C:\Users\[user name]\Desktop\foo" not recognized as internal command...
I don't understand why Rscript.exe looks for the absolute path before running even if it's called from its parent directory using relative path.
I heard about the double quote solution using the absolute path but it doesn't seem to work with .exe scripts (it does though with .bat and .cmd)
Thanks for any help!
Below code will help you
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
'run command'
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec("C:\Program Files\R\R-3.2.3\bin\Rscript.exe C:\subfolder\YourScript.R " & """" & var1 & """")
Set oOutput = oExec.StdOut
handle the results as they are written to and read from the StdOut object
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
sLine = oOutput.ReadLine
If sLine <> "" Then s = s & sLine & vbCrLf
Wend

Escaping spaces in folder path

I have a backup script the copies from one server to another via scheduled task. Most of the folders copy ok. However, there is one folder that has a space in the name and it blows the whole thing up.
This runs on the destination server (pulls data in).
I've tried various escape patterns, and they all fail.
(vars are dimmed, code truncated)
sArchiveFolder = "D:\Backup\" & year(now) & "-" & month(now) & "-" & day(now) & "\"
sDataFolder = "\\Server\Share\System Library"
sDestFolder = sArchiveFolder & "System Library\"
Call subCopyFolder(fso, objShell, sDataFolder, sDestFolder)
sub subCopyFolder(fso, objShell, sDataFolder, sArchiveFolder)
dim iCounter, excludedDirs
if not(fso.folderexists(sArchiveFolder)) then
fso.createfolder(sArchiveFolder)
excludedDirs = " /XD Logs"
if(right(sDataFolder,7)="Library") then
'this fails
'sDataFolder = """"&sDataFolder&""""
'sArchiveFolder = """"&sArchiveFolder&""""
'so does this
'sDataFolder = chr(34)&sDataFolder&chr(34)
'sArchiveFolder = chr(34)&sArchiveFolder&chr(34)
end if
Dim sRoboCopyCommand
sRoboCopyCommand = "robocopy " & sDataFolder & " " & sArchiveFolder & " /E "& excludedDirs &" /R:5 /W:1 /log+:log.txt"
objShell.Run (sRoboCopyCommand)
end sub
How do I properly escape this? I also tried putting the literal quotes in the robocopy command line itself and that broke the folders that don't need the quotes too.
As noted in the code, I tried the "4 quotes method" and it does not work within the robocopy command line.
with 4 quotes method:
(stripped out private stuff not relevant to issue, ie full paths and other eXcludeD fodlers)
For posteriority: apparently trailing backslashes in source or destination path mess up robocopy's parameter handling, so the paths need to be specified without them:
sArchiveFolder = "D:\Backup\" & year(now) & "-" & month(now) & "-" & day(now)
sDataFolder = "\\Server\Share\System Library"
sDestFolder = sArchiveFolder & "\System Library"
...
sRoboCopyCommand = "robocopy """ & sDataFolder & """ """ & sArchiveFolder & _
""" /E " & excludedDirs & " /R:5 /W:1 /log+:log.txt"
objShell.Run sRoboCopyCommand

invalid Directory name used in property "CurrentDirectory" vbscript

I'm trying to compile au3 script through vbscript which located in another directory, so I used "CurrentDirectory" property to change the working directory from script directory to au3 file directory using this code
drivepath = "K"
strTempTarget = "New Folder"
filename = "gate.jpg"
IconName = "102.ico"
Comm = "cmd /c " & "Aut2Exe.exe /in " & filename & ".au3" & " /out " & filename & ".exe" & " /icon " & IconName
Path = """" & drivepath & "\" & strTempTarget & """"
MsgBox(Path)
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = Path
objShell.Run(Comm), 0, True
But I got an error "invalid file name or directory name"
since there is an empty space in "strTempTarget" value I should put a double quotes at the start and at the end of directory name "Path" I even tried to use ASCII : something like
Path = Chr(34) & drivepath & "\" & strTempTarget & Chr(34)
but the same error keep raising
So how to make this script work fine ?
The shell needs quoted pathes, because it uses space as separator; .CurrentDirectory 'knows' the whole string (included any spaces) is meant to be a folder path; so don't quote the string for .CurrentDirectory.
Evidence:
>> Set objShell = WScript.CreateObject("WScript.Shell")
>> Path = """C:\Documents and Settings"""
>> objShell.CurrentDirectory = Path
>>
Error Number: -2147024773
Error Description:
>> Path = "C:\Documents and Settings"
>> objShell.CurrentDirectory = Path
>>
>> <-- no news are good news

VBscript to zip log files with 7zip

Would like someone to take a look at my script and tell me where I am messing up.
It is a script to zip log files and then I would like to move them into a new folder that is going to be shared over a network. Right now I am just trying to get the part where it zips up the files using 7zip correctly.
I am very new to VB (like 2 days) so having some syntax problems I think.
Script is found below, thank you in advance for all advice and help
Option Explicit
WScript.Echo "Press to start zipping log files."
Dim objFile, objPath, objFolder, Command, PathLogs, RetVal
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell = CreateObject("WScript.Shell")
PathLogs = "C:\Testscripts\testfolder\" 'This path just has some test logs
' Loop through the logs and zip and move each file (if required, you could just move files with an '.log' extension)
Set objPath = objFSO.GetFolder(PathLogs)
For Each objFile In objPath.Files
If (LCase(objfso.GetExtensionName(objFile)) = "log") Then
Wscript.Echo objFile.Name
' zip and move files
'Command = """C:\Program Files\7-zip\7z.exe"" -m -ex """ & PathLogs & \objFile.Name objfso.GetBaseName(objFile) & "*.zip"" """ & PathLogs & objFile.Name & """"
Command = ""C:\Program Files\7-zip\7z.exe"" a -m -ex " & PathLogs & "" & objFile.Name & ".zip " & PathLogs & "" & objFile.Name & "
WScript.Echo "Command: " & Command
RetVal = objShell.Run(Command,0,true)
End If
Next
WScript.Echo "Zip Successful."
You have your quotes wrong. To use a quote inside a string, you have to duplicate the quote.
Command = """C:\Program Files\7-zip\7z.exe"" a -m -ex " _ 'this is the first part of the string
& PathLogs & objFile.Name & ".zip " & PathLogs & objFile.Name
If your Logfile or PathLogs can contain spaces they must be quoted as well.

Resources