From: ekkehard.horner on
williamkow schrieb:
> I have a text file containing a list of path or folder names and filenames,
> and I wish to convert it to a list of path or folder name only -- using .vbs
> language (wscript or cscript)
>
> Example, the content of the input text file.
>
> C:\Users\Alex\Desktop\abc.xls
> C:\Users\Chong\Desktop\abc.xls
> C:\Users\Mona\Desktop\abcDEX.xls
> C:\Users\Siva\Desktop\abc.xls
> C:\Users\Thong\Desktop\adrbc.xls
>
> Output text file :
>
> C:\Users\Alex\Desktop
> C:\Users\Chong\Desktop
> C:\Users\Mona\Desktop
> C:\Users\Siva\Desktop
> C:\Users\Thong\Desktop
>
> Please use simple coding. Thank you very much in advance.
>
Use the .GetParentFolderName method of the FileSystemObject.

demo script:

Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )
Dim aTests : aTests = Array( _
"C:\Users\Alex\Desktop\abc.xls" _
, "C:\Users\Chong\Desktop\abc.xls" _
, "C:\Users\Mona\Desktop\abcDEX.xls" _
, "C:\Users\Siva\Desktop\abc.xls" _
, "C:\Users\Thong\Desktop\adrbc.xls" _
)
Dim sFSpec
For Each sFSpec In aTests
WScript.Echo sFSpec, "==>", goFS.GetParentFolderName( sFSpec )
Next

output:

C:\Users\Alex\Desktop\abc.xls ==> C:\Users\Alex\Desktop
C:\Users\Chong\Desktop\abc.xls ==> C:\Users\Chong\Desktop
C:\Users\Mona\Desktop\abcDEX.xls ==> C:\Users\Mona\Desktop
C:\Users\Siva\Desktop\abc.xls ==> C:\Users\Siva\Desktop
C:\Users\Thong\Desktop\adrbc.xls ==> C:\Users\Thong\Desktop
From: ekkehard.horner on
Mayayana schrieb:
> Function GetParentFolderPath(path)
> Dim Pt1, LenP
> LenP = Len(path)
> Pt1 = InstrRev(path, "\")
> Select Case Pt1
> Case 3
> GetParentFolderPath = Left(path, Pt1) '-- C:\
> Case LenP
> GetParentFolderPath = Left(path, (len(path) - 1))
> Case Else
> GetParentFolderPath = Left(path, (Pt1 - 1))
> End Select
> End Function
>
> In most cases you don't need all that -- if you
> know you have valid file path strings in the first place.
> If you're sure it's a file path and not on a root drive
> then all you really need is:
>
> Pt1 = InstrRev(path, "\")
> GetParentFolderPath = Left(path, (Pt1 - 1))
>
> The way I wrote the function it also deals with
> a root drive/file such as: C:\file.txt
>
> And it deals with a path like: C:\folder1\
> by sending back: C:\folder1

If you compare the results of the goFS.GetParentFolderPath method
and Mayayana's function GetParentFolderPath():

M C:\Users\Thong\Desktop\adrbc.xls ==> C:\Users\Thong\Desktop
F C:\Users\Thong\Desktop\adrbc.xls ==> C:\Users\Thong\Desktop

M C:\adrbc.xls ==> C:\
F C:\adrbc.xls ==> C:\

M C:\dir1\dir2 ==> C:\dir1
F C:\dir1\dir2 ==> C:\dir1

M C:\dir1\dir2\ ==> C:\dir1
F C:\dir1\dir2\ ==> C:\dir1\dir2

M C:\ ==>
F C:\ ==> C:\

you may prefer using the method - much less work and more resonable
results.

[...]