Solved

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

Posted on 2009-05-19
8
177 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:FaheemAhmadGul
Comment Utility
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
Comment Utility
Sorry :)

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

Author Comment

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

Author Closing Comment

by:FaheemAhmadGul
Comment Utility
Many thanks. This worked perfectly. I am very grateful.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

13 Experts available now in Live!

Get 1:1 Help Now