Check if directory exists without having permissions to directory - vba

I'm using the below code to have a user provide the path to an SFTP directory for recording in an inventory of such vendor projects. I'm hitting an issue populating the path for a project where I know the directory exists, so I'm 99% sure that the issue is I don't have permissions to the directory (which I know I don't).
'Check if the SFTP exists already
If MsgBox("Does the SFTP for this vendor/campaign already exist?", vbYesNo, "SFTP Exists?") = vbYes Then
'Prompt for the network folder
Do While SFTP = ""
SFTP = Trim(InputBox("Please provide the folder path to the report's SFTP.", "New Campaign/Vendor SFTP", " "))
Select Case SFTP
Case "", " "
If MsgBox("No path provided for the new campagin/vendor SFTP folder. Would you like to retry providing the SFTP (clicking 'No' " & _
"will leave the SFTP folder field empty for now).", vbYesNo, "No Value Provided") = vbNo Then
SFTP = "ignore"
Else
SFTP = ""
End If
Case Else
'Potentially valid folder, check if it exists already
If Dir(SFTP, vbDirectory) = "" Then
'Provided path doesn't exist, prompt user to retry
If MsgBox("The specified path (" & SFTP & ") does not exist. Would you like to retry providing the SFTP (clicking 'No' " & _
"will leave the SFTP folder field empty for now).", vbYesNo, "Create the Folder?") = vbYes Then
SFTP = ""
Else
SFTP = "ignore"
End If
End If 'the folder exists no action needed
End Select
Loop
Else
'Set SFTP to ignore so that we leave the field blank downstream
SFTP = "ignore"
End If
In particular I get run-time error 52.
Is there a way to use DIR, or something like it, to check that the directory exists without needing permissions to the folder?
I'd like to keep that validation to avoid things like typos messing up the inventory sheet (further down the path is used to create link in the sheet, which a bad path obviously would mess up), but I can't always be sure that the person who will be filling in the path has access to the folder.

Referencing "Microsoft Scripting Runtime" ..
Not sure ..Check if this works ...
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
and then you can check if the folder exists using..
If fso.FolderExists(FolderPathHere ending with \) Then

Related

Trying to print new folder names to a text edit/numbers file when logged (AppleScript)

I'm trying to add new folders added to a drive onto a textEdit or numbers file so that there's an up to date list of what projects I have active on individual drives.
I'm very new to AppleScript so I don't know what my limitations are, but I basically just need to figure out how to append to the end of the file (it seems like textEdit would be simplest) with the name of the new folder. I currently have:
on adding folder items to theAttachedFolder after receiving theNewItems
-- Get the name of the attached folder
tell application "Finder"
set theName to name of theAttachedFolder
-- Count the new items
set theCount to length of theNewItems
-- Display an alert indicating that the new items were received
activate
display alert "Attention!" message (theCount & " new items were detected in folder. Adding to TextEdit file.")
end repeat
end tell
Any help is much appreciated. Thank you!
The File Read/Write suite in the StandardAdditions scripting dictionary has open for access and write commands that can be used to append to a text file (the AppleScript Language Guide also has a command reference), and AppleScript's text item delimiters or string concatenation can be used to assemble a string from the list of file items.
There are some older topics describing ways to write to a file, but in the following example I've separated the main stuff from the folder action handler so that it can also be called from the run handler for testing in the Script Editor:
property logFile : "/path/to/output/textfile.txt" -- HFS or POSIX
on run -- test
set folderItems to (choose file with multiple selections allowed)
tell application "Finder" to set theFolder to container of first item of folderItems
doStuff(theFolder, folderItems)
end run
on adding folder items to theAttachedFolder after receiving theNewItems
doStuff(theAttachedFolder, theNewItems)
end adding folder items to
to doStuff(theFolder, theItems)
set folderItems to ""
activate
display alert "Attention!" message "" & (count theItems) & " new items were detected in folder." & return & "Adding to log file."
tell application "Finder" to set folderName to name of theFolder
repeat with anItem in theItems
tell application "Finder" to set newName to name of anItem
set folderItems to folderItems & tab & newName & return
end repeat
writeToFile(logFile, "Items added to " & folderName & ":" & return & folderItems & return)
end doStuff
to writeToFile(someFile, someText)
try
set theOpenFile to (open for access someFile with write permission)
write someText to theOpenFile starting at eof -- append
close access theOpenFile
on error -- make sure file is closed
try
close access theOpenFile
end try
end try
end writeToFile

How to delete a excel file in Sharepoint Online using VBA

As per title, I am trying to write code that deletes an excel file from a sharepoint online folder, using only vba.
Since I am very new to VBA, I have no idea where to begin with
Something as the following code should work.
Please note that the Kill function does not move the file to the trash bin. Use with care!
Sub RemoveSharePointFile()
Dim folder As String, file As String, fulladdress As String
folder = "https://example.sharepoint.com/sites/test/"
file = "test.xls" 'must include extension
fulladdress = folder & file
Kill fulladdress
MsgBox "File '" & file & "' was successfully deleted.", vbInformation
End Sub
on cmd create a local folder point to sharepoint with "net use" (net use x:\collaborate.yousite.com#SSL\sites...) then
delete the file from the local driver
kill x:\yourfile.pfd

VB.Net - My.Computer.FileSystem.CopyFile() Won't Let Me Copy a File to a Directory

Basically I'm writing a program what will allow the user to choose a file or directory to copy over a network based on a set of stations and then they type in the location to copy those file/directory to.
I'm getting close to finishing I think but I'm having an issue with the My.Computer.FileSystem.CopyFile() method. I'm checking beforehand to see if the user-selected item to copy is a file or directory but in the case that the user enters a directory for the file to be copied to I get an error telling me that "The given file path ends with a directory separator character.", even though it's the DESTINATION location that it's erroring out on. I need to be able to have it copy a file to a directory if a directory is specified without a filename.
I tried playing around with the Trim functions and copying the name to the end of the destination path but I'm having a hard time getting just the file name of the source file.
Any ideas?
Here's the code for my fileCopy function far-
If (pushFileSelectCheckBox1.Enabled) Then
For Each item As String In stations
copyTo = Path.Combine(copyTo, stations([i].ToString))
copyToLoc = copyTo.ToString
copyToLoc = Path.Combine(copyTo, pushLocationBox1.ToString.Remove(0, 36))
If Directory.Exists(pushFrom1) Then
If (System.IO.Directory.Exists(copyToLoc)) Then
My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc, True)
LogOutput("Directory 1 copied.")
Else
Directory.CreateDirectory(copyToLoc)
LogOutput("Directory created.")
My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc, True)
LogOutput("Directory 1 copied.")
End If
ElseIf File.Exists(pushFrom1) Then
If (System.IO.Directory.Exists(copyToLoc)) Then
My.Computer.FileSystem.CopyFile(pushFrom1, copyToLoc, True)
LogOutput("File 1 copied.")
Else
Directory.CreateDirectory(copyToLoc)
LogOutput("Directory created.")
My.Computer.FileSystem.CopyFile(pushFrom1, copyToLoc, True)
LogOutput("File 1 copied.")
End If
Else
MsgBox("Chosen file, or whatever, is neither a file nor a directory. What did you do?!?!", MsgBoxStyle.Critical, "Umm....")
End If
i += 1
Next
i = 0
End If
Thanks in advance.
Use Path.GetFileName to get the filename from the pushFrom variable and append it at the end of copyToLoc. The second parameter of CopyFile need to end with a filename, not just a path.

Kill Command Deleting Wrong File(s)i

In Access VBA, I have a procedure I've put together to do this:
Allow the user to select zip file(s)
Extract any files from the zip files to the same directory (In this
specific use-case instance, it is ALWAYS extracting Excel files from
Zip files, never any change, and always using the same password)
Then I want the code to Delete the Zip file after extracting the
.xls file.
Everything works beautifully except the delete file portion. The issue is that when I tell it to delete "FileName.Zip", it is deleting "FileName.Zip" AND "FileName.xls"
Is there any way to make sure that he kill command ONLY deletes what I want it to delete? I've used it before on various occasions, and never had this happen before.
Here is the code I am using:
Dim fd As FileDialog
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tblProjectPath")
Set fd = FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = True
fd.Title = "Select TOC Return File(s) to process"
fd.InitialFileName = rs.Fields("ProjectPath") & "\FilingReports\*.zip"
fd.Show
For Each i In fd.SelectedItems
'Debug.Print i
Debug.Print '------------------------'
Debug.Print i
Unzip (i) 'The bit calling the command line unzip utility to unzip the file - just telling it to extract all files to the current folder.
Debug.Print i
'Kill i
'had to take out the kill bit, b/c it was deleting both the .zip and .xls files which is not desired nor expected
If InStr(i, ".zip") Then
Kill i 'Tried to specify only .zip files even though think I shouldn't need to, but it's still deleting .xls files
End If
Next i
Edit: Add Unzip code to post:
Unzip code:
Sub Unzip(Path As String)
Dim strUnzip As String
Dim QU As String 'quotation mark
QU = Chr(34)
strUnzip = QU & "c:\program files (x86)\winzip\wzunzip" & QU & " -s" & _
"ZipPassword " & _
Path & " " '& _
Call Shell(strUnzip)
End Sub
At this point, I don't really think a "real" answer will come about. However, I'll post what I've decided to do with the particular process I'm writing this code for anyway.
I'm going to use a folder structure to divide up the files:
1. Place zip file(s)
2. Unzip files to a 2nd folder
3. After processing Excel files in 2nd folder, move to a 3rd "complete" folder.
This will get around the deleting wrong files bit.
Also, it appears that the cause for the issue is related to something to do with the call to the WinZip Command Line Unzip utility (wzunzip) in the Unzip code above, or else something with the tool itself. I thought that maybe it was b/c the tool was asking me if I wanted to overwrite existing files, but that wasn't the case, b/c I had the same issue when there were no files to overwrite.
Anyway, I'm attempting to close this one up at this point. Thanks to Wayne G. Dunn for his assistance on this.

How can I create a display dialog that presents specific file path options for a given PDF file?

I am very new to using Automator and Applescript.
I would like to use Automator and AppleScript to detect PDF files that are downloaded to the "Downloads" folder and opens a display dialog that allows me to select the file path and move the file. So far, what I have (which isn't right) is something like:
set question to display dialog "Save fileName in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3
set answer to button returned of question
if answer is equal to "Figuring Relation" then
tell application "Finder" to move fileName to POSIX file "/Users/mac/Documents/College/Junior/Fall/Art 347 - Figuring Relation"
I want the "Figuring Relation" and "Iconoclasm" buttons to change the file path to a designated file path (I don't want to browse for it), and the "Elsewhere" button to open a Finder window where I can select/browse the path.
If possible, I'm also looking to add the date to the beginning of the file name as "mm-dd_filename".
I am not sure of how to translate the Automator Input to Applescript, or how to include the filename in the display dialog text. Thank you so much for any help.
Here is an example using just applescript. In my example, it assumes you're selecting the file you're wanting to move, but you could easily add something for the script to "Find" all files ending with ".pdf" if you wanted to and then loop through the results.
on run
try
set thisFile to choose file
tell application "Finder" to set currentName to thisFile's name
-- Setting variables for the destinations to be used later
set FiguringRelationPath to (path to documents folder) & "College:Junior:Fall:Art 347 - Figuring Relation:" as string
set IconoclasmPath to (path to documents folder) & "Iconoclasm:" as string
-- Ask the user
set answer to button returned of (display dialog "Save \"" & currentName & "\" in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3)
-- Set the destination variable based on the users response to the dialog
if answer is equal to "Figuring Relation" then
set destination to FiguringRelationPath
else if answer is equal to "Iconoclasm" then
set destination to IconoclasmPath
else
set destination to choose folder with prompt "Please select the destination folder" as string
end if
-- Test that the destination directory exists, if not post the error
try
set destination to destination as alias
on error
error ("Destination path " & destination as string) & " doesn't appear to exist"
end try
-- Rename the file with the date prefix
set tDatePrefix to (do shell script "date '+%m-%d'") & "_" as string
tell application "Finder" to set x's name to tDatePrefix & x's name as string
-- Move the file
tell application "Finder" to move thisFile to destination
on error err
activate
display dialog "Error: " & err buttons {"OK"} default button 1
end try
end run