#1
|
|||
|
|||
Define New Valid Folder Name
Cross posted in VBA Express:
http://www.vbaexpress.com/forum/show...335#post336335 I'm working on a project where the user is required to identify a new unique folder name prior to further processing. If the folder identified already exists processing cannot continue. If the folder identified is invalid (invalid characters or reserved) then processing cannot continue. The only thing I can think of is: 1) Check if folder exists and 2) Try to create the folder and if it fails ... I created the following function. Anyone have a better idea? Code:
Sub Test() MsgBox fcnIsNewValidFolderName("Test") 'New valid folder name. MsgBox fcnIsNewValidFolderName("My Documents") 'Existing folder - returns false MsgBox fcnIsNewValidFolderName("A*B?C") 'Invalid characters in name - returns false MsgBox fcnIsNewValidFolderName("PRN") 'Reserved name - returns false lbl_Exit: Exit Sub End Sub Function fcnIsNewValidFolderName(strFolder As String) As Boolean Dim oFSO As Object, oRootFolder As Object, oFolder As Object fcnIsNewValidFolderName = True Set oFSO = CreateObject("Scripting.FileSystemObject") On Error GoTo Err_Root Set oRootFolder = oFSO.GetFolder("D:\") On Error GoTo Err_Create Set oFolder = oRootFolder.SubFolders(strFolder) fcnIsNewValidFolderName = False GoTo lbl_Exit CreateReEntry: On Error GoTo Err_Last 'See if a folder can be created using the folder name passed. Set oFolder = oFSO.CreateFolder(oRootFolder & Application.PathSeparator & strFolder) oFolder.Delete lbl_Exit: Set oFSO = Nothing Exit Function Err_Root: fcnIsNewValidFolderName = True Resume lbl_Exit Err_Create: Resume CreateReEntry: Err_Last: Beep fcnIsNewValidFolderName = False Resume lbl_Exit End Function |
#2
|
||||
|
||||
On what basis is the foldername being created? Is it just a random sequence of letters or letters and numbers, or is there some method behind it?
The FolderExists function on my web site - http://www.gmayor.com/useful_vba_functions.htm will check if the folder name exists, and can be used to quit if the folder name exists. You can adapt the FileNameUnique function to work with Foldernames to append a number if the name exists and the CleanFilename function to vet a name for illegal filename characters, but really it is the original naming that is the main issue.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Who is this mysterious respondent
I already have the basic FolderExists function. If not mistaken it is a part of our collaborative modAddInUtils module. I'll look again at FileNamUnique. My first stab is to let the user create or "attempt" to create any folder name they like as long as it doesn't already exists and is a valid folder name. I think I will add a key press event to keep them from entering invalid characters though. Thanks! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Exporting .doc as .pdf Not A Valid File Name | vbAMEN | Word VBA | 3 | 06-09-2014 04:09 PM |
how to download and install office 10 with a valid product key | ozarkopa | Office | 6 | 12-28-2013 06:56 PM |
How to define restrictions to resource combination | andrebmsilva | Project | 6 | 11-10-2013 03:02 PM |
Office 2008 for Mac - product key not valid | NJDevil | Office | 2 | 06-21-2010 10:22 AM |
Powerpoint not recognising wmv as valid movie type | Robairto | PowerPoint | 2 | 05-20-2010 02:43 AM |