Solved

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

Posted on 2009-05-19
8
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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
 
LVL 1

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
 
LVL 1

Author Comment

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

Author Closing Comment

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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 …

752 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