FaheemAhmadGul
asked on
Code for Assigning the Name of a File in a Specific Folder to a String Variable
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.
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.
Is your issue getting the filename or how to parse it once you have the whole path and file?
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
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
TestString = Mid(InStrRev(TestString, "\") + 1)
The error message says Compile error. Argument Not Optional
Sorry :)
TestString = Mid(TestString, InStrRev(TestString, "\") + 1)
TestString = Mid(TestString, InStrRev(TestString, "\") + 1)
ASKER
Please also note that I am using this code in a Visual Basic for Applications Project.
ASKER
Many thanks. This worked perfectly. I am very grateful.