Visual Basic string syntax error while trying to automate WinSCP - vba

I wish to use thing string useing Shell() function to automate an upload of an excel file to an ftp server whenever a save occurs:
strShell = "C:\Program Files\WinSCP\WinSCP.exe /console /command " & Chr(34) & "option batch on" & Chr(34) & " " & Chr(34) & "open user:pass#localhost" & Chr(34) & " " & Chr(34) "put " ThisWorkbook.Path & "/" & ThisWorkbook.Name & "/home/User/Directory" & Chr(34) & " " & Chr(34) "exit" & Chr(34)
For some reason I get syntax error for this string. My knowledge in VBA isn't wide but I going over MSDNfor the last couple of hours didn't seem to help at all.

I suspect that you need more quotes for file paths containing spaces. Something like
strShell = "'C:\Program Files\WinSCP\WinSCP.exe' /console /command " & Chr(34) &....
you also forgot some "&" like on the 3dr line:
Chr(34) & "put " & ThisWorkbook.Path &
Again on that line, you might need to surround filenames with quotes !
Your best help would be to include a Debug.Print strShell and watch the result in the VBE Immediate Window (^G)

According to the documentation for WinSCP, you need to add the double quotes around your commands. For Example:
WinSCP.exe /command "option batch abort"
So in VBA that would be: "WinSCP.exe /command ""option batch abort"""

Related

Start CMD Window Minimized?

I use the following code to start a process and then wait for it to exit. It works, but the CMD window is full size. I'd rather have it start at least minimized or possibly hidden. Thanks in advance!
Edit: If I am doing this in a method does not allow me to change the method in which to specify either hidden or minimized, how do I go about this?
mProcessParameters = New StringBuilder
mProcessParameters.Append(" -input:" & Chr(34) & "\\server\waybill\Waybill files\")
mProcessParameters.Append(txt_Year.Text & " Data\Interim Data Processing\")
mProcessParameters.Append(mBPGM_FileNames(mLooper2) & mInFileNameSuffix & Chr(34) & " ")
mProcessParameters.Append(" -config:" & Chr(34) & "\\server\waybill\Waybill files\")
mProcessParameters.Append(txt_Year.Text & " Data\Interim Data Processing\")
mProcessParameters.Append(mBPGM_FileTypes(mLooper2) & mCfgFileNameSuffix & Chr(34))
mProcess.Start(My.Settings.BatchProg, mProcessParameters.ToString).WaitForExit()

Run a Powershell command (not script) from Excel VBA

I've searched SO, and I can find plenty of examples of running a PowerShell script from VBA, but I can't find any examples of just running a simple command.
For example, this works:
Dim retval As Variant
retval = Shell("PowerShell ""C:\MyScript.ps1""", vbNormalFocus)
But this does not:
Dim retval As Variant
Dim pscmd As String
pscmd = "PowerShell " & _
Chr(34) & "Get-ScheduledTask" & _
" -TaskName " & Chr(34) & "My Task" & Chr(34) & _
" -CimSession MYLAPTOP" & Chr(34)
retval = Shell(pscmd, vbNormalFocus)
Debug.Print pscmd
'pscmd = PowerShell "Get-ScheduledTask -TaskName "My Task" -CimSession MYLAPTOP"
I know I could write the PS command to a file, execute it as a script, and then delete the file, but that does not seem to be very elegant.
To run an inline Powershell command, you'll probably need to use the -Command param and surround your statement with quotes. It'll also be easier if you use single quotes within the command.
Try this out:
pscmd = "PowerShell -Command ""{Get-ScheduledTask -TaskName 'My Task' -CimSession MYLAPTOP}"""
Haven't tested but this but the execution policy may be a factor.
pscmd = "PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command {" & _
Chr(34) & "Get-ScheduledTask" & _
" -TaskName " & Chr(34) & "My Task" & Chr(34) & _
" -CimSession MYLAPTOP" & Chr(34) & "}"

FilemakerPro 12 running an applescript to convert a PDF to a Jpeg. Expected End of line etc. but found identifier

We have an internal system built in Filemaker Pro 12. We have a script that will combine two files together to make a PDF. Then our system calls up the below applescript to convert that PDF into a Jpeg. We get the error: Expected end of line etc. but found identifier.
I have found several posts, but I am not able to get an answer from those posts that solve my issue. FYI... The ftp site below was changed on purpose, it is correct in our system. Any help would be greatly appreciated, let me know if you have any questions.
Unstored, from SnDesign to Same Designs, =
"set theFile to alias \"" & Substitute(Right(Get(DesktopPath);Length(Get
(DesktopPath))-1);"/";":") & "PDFTemplates:" & TemplateNamePDF & "\" "&
¶ &
"tell application \"Adobe Photoshop CS4\" "& ¶ &
"activate" & "¶" &
"open theFile as PDF with options {resolution:150, use antialias:
true}" & ¶ &
"set docRef to the current document" & ¶ &
"tell docRef" & ¶ &
"resize image width pixels 1298 height pixels 1696
resolution 150 resample method bicubic" & ¶ &
"save in \"" & Substitute(Right(Get(DesktopPath);Length
(Get(DesktopPath))-1);"/";":") & "PDFTemplates:" & TemplateNamePDF &
"\"" & " as JPEG with options {quality:6} appending lowercase extension with
copying" & ¶ & "end tell" & ¶ &
"close current document without saving" & ¶ &
"end tell" & ¶ &
"set theFile to alias \"" & Substitute(Right(Get(DesktopPath);Length(Get
(DesktopPath))-1);"/";":") & "PDFTemplates:" & TemplateNameJPG & "\" " &
¶ &
"tell application \"Finder\" " & ¶ &
"duplicate file theFile to \"RETAIL:" & Category & "\"" & " with
replacing" & ¶ &
"duplicate file theFile to \"Mac Volume:RETAIL CURRENT:
_JPEGS2File\" with replacing" & ¶ &
"end tell" & ¶ &
"tell current application" & ¶ &
"do shell script \"usr/bin/curl -T " & Right(Get(DesktopPath);Length
(Get(DesktopPath))-13) & "PDFTemplates/" & Substitute
(TemplateNameJPG;["(";"'('"] ;[")";"')'"]) & " ftp: //Domain Name/\" " & ¶ &
"end tell" & ¶ &
"tell application \"Finder\" " & ¶ &
"delete file theFile" & ¶ &
"end tell" & ¶ &
"set theFile to alias \"" & Substitute(Right(Get(DesktopPath);Length(Get
(DesktopPath))-1);"/";":") & "PDFTemplates:" & TemplateNamePDF & "\" "&
¶ &
"tell application \"Finder\" " & ¶ &
"duplicate file theFile to \"" & Substitute
(ApplescriptArtFolderPDFPathCalc;"/";":") & "\" " & " with replacing" & ¶ &
"end tell" & ¶ &
"tell application \"Filemaker Pro\"" & ¶ &
"activate" & ¶ &
"end tell"
Syntax errors in AppleScript are difficult to debug from FileMaker. Evaluate the calculation (use Data Viewer in FMA or make your own in FMP) - this will give just the AppleScript code. Paste it into AppleScript Editor (in Utilities). It should tell you exactly where the error is.

My automatic phonegap installer needs jdk download with wget, I have it working in command prompt but not in .hta file

I have written a vbscript based .hta installer to install PhoneGap/Cordova automatically with all the pre-requisites(ant/jdk/bada sdk etc). Its almost done but I am stuck at the part where I need to download JDK directly.
If you copy paste this (long) line in commandprompt and it works
wget --header "Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html;" ""http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe -O jdk.exe
It will download JDK directly , absolutely fine .
When you paste the code snippet in notepad e.g and save as .vbs and run it then it doesnt work
Set objShell = CreateObject("WScript.Shell")
objShell.CurrentDirectory = "c:\"
'inQuotes function just puts quotes around the command parameters
objShell.Run inQuotes(WorkingDir & "\wget.exe") & " " & " --header " & inQuotes("Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html; ") & "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe" & " -O" & " " & inQuotes("jdk.exe"),1,True
Function inQuotes(toQuote)
'return with quotes around the toQuote parameter
inQuotes = chr(34) & toQuote & chr(34)
End Function
Somewhere in teh objShell.Run line there is a problem and wget isn't downloading the jdk ...
Will really appreciate any help
thank you :)
Is your variable WorkingDir is defined? If I put one echo:
strCmd = inQuotes(WorkingDir & "\wget.exe") & " " & " --header " _
& inQuotes("Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html; ") _
& "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe" & " -O" & " " & inQuotes("jdk.exe")
WScript.Echo strCmd
I get this:
"\wget.exe" --header "Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html; "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe -O "jdk.exe"
And this not pass to the original command. Maybe it s'd be like this?
strCmd = "wget.exe --header " _
& inQuotes("Cookie: gpw_e24=http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html;") _
& " " & chr(34) &chr(34) & "http://download.oracle.com/otn-pub/java/jdk/7u17-b02/jdk-7u17-windows-i586.exe" _
& " -O jdk.exe"
WScript.Echo strCmd

Write multiple lines with quotes in a text file vb.net

Hi Is there any easier way to write multiple lines in a file which has things like quotes in and other things like that or is the only way to do it like this
Dim objwriter As New System.IO.StreamWriter(AppsDir & "EthIPChanger.bat")
objwriter.WriteLine("#echo off")
objwriter.WriteLine("netsh interface ip set address name=""" & "Local Area Connection""" & " static " & TB_EthIPAddress.Text & " " & TB_EthSubnetMask.Text & " " & TB_EthDefaultGateway.Text & " 1")
objwriter.WriteLine("netsh interface ip set dns """ & "Local Area Connection""" & " static " & TB_EthDNS1.Text)
objwriter.WriteLine("ipconfig /all > """ & AppsDir & "NetworkInfo.txt""")
objwriter.WriteLine("echo hi > """ & AppsDir & "CheckLen.txt""")
objwriter.Close()
I know if you are using python you can do """ and then do anything inside that and end it with """
Does anything like that exist in vb.net?
thanks
If you use objwriter.Write - then you can supply the vbcrlf yourself - and then you can put multiple 'lines' in one write statement.
For example:
Dim str2write As string
str2write = "firstline" and Chr(34) & Chr(34) & vbcrlf
str2write &= Chr(34) & "second line" and Chr(34) & vbcrlf & vbcrlf
objwriter.write(str2write)
objwriter.close()
You can try with StringBuilder:
Dim objwriter As New System.IO.StreamWriter(AppsDir & "EthIPChanger.bat")
Dim textToWrite As New System.Text.StringBuilder
With textToWrite
.Append("#echo off")
.AppendFormat("netsh interface ip set address name={0}Local Area Connection{0} static {1} {2} {3} 1", Chr(34), TB_EthIPAddress.Text, TB_EthSubnetMask.Text, TB_EthDefaultGateway.Text)
.AppendFormat("netsh interface ip set dns {0}Local Area Connection{0} static {1}", Chr(34), TB_EthDNS1.Text)
.AppendFormat("ipconfig /all > {0}{1}{2}{0}", Chr(34), AppsDir, TB_EthDNS1.Text)
.AppendFormat("echo hi > {0}{1}{2}{0}", Chr(34), AppsDir, CheckLen.Text)
End With
objwriter.WriteLine(textToWrite.ToString)
objwriter.Close()