By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,466 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,466 IT Pros & Developers. It's quick & easy.

How to find location of variable in string

P: 18
I currently have a code that returns the complete file path name of all files contained within the directory specified ("V:\DeptTrans\") to a variable string ("strFile").

Example Returns:
V:\DeptTrans\Fixed\MDeChene\1675\1675.jpg
V:\DeptTrans\Fixed\MDeChene\1675\112113.vision
V:\DeptTrans\Fixed\1675\1675.jpg
V:\DeptTrans\Fixed\1675\112113.vision
V:\DeptTrans\Fixed\GSmith\234\234.jpg
V:\DeptTrans\Fixed\GSmith\234\050613.vision
V:\DeptTrans\Maint\TGriffith\2103\2103.jpg
V:\DeptTrans\Maint\TGriffith\2103\101013.avi
V:\DeptTrans\Maint\TGriffith\2103\2103\2103a\2103. jpg


From this string I'd like to find the first location in each string which only numbers are contained between the forward slashes, from this I think I'd be able to eliminate the rest of the string.

Example Results (based on Example Returns from above):
V:\DeptTrans\Fixed\MDeChene\1675\
V:\DeptTrans\Fixed\MDeChene\1675\
V:\DeptTrans\Fixed\1675\
V:\DeptTrans\Fixed\1675\
V:\DeptTrans\Fixed\GSmith\234\
V:\DeptTrans\Fixed\GSmith\234\
V:\DeptTrans\Maint\TGriffith\2103\
V:\DeptTrans\Maint\TGriffith\2103\
V:\DeptTrans\Maint\TGriffith\2103\


Also notice the last example shows there are times in which a file may be bedded deeper in sub-folders, in which I still want the first occurrence in which the folder name contains only numbers.

I've tried using Left(), Right(), Mid(), InStr() and even InStrRev() as well as combinations of these, assigning results to additional variables for further manipulation... but I still can't figure this out and I'm thinking someone out there (especially here) might know an easier way (I hope).
Nov 22 '13 #1

✓ answered by NeoPa

Recognising that there is no predefined way to check any string for all integers (IsNumeric() includes other allowable characters.) I suggest that each character of the string is checked until a valid string is found, or until the string ends, whichever is the sooner.

The function to handle each file name could be :
Expand|Select|Wrap|Line Numbers
  1. Private Function NumFolder(strFullName As String) As String
  2.     Dim lngX As Long
  3.     Dim blnNum As Boolean
  4.  
  5.     For lngX = 1 To Len(strFullName)
  6.         Select Case Mid(strFullName, lngX, 1)
  7.         Case "\"
  8.             If blnNum Then Exit For
  9.             blnNum = True
  10.         Case "0" To "9"
  11.             'Do nothing here
  12.         Case Else
  13.             blnNum = False
  14.         End Select
  15.     Next lngX
  16.     If lngX > Len(strFullName) Then Exit Function
  17.     NumFolder = Left(strFullName, lngX)
  18. End Function

Share this Question
Share on Google+
7 Replies


Expert 100+
P: 634
Hi

If its a VBA solution you are after then you could try something like this
Expand|Select|Wrap|Line Numbers
  1. Sub TestFunction()
  2.       Dim File As String
  3.  
  4.     File = "V:\DeptTrans\Fixed\MDeChene\1675\1675.jpg"
  5.     If FileFound(File) Then
  6.         MsgBox "File Found = " & File
  7.     Else
  8.         MsgBox "File not found."
  9.     End If
  10.  
  11. End Sub
  12.  
  13. Function FileFound(ByRef FilePath As String) As Boolean
  14.     Dim i As Integer
  15.     Dim Length As Integer
  16.  
  17.     Length = Len(FilePath)
  18.     i = 1
  19.     Do Until i > Length
  20.         If Mid(FilePath, i, 1) = "\" Then
  21.             Do Until i > Length
  22.                 i = i + 1
  23.                 If Not IsNumeric(Mid(FilePath, i, 1)) Then Exit Do
  24.             Loop
  25.             If Mid(FilePath, i, 1) = "\" Then
  26.                 FilePath = Left(FilePath, i)
  27.                 FileFound = True
  28.                 Exit Function
  29.             End If
  30.         End If
  31.         i = i + 1
  32.     Loop
  33. End Function
??

Code Tags to work (for me anyway ??), which is a great advantage illustating nested loops etc.


MTB
Nov 22 '13 #2

NeoPa
Expert Mod 15k+
P: 31,186
Recognising that there is no predefined way to check any string for all integers (IsNumeric() includes other allowable characters.) I suggest that each character of the string is checked until a valid string is found, or until the string ends, whichever is the sooner.

The function to handle each file name could be :
Expand|Select|Wrap|Line Numbers
  1. Private Function NumFolder(strFullName As String) As String
  2.     Dim lngX As Long
  3.     Dim blnNum As Boolean
  4.  
  5.     For lngX = 1 To Len(strFullName)
  6.         Select Case Mid(strFullName, lngX, 1)
  7.         Case "\"
  8.             If blnNum Then Exit For
  9.             blnNum = True
  10.         Case "0" To "9"
  11.             'Do nothing here
  12.         Case Else
  13.             blnNum = False
  14.         End Select
  15.     Next lngX
  16.     If lngX > Len(strFullName) Then Exit Function
  17.     NumFolder = Left(strFullName, lngX)
  18. End Function
Nov 22 '13 #3

ADezii
Expert 5K+
P: 8,597
Personally, I feel that there is a more compact and efficient solution:
  1. Pass the Absolute PATH to a Function.
  2. Split() the PATH and extract the File Name.
  3. Examine the Base File Name to see if it is Numeric.
  4. If the Base File Name is Numeric, return the Absolute PATH, if not return 'N/A' or anything you so desire.
  5. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fFindNumericFile(strPATH As String) As String
    2. Dim varSplit As Variant
    3. Dim strFileName As String
    4.  
    5. varSplit = Split(strPATH, "\")
    6.  
    7. strFileName = varSplit(UBound(varSplit))    'Extract File Name
    8.  
    9. If IsNumeric(Left$(strFileName, InStrRev(strFileName, ".") - 1)) Then
    10.   fFindNumericFile = strPATH    'Numeric Base
    11. Else
    12.   fFindNumericFile = "N/A"
    13. End If
    14. End Function
    15.  
  6. Sample Function Calls:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print fFindNumericFile("V:\DeptTrans\Fixed\MDeChene\1675\1675.jpg")
    2. Debug.Print fFindNumericFile("V:\DeptTrans\Fixed\MDeChene\1675\112113.vision")
    3. Debug.Print fFindNumericFile("V:\DeptTrans\Fixed\1675\1675.jpg")
    4. Debug.Print fFindNumericFile("V:\DeptTrans\Non Fixed\1675\1675n.jpg")
    5. Debug.Print fFindNumericFile("V:\DeptTrans\Fixed\1675\112113.vision")
    6. Debug.Print fFindNumericFile("V:\DeptTrans\Fixed\GSmith\234\234.jpg")
    7. Debug.Print fFindNumericFile("V:\DeptTrans\Fixed\GSmith\234\050613.vision")
    8. Debug.Print fFindNumericFile("V:\DeptTrans\Maint\TGriffith\2103\2103.jpg")
    9. Debug.Print fFindNumericFile("V:\DeptTrans\Maint\TGriffith\2103\101013.avi")
    10. Debug.Print fFindNumericFile("V:\DeptTrans\Maint\TGriffith\2103\2103\2103a\2103.jpg")
    11. Debug.Print fFindNumericFile("V:\DeptTrans\Maint\TRex\2103\T1013.avi")
    12.  
  7. Function Return Values:
    Expand|Select|Wrap|Line Numbers
    1. V:\DeptTrans\Fixed\MDeChene\1675\1675.jpg
    2. V:\DeptTrans\Fixed\MDeChene\1675\112113.vision
    3. V:\DeptTrans\Fixed\1675\1675.jpg
    4. N/A
    5. V:\DeptTrans\Fixed\1675\112113.vision
    6. V:\DeptTrans\Fixed\GSmith\234\234.jpg
    7. V:\DeptTrans\Fixed\GSmith\234\050613.vision
    8. V:\DeptTrans\Maint\TGriffith\2103\2103.jpg
    9. V:\DeptTrans\Maint\TGriffith\2103\101013.avi
    10. V:\DeptTrans\Maint\TGriffith\2103\2103\2103a\2103.jpg
    11. N/A
    12.  
Nov 22 '13 #4

NeoPa
Expert Mod 15k+
P: 31,186
I'm not sure that meets the requirements of the question ADezii. The idea is to find the first of potentially numerous folder names, within a complete file path, which consists solely of digits (See the expected result for the last example in the question).

IsNumeric(), as I explained was the reason I hadn't used it in my earlier post, may work for the example data but cannot be relied upon to screen out all characters other than the digits (Other characters may legitimately be considered as part of a number). That's one part of why my version was longer.
Nov 23 '13 #5

ADezii
Expert 5K+
P: 8,597
@NeoPa
My sincere apologies! After reading this Post over again I realized that I was way off tangent! What about the highest element of the Array after Split() that consists of all Digits? Trying hard to get out of the HUGH hole that I put myself into (LOL).
Nov 23 '13 #6

NeoPa
Expert Mod 15k+
P: 31,186
Hole/schmole. You're offering your suggestions just as we all are. If there is any judging to be done then it'll be on your overall offerings and importance to the site - and that leaves you as one of the best on here still.

Checking the elements of the Split() results would certainly work my friend, but that leaves you more work to build up the returned value.
Nov 23 '13 #7

P: 18
Worked GREAT, Thank you all!
Nov 24 '13 #8

Post your reply

Sign in to post your reply or Sign up for a free account.