VBA to run shell command but to run from different directory - excel

I have code as follows:
ChDir (ActiveWorkbook.Path)
ShellString = "cmd.exe /k cpdf -split " + Chr(34) + ".\" + Replace(File, ".csv", ".pdf") + Chr(34) + " -o temp/x_%%%.pdf"
Shell ShellString, vbNormalFocus
When I run the code, it does nothing, because it can't find cpdf.exe.
Cpdf.exe exists in the same path as my Active Workbook.
The ChDir command did not do the trick.
When I run the code, I get
'cpdf' is not recognized as an internal command....
And I am left at the following prompt:
C:\Users\ksmith\Documents
This tells me that the command was trying to run from that folder, and that is why it failed.
How do I run Shell from Desired folder in VBA?
ChDir doesn't seem to do the trick, as some people had suggested...

The ChDir command only changes the "current" directory on the drive specified, but does not affect which drive is currently "current".
So, if
your computer has a "C" drive on which the "current" directory is "C:\Users\ksmith\Documents", and
you also have an "L" drive on which the "current" directory is "L:\abc\def",
and
the directory containing your workbook is "L:\Temp1\Temp2"
then executing
ChDir ActiveWorkbook.Path
will change the "current" directory of the "L" drive to be "L:\Temp1\Temp2", but the "current" drive will continue to be "C" and the C drive's "current" directory will continue to be "C:\Users\ksmith\Documents".
You therefore need to also change the "current" drive. Providing you have a mapped drive (i.e. it won't work with a UNC specified drive) you can do this by using:
ChDrive ActiveWorkbook.Path
ChDir ActiveWorkbook.Path

Related

Calling a PowerShell script from VBA in a synced location (SharePoint)

I have a working VBA script that calls a Powershell script as follows:
Call Shell("powershell -executionpolicy bypass & """ & ThisWorkbook.Path & "\FTP\FTP.ps1""")
The workbook path is H:\ABC\TSV\Forecast\Worksheets
If I move the workbook and the script to C:\Test it executes the script.
However, if I move the workbook and script to a synced location (SharePoint), it doesn't execute the script. Is the synced location's file path is too long? The file path is 72 characters (e.g. C:\Users\MyUsername\SharePointSite\SharePoint-Library\Forecast\Worksheets).
How do I get around this?
Here's a workaround:
ChDir ThisWorkbook.Path
Call Shell(ThisWorkbook.Path & "\TryShell.bat " & ThisWorkbook.Path)
TryShell.bat
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& './FTP/FTP.ps1'"

Error on zipping folders in applescript

I'm trying to develop an automated script to zip up folders but I keep getting the following error:
error "Finder got an error:
zip error: Nothing to do! (/Users/dam/Desktop/Design/_TEST/basefolder/Fonts.zip)" number 12
Been scouring the forums and not quite hitting the mark. Any help for a noob would be appreciated. Thanks!
tell application "Finder"
set projFonts to (choose folder with prompt "Please select font folder.")
set fontPath to quoted form of POSIX path of projFonts
set zipName to the name of projFonts
set theFolder to POSIX path of (container of projFonts as alias)
set zipFile to the quoted form of (theFolder & zipName)
do shell script "zip -j " & zipFile & " " & fontPath
end tell
I think this is what you want, although the question lacks details.
set theItem to (choose folder)
tell application "Finder" to set {fileName, theFolder} to {theItem's name, (theItem's container as text)'s POSIX path}
do shell script "zip -rj " & (theFolder & fileName & ".zip")'s quoted form & space & theItem's POSIX path's quoted form

Using VBA to run WinSCP script

I am able to download files from SFTP in CMD window, by using following code:
WinSCP.com
# Connect to the host and login using password
open user:pw#address
# get all the files in the remote directory and download them to a specific local directory
lcd C:\Users\xx\Desktop
get *.xlsx
# Close and terminate the session
exit
I searched online and found out that I can put these codes in a bat file and use
Call Shell("cmd.exe /c C:\Users\xx\Desktop\WinSCPGet.bat", 1)
However, only the first line of the bat file WinSCP.com is being executed. It will pop up the cmd window, showing this, without doing anything else.
How to execute all the lines at one time?
Thanks
The code you have is not a Windows batch file. It's one Windows command followed by WinSCP commands. The first command runs winscp.com application, which then sits and waits for input. If you eventually close it, Windows command interpreter (cmd.exe) will carry on executing the remaining commands, failing most, as they are not Windows commands. See also WinSCP script not executing in batch file and WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?
So you either have to save the commands (open to exit) to a WinSCP script file (say script.txt) and execute the script using the /script switch:
Call Shell("C:\path\winscp.com /ini=nul /script=c:\path\script.txt")
Alternatively, specify all commands on WinSCP command line, using the /command switch:
Call Shell("C:\path\winscp.com /ini=nul /command ""open user:pw#address"" ""lcd C:\Users\xx\Desktop"" ""get *.xlsx"" ""exit""")
Regarding the quotes: With the /command switch, you have to enclose each command to double-quotes. In VBA string, to use a double-quote, you have to escape it by doubling it.
Also note that you generally should use the /ini=nul switch to isolate the WinSCP script run from your WinSCP configuration. This way you can also make sure that the script will run on other machines. Your script won't, as it lacks the -hostkey switch to verify the SSH host key fingerprint. Using the /ini=nul will help you realize that.
You can have WinSCP GUI generate complete command-line (including the -hostkey) for you.
See also Automating file transfers to SFTP server with WinSCP.
I like this small and compact procedure, and use it in my own projects. No temp-files required. Fast and reliable.
Parse a string src (an absolute filepath) to uploadImageByFTP. Etc. C:\Users\user\Desktop\image.jpg, and the file will be uploaded.
Replace:
<username> with FTP-User
<password> with FTP-Password
<hostname> with FTP-hostname (etc. example.com)
<WinSCP.com path> with path on your WinSCP-client (etc. C:\Program Files (x86)\WinSCP\WinSCP.com. Caution: WinSCP.com and not WinSCP.exe)
<FTP-path> with path on your FTP-client (etc. /httpdocs/wp-content/uploads)
Sub uploadImageByFTP(src As String)
Dim script As Object: Set script = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
'Not empty
If (src <> vbNullString) Then
'Execute script
script.Run _
"""<WinSCP.com path>"" " + _
"/ini=nul " + _
"/command " + _
"""open ftp://<username>:<password>#<hostname>/"" " + _
"""cd <FTP-path>"" " + _
"""put " & """""" & src & """""" & """ " + _
"""close"" " + _
"""exit""", windowStyle, waitOnReturn
End If
End Sub
WScript.Shell is more powerful than the default Shell(), as you can append a waitOnReturn-command; this tells VBA, that further execution isn't allowed before the file(s) have been uploaded to the FTP-server.
Change windowStyle to 0, if you don't like the command prompt to open on each execution.

Shell command on Excel with long path name don't work

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)

use winrar command line to create zip archives

I'm using the following winrar command line to create zip archives:
rar.exe a -df -ep -ag[yyyyMMddhhmmss] -ms[txt] C:\MyZipFile.zip C:\tmp\MyFiles*.txt
The archives created are in RAR format instead of ZIP. Is there a way to create regular ZIP and not RAR archives?
Make certain you are using WinRAR.exe and not Rar.exe.
If you are using the command line to do this make sure you type:
winrar a -afzip c:\test.zip c:\test.csv
not:
a -afzip c:\test.zip c:\test.csv
It works for me. I also got it to work in SSIS.
WinRAR has a detailed description of its command line syntax in its help files (WinRAR Help), chapter "Command line syntax".
All the commands such as "a" (add to an archive), "d" (delete from an archive), "e" (extract from an archive ignoring paths) and switches such as "-af" (specify whether to create a rar or a zip file), "-ad" (append archive name to destination path) or "-p" (encrypt the archive using password protection) are listed there.
There are quite a lot of options. I recommend reading the command line syntax rules when working with WinRAR via command lines.
In order to trigger WinRAR zip-packaging from within a MS Access database application, I use in the VBA code for example
Shell c:\Programme\WinRAR\winrar.exe a -afzip -p <AnyPasswordYouLike> "e:\MyStuff\TargetFolder\Output.zip" "e:\MyStuff\SourceFolder\Input.docx"
Of course, the file paths and names are ususally entered via variables, e.g. like
Dim strWinrarCommandline As String
'... and the other variables as well declared in advance, of course...
strWinrarCommandline = strWinrarPathAndSwitches & "-p" & strPassword & " " & Chr(34) & strOutputFullName & Chr(34) & " " & Chr(34) & strInputFullName & Chr(34)
'And then call Winrar simply by:
Shell strWinrarCommandline
So rar.exe is currently unable to create zip files by itself only by calling in the Windows version it is possible.

Resources