Solved

Code for Assigning the Name of a File in a Specific Folder to a String Variable

Posted on 2009-05-19
8
178 Views
Last Modified: 2013-11-26
I would like to capture the name of a specific file in specific folder in a string variable (MyString) such that the string variable contains only the first two words in the name of the document and does not include the full path of the file or the file extension. To explain it a little further

If I have a file C:\Important Files\Investigation Results.doc
I would like MyString to be equal to Investigation Results

If I have a file C:\Important Files\Test Results\Blood Results.doc
I would then like MyString to be equal to Blood Results

Thanks for your help in anticipation.
0
Comment
Question by:FaheemAhmadGul
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:borgunit
ID: 24422475
Is your issue getting the filename or how to parse it once you have the whole path and file?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24422502
Dim TestString As String, TestArr As Variant, MyString As String

TestString = "C:\Important Files\Investigation Results.doc"
TestString = Mid(InStrRev(TestString, "\") + 1)
TestString = Left(TestString, InStr(1, TestString, ".") - 1)
TestArr = Split(TestString, " ")
If UBound(TestArr) > 0 Then
    MyString = TestArr(0) & " " & TestArr(1)
Else
    MyString = TestArr(0)
End If
0
 

Author Comment

by:FaheemAhmadGul
ID: 24422643
Many thanks for your response to my question. I am sorry I have realized that I have not asked my question properly. The code I want should be such that it first gets the name of the only file that exists in a given folder and then parses that file name such that it assigns the first two words from the name of the only file that exists in that  specific folder to MyString. That is the code writer knows the name of the folder and also knows that there is only one word document that exists in that folder, but he does not know the name of the file that exists in that folder.

So there is a folder called C:\Important Files\    and there is only one word document in this file the name of which consists of two words and the extension name which would be .doc. The code should get those two words that make the file name and assign it to MyString.
As I am revising the question, I am raising the points for this question to 500.
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 24422869
Dim TestString As String, TestArr As Variant, MyString As String

TestString = Dir("C:\Important Files\*.doc") 'modify as needed
If TestString <> "" Then
    TestString = Mid(InStrRev(TestString, "\") + 1)
    TestString = Left(TestString, InStr(1, TestString, ".") - 1)
    TestArr = Split(TestString, " ")
    If UBound(TestArr) > 0 Then
        MyString = TestArr(0) & " " & TestArr(1)
    Else
        MyString = TestArr(0)
    End If
Else
    'code for file not found
End If
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:FaheemAhmadGul
ID: 24423092
Thank you for your response. I get an error at the following line
TestString = Mid(InStrRev(TestString, "\") + 1)

The error message says Compile error. Argument Not Optional
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24423167
Sorry :)

TestString = Mid(TestString, InStrRev(TestString, "\") + 1)
0
 

Author Comment

by:FaheemAhmadGul
ID: 24423206
Please also note that I am using this code in a Visual Basic for Applications Project.
0
 

Author Closing Comment

by:FaheemAhmadGul
ID: 31583014
Many thanks. This worked perfectly. I am very grateful.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now