Excel VBA - Passing a variable path with spaces to WinSCP command-line - vba

I have the code at the bottom of this post inside one of my excel books (my first time ever writing vba code). The goal here is to allow users to:
start a video encode using MXLight software with a temp file name
select a cell with the person currently on video
stop the video encode, rename the temp file, move it to a specific folder,
upload it via FTP via WinSCP software, mark it green, move one cell
down.
So during the event, you:
Press button 1 which is the Sub StartMXL
then you highlight your cell
Press button 2 which is the Sub StopAndProcess
My questions are the following:
1) First and foremost, the entire (stop and process) button doesn't work because the upload function fails, because I can't figure out how to get the winscp command to use the variable referenced... and not try to literally use that word. Check the code under the Sub Upload, and here is the log file when I try that:
1 . 2015-11-12 17:53:18.490 Connected
2 . 2015-11-12 17:53:18.490 Using FTP protocol.
3 . 2015-11-12 17:53:18.490 Doing startup conversation with host.
4 > 2015-11-12 17:53:18.491 PWD
5 < 2015-11-12 17:53:18.520 257 "/" is the current directory
6 . 2015-11-12 17:53:18.520 Getting current directory name.
7 . 2015-11-12 17:53:18.520 Startup conversation with host finished.
8 < 2015-11-12 17:53:18.520 Script: Active session: [1] ftp1934501#ftp.kaltura.com
9 > 2015-11-12 17:53:18.520 Script: put RealFile
10. 2015-11-12 17:53:18.520 Copying 1 files/directories to remote directory "/"
11. 2015-11-12 17:53:18.520 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask:
12. 2015-11-12 17:53:18.520 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
13. 2015-11-12 17:53:18.520 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
14* 2015-11-12 17:53:18.520 (EOSError) System Error. Code: 2.
15* 2015-11-12 17:53:18.520 The system cannot find the file specified
You can see on line 9 it's trying to literally upload the file called "RealFile" instead of using the contents of the variable with file name and folder structure. That variable is working in other parts of the code, such as when I'm renaming and moving it.
Any idea there?
Here is the total code for the whole thing:
Public Sub StartMXL()
Dim MXLapp As String
MXLapp = "C:\1a7j42w\MXLight-2-4-0\MXLight.exe"
Shell (MXLapp & " record=on"), vbNormalNoFocus
AppActivate Application.Caption
End Sub
---
Public Sub StopMXL()
Dim MXLapp As String
MXLapp = "C:\1a7j42w\MXLight-2-4-0\MXLight.exe"
Shell (MXLapp & " record=off"), vbNormalNoFocus
AppActivate Application.Caption
End Sub
---
Sub ChooseRootDir()
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please choose a folder"
.AllowMultiSelect = False
If .Show = -1 Then Sheets("rawdata").Range("I1").Value = .SelectedItems(1)
End With
End Sub
---
Public Sub RenameAndMove()
Dim TempFile As String
Dim RealFile As String
If Len(Dir(Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value, vbDirectory)) = 0 Then
MkDir Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value
End If
If Len(Dir(Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value, vbDirectory)) = 0 Then
MkDir Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value
End If
If Len(Dir(Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value, vbDirectory)) = 0 Then
MkDir Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value
End If
TempFile = Sheets("rawdata").Range("I1").Value & "\tempfile\spiderman.TS"
RealFile = Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value & "\" & ActiveCell.Value & ".TS"
Name TempFile As RealFile
End Sub
---
Public Sub Upload()
Dim RealFile As String
Dim TempFile As String
RealFile = Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value & "\" & ActiveCell.Value & ".TS"
TempFile = "C:\1a7j42w\MXLight-2-4-0\recordings\tempfile\spiderman.TS"
Call Shell( _
"C:\1a7j42w\WinSCP\WinSCP.com /log=C:\1a7j42w\WinSCP\excel.log /command " & _
"""open ftp://ftp1934501:da7Mc4Fr#ftp.kaltura.com/"" " & _
"""put RealFile"" " & _
"""exit""")
End Sub
---
Sub StopAndProcess()
Call StopMXL
Call RenameAndMove
Call Upload
Selection.Interior.ColorIndex = 4
ActiveCell.Offset(1, 0).Select
End Sub

In WinSCP script, you want:
put "path with space"
See Command parameters with spaces.
On WinSCP command line, you have to enclose each command to a double quotes and double all double quotes in the command itself:
"put ""path with space"""
See WinSCP command-line syntax.
In VB you need to enclose the string in double quotes and double all double quotes in the string itself:
"""put """"path with space"""""" "
And to replace the path with a variable, substitute the path with space with " & RealFile & ".
This gives you:
"""put """"" & RealFile & """"""" "

Related

Ms Access Get filename with wildcards or loop

I am using MS Access Forms and I am trying to open a file but don't know how to open the file based knowing only part of the name. Example below works
Private Sub Open_Email_Click()
On Error GoTo Err_cmdExplore_Click
Dim x As Long
Dim strFileName As String
strFileName = "C:\data\office\policy num\20180926 S Sales 112.32.msg"
strApp = """C:\Program Files\Microsoft Office\Office15\Outlook.exe"""
If InStr(strFileName, " ") > 0 Then strFileName = """" & strFileName & """"
x = Shell(strApp & " /f " & strFileName)
Exit_cmdExplore_Click:
Exit Sub
Err_cmdExplore_Click:
MsgBox Err.Description
Resume Exit_cmdExplore_Click
End Sub
If I change the strFilename to being
strFileName = "C:\data\" & Me.Office & "\" & Me.nm & " " & Me.pol & "\" & "*"& " S Sales " & Me.amt & "*" & ".msg"
It includes the * rather than using it as a wildcard, the date/numbers can be anything or in another format but always eight numbers. I tried using a while loop on the numbers but I am not sure the best way of doing this sorry.
You can use the Dir function to iterate over all files that match a string pattern.
strApp = """C:\Program Files\Microsoft Office\Office15\Outlook.exe"""
Dim strFilePattern As String
strFilePattern ="C:\data\" & Me.Office & "\" & Me.nm & " " & Me.pol & "\" & "*"& " S Sales " & Me.amt & "*" & ".msg"
Dim strFileName As String
strFileName = Dir(strFilePattern)
Do While Not strFileName = vbNullString
If InStr(strFileName, " ") > 0 Then strFileName = """" & strFileName & """"
x = Shell(strApp & " /f " & strFileName)
strFileName = Dir
Loop
The first call to Dir with the pattern as a parameter will find the first file that matches the pattern supplied. All subsequent calls without the pattern will return the next file that matches the pattern.
So, lets rebuild the question a bit. Imagine that you are having the following 5 files in a given folder:
A:\peter.msg
A:\bstack.msg
A:\coverflow.msg
A:\heter.msg
A:\beter.msg
and you need to find the files, that correspond to "A:\*eter.msg" and print them.
For this, you need to use the keyword Like:
Sub TestMe()
Dim someNames As Variant
someNames = Array("A:\peter.msg", "A:\bstack.msg", _
"A:\coverflow.msg", "A:\heter.msg", "A:\beter.msg")
Dim cnt As Long
For cnt = LBound(someNames) To UBound(someNames)
If someNames(cnt) Like "A:\*eter.msg" Then
Debug.Print someNames(cnt)
End If
Next
End Sub
Loop through files in a folder using VBA?

VBA Access CSV import issue from netdrive using shell

I have an issue which i can't figure out.
Quest is to import csv file from a netdrive.
In a basic version files to import were selected automaticaly by code:
Function FilesAfterDate(Directory As String, FileSpec As String, AfterDate As Date) As String()
'Requires a reference to the Microsoft Scripting Runtime object lib
Dim oFS As New FileSystemObject, oFile As File
Dim listFiles() As String
Dim i As Long
i = 0
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
For Each oFile In oFS.GetFolder(Directory).files
If oFile.DateLastModified > AfterDate And oFile.Name Like FileSpec Then
ReDim Preserve listFiles(i)
listFiles(i) = oFile.Name
i = i + 1
End If
Next
FilesAfterDate = listFiles
End Function
And then it was imported by code (for each Importfiles(i) where ImportReport = fullpath of Importfiles(i))
DoCmd.TransferText acImportDelim, "ImpSpec_" & sObjSpec & "Csv", "tb" & sObjName & cloneTableNameDesc, ImportReport, False
This solution works really slow, so with the help of the users of this portal I've created a shell import:
fileDetails = Split(CreateObject("wscript.shell").exec("cmd /c pushd " & Chr(34) & source_path & Chr(34) & " & forfiles /S /D +" & s_data & " & popd").StdOut.ReadAll, Chr(10))
and if I use same import command where ImportReport = fullpath of fileDetails (i) i get an error number 31519.
I used debug.print to check all vartypes, path etc and they are all the same. However sollution with shell doesn't work... Any idea for the reason why?
SOLVED:
As I figure it out - splitting shell function to array somehow doesn't have right data/names for access.
It worked when instead of splitting the shell function i've just assigned it to string value
full_string_paths = CreateObject("wscript.shell").exec("cmd /c pushd " & Chr(34) & source_path & Chr(34) & " & forfiles /S /D +" & s_data & " & popd").StdOut.ReadAll
and then using Mid function and Instr I've created an array of correct filename's
After that everything worked perfectly.

VBA Excel - Send to Compressed Zip Folder

I'm looking for a code to zip the folders of a path specified in my cells(1,1).value
After googling i found vba codes to zip the files of a folder but they are using WinZip.
My office machine does not have a WinZip installed and we are restricted to use WinZip. Could anyone please help with this. I need to use the default zip (Right click -> Send to compressed ZIP folder)
Thanks!
Sub Zip_File_Or_Files()
Dim strDate As String, DefPath As String, sFName As String
Dim oApp As Object, iCtr As Long, I As Integer
Dim FName, vArr, FileNameZip
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"
'Browse to the file(s), use the Ctrl key to select more files
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
MultiSelect:=True, Title:="Select the files you want to zip")
If IsArray(FName) = False Then
'do nothing
Else
'Create empty Zip File
NewZip (FileNameZip)
Set oApp = CreateObject("Shell.Application")
I = 0
For iCtr = LBound(FName) To UBound(FName)
vArr = Split97(FName(iCtr), "\")
sFName = vArr(UBound(vArr))
If bIsBookOpen(sFName) Then
MsgBox "You can't zip a file that is open!" & vbLf & _
"Please close it and try again: " & FName(iCtr)
Else
'Copy the file to the compressed folder
I = I + 1
oApp.Namespace(FileNameZip).CopyHere FName(iCtr)
'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameZip).items.Count = I
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0
End If
Next iCtr
MsgBox "You find the zipfile here: " & FileNameZip
End If
End Sub
Powered by Ron De Bruin - http://www.rondebruin.nl/win/s7/win001.htm
I have found it helpful to make a couple of tweaks to make this more friendly for the user (which for this sort of thing is often myself).
Limit how long you're willing to wait for the file & message the user if that time limit was reached without success
Add a DoEvents so that you can ctrl+break to pause the code in case you want to inspect (otherwise - can sometimes have to crash Excel, in my experience)
Add a statusbar update so the user knows what's going on
Sub ZipTheFile(ByVal strPath As String, ByVal strFileNameXls As String, ByVal strFileNameZip As String)
'Taken largely from Ron De Bruin - https://www.rondebruin.nl/win/s7/win001.htm
'Create empty Zip File
NewZip (strPath & strFileNameZip)
'Copy the file in the compressed folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(strPath & strFileNameZip).CopyHere strPath & strFileNameXls
'Keep script waiting until Compressing is done (OR we waited more than 40 seconds...)
On Error Resume Next
i = 0
Do Until oApp.Namespace(strPath & strFileNameZip).Items.Count = 1 Or i > 40 '<-- set how long you're willing to wait here
Application.Wait (Now + TimeValue("0:00:01"))
DoEvents
Application.StatusBar = "Waiting for Zip - counter: " & i
i = i + 1
Loop
On Error GoTo 0
If i > 40 Then MsgBox "there seems to have been a problem putting the file into the zip foder. Check the zip at: " & strPath & strFileNameZip
End Sub
Sub NewZip(sPath) 'You need this sub-routine as well
'Create empty Zip File
'by Ron De Bruin - https://www.rondebruin.nl/win/s7/win001.htm
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
STILL Powered by Ron De Bruin - http://www.rondebruin.nl/win/s7/win001.htm

Excel VBA Get True Folder Path Length (Excluding Tilde)

I have VBA code that creates a backup file (using .SaveCopyAs) every X times my client saves the file. Recently a client ran into the max folder & file path length which seems to be around 220 characters. I'm trying to catch the long file name but Excel/Windows is replacing the long folder names with ~ (tilde) so I can't get the true path length.
How do I get the actual folder/file path string length and prevent Windows from using the "~"?
Sub Backup()
Set awb = ActiveWorkbook
BackupFolder = awb.Path & "\Backups"
BackupFileName = BackupFolder & "\" & awb.Name
BackupFileName = BackupFileName & " " & Format(Now(), "mmddhhmm") & ".xlsm"
'debug.print BackupFileName
'Result: D:\MF\DOCUME~1\LATEST~1\MASTER~1\SUPERL~1\SUPERL~1\Backups\TestLength-07021655.xlsm
'debug.print Len(BackupFileName)
'Result: 83 but the TRUE length is well over 300 characters
PathLen = Len(BackupFileName) 'Result: 83
If PathLen > 215 Then 'This obviously doesn't fire
BackupFolder = GetDesktop & "BidListBackups"
BackupFileName = BackupFolder & "\" & awb.Name
BackupFileName = BackupFileName & " " & sType & Format(Now(), "mmddhhmm") & ".xlsm"
End If
With awb
.SaveCopyAs BackupFileName
End With
If PathLen > 215
MsgBox "Backup file was saved to your desktop", vbokonly
End If
End Sub
See this answer for code that uses the GetLongPathName API. Note that you will need to increase the buffer size from the 165 shown in the code.

Moving a Sub and maintaining correct scope (replacing Me.)

I have a Sub within my application that is currently located within a userform called FRMPFC_folderCreatorWindow. For clarity of the overall application I wish to move this Sub from the userform into a Module called PFC_filesystemManipulation and call the Sub from there via a button in FRMPFC_folderCreatorWindow however, when I do this and run my code, an error is generated at the line:
For Each cCont In Me.Controls
I understand that this is because the Sub has been taken outside of the context of the form however, how do I maintain context without using Me.Controls? I'm guessing I need to reference the form and use FRMPFC_folderCreatorWindow.Controls but as most of the controls are nested within frames I'm unsure whether my current code acts upon the form or just the frame within which the button is located. Any help would be much appreciated.
Private Sub PFC_createFolders(Basepath, currentControl, parentFolder, parentGroup)
Dim cCont As Control
Dim createSubFolder As String
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
'Check if the project folder already exists and if so, raise an error and exit
MkDir Basepath & "\" & parentFolder
'Create the superceded documents folder in every 2nd generation folder
MkDir Basepath & "\" & parentFolder & "\" & "_Old versions"
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
If cCont.GroupName = parentGroup Then
If cCont.Value = True Then
If cCont.Name <> currentControl Then
createSubFolder = cCont.Caption
NewFolder = Basepath & "\" & parentFolder & "\" & createSubFolder
If fs.folderexists(NewFolder) Then
'do nothing
Else
'Create 3rd generation folder
MkDir NewFolder
'Create the superceded documents folder in every 3rd generation folder
MkDir NewFolder & "\" & "_Old versions"
'Create hard-coded subfolders within Confirmit Exports
If createSubFolder = "Confirmit Exports" Then
MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Triple S"
MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Word Export"
MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Survey Definition"
MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Data"
MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Data" & "\" & "Early Data"
MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Data" & "\" & "Final Data"
End If
End If
End If
End If
End If
End If
Next cCont
End Sub
I've just experimented with this and replacing the Me.Controls with the following code works:
FRMPFC_folderCreatorWindow.Controls