Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Extracting drive, path, filebasename WITH string functions and NOT system function ???

Reply
 
Old 04-01-2009   #1 (permalink)
Thomas Lebrecht


 
 

Extracting drive, path, filebasename WITH string functions and NOT system function ???

Recently I asked here for functions to extract the individual
parts (drive, path, filebase and extension) from a string like:

myfile="D:\aaa\bbb\ccc\ddd.txt"

I was told to use built-in functions like:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strFile)
Set objFolder = objFile.ParentFolder
Wscript.Echo "Filename : " & objFile.Name
Wscript.Echo "Path : " & objFile.ParentFolder

But the big disadvantage here is that the file MUST exist.

But what if the filenamestring is passed as parameter and tells which log file should be created
(in the future). At the moment of processing the logfile does not exist yet.

So all the built-in funtion don't work and I get an error.

I must use some string manipulation functions like:

Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile, "\"))

But how can I extract similarly filebase name ("ddd"), extension ("txt"),
filename ("ddd.txt") and drive ("D:") with string search and copy functions?

Thomas


My System SpecsSystem Spec
Old 04-01-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???


"Thomas Lebrecht" <t.lebrecht@xxxxxx> wrote in message
news:49d328f1$0$30236$9b4e6d93@xxxxxx-online.net...
Quote:

> Recently I asked here for functions to extract the individual
> parts (drive, path, filebase and extension) from a string like:
>
> myfile="D:\aaa\bbb\ccc\ddd.txt"
>
> I was told to use built-in functions like:
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.GetFile(strFile)
> Set objFolder = objFile.ParentFolder
> Wscript.Echo "Filename : " & objFile.Name
> Wscript.Echo "Path : " & objFile.ParentFolder
>
> But the big disadvantage here is that the file MUST exist.
>
> But what if the filenamestring is passed as parameter and tells which log
> file should be created
> (in the future). At the moment of processing the logfile does not exist
> yet.
>
> So all the built-in funtion don't work and I get an error.
>
> I must use some string manipulation functions like:
>
> Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile,
> "\"))
>
> But how can I extract similarly filebase name ("ddd"), extension ("txt"),
> filename ("ddd.txt") and drive ("D:") with string search and copy
> functions?
>
> Thomas
>
To extract the file name, use the "mid" function in order to extract all
characters after the right-most backslash. InstrRev(strFile, "\") will help
you locate it.

To extract the file extension, use the "mid" function in order to extract
all characters after the right-most full stop. InstrRev(strFile, ".") will
help you locate it.


My System SpecsSystem Spec
Old 04-01-2009   #3 (permalink)
Steve


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???

Thomas Lebrecht wrote:
Quote:

> Recently I asked here for functions to extract the individual
> parts (drive, path, filebase and extension) from a string like:
>
> myfile="D:\aaa\bbb\ccc\ddd.txt"
>
> I was told to use built-in functions like:
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.GetFile(strFile)
> Set objFolder = objFile.ParentFolder
> Wscript.Echo "Filename : " & objFile.Name
> Wscript.Echo "Path : " & objFile.ParentFolder
>
> But the big disadvantage here is that the file MUST exist.
>
> But what if the filenamestring is passed as parameter and tells which
> log file should be created (in the future). At the moment of
> processing the logfile does not exist yet.
>
> So all the built-in funtion don't work and I get an error.
>
> I must use some string manipulation functions like:
>
> Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile,
> "\"))
>
> But how can I extract similarly filebase name ("ddd"), extension
> ("txt"),
> filename ("ddd.txt") and drive ("D:") with string search and copy
> functions?
Paul Randall's reply to your original post explained that the
FileSystemObject object has methods to parse a string to extract the
information. You really should download the scripting help file
(script56.chm) if it's not already on your system.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo "Filename : " & objFSO.GetFileName(myfile)
Wscript.Echo "Path : " & objFSO.GetParentFolderName(myfile)
Wscript.Echo "File basename : " & objFSO.GetBaseName(myfile)
Wscript.Echo "Extension : " & objFSO.GetExtensionName(myfile)
Wscript.Echo "Drive : " & objFSO.GetDriveName(myfile)

As the documentation says: (Honestly, install the docs!) the methods
work only on the provided path string. They do not attempt to resolve
the path, nor check for the existence of the specified path.

--
Steve

When a man wants to murder a tiger he calls it sport; when the tiger
wants to murder him he calls it ferocity. -George Bernard Shaw



My System SpecsSystem Spec
Old 04-01-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???


"Steve" <cerberus40@xxxxxx> wrote in message
news:eRg268qsJHA.5912@xxxxxx
Quote:

> Thomas Lebrecht wrote:
Quote:

>> Recently I asked here for functions to extract the individual
>> parts (drive, path, filebase and extension) from a string like:
>>
>> myfile="D:\aaa\bbb\ccc\ddd.txt"
>>
>> I was told to use built-in functions like:
>>
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set objFile = objFSO.GetFile(strFile)
>> Set objFolder = objFile.ParentFolder
>> Wscript.Echo "Filename : " & objFile.Name
>> Wscript.Echo "Path : " & objFile.ParentFolder
>>
>> But the big disadvantage here is that the file MUST exist.
>>
>> But what if the filenamestring is passed as parameter and tells which
>> log file should be created (in the future). At the moment of
>> processing the logfile does not exist yet.
>>
>> So all the built-in funtion don't work and I get an error.
>>
>> I must use some string manipulation functions like:
>>
>> Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile,
>> "\"))
>>
>> But how can I extract similarly filebase name ("ddd"), extension
>> ("txt"),
>> filename ("ddd.txt") and drive ("D:") with string search and copy
>> functions?
>
> Paul Randall's reply to your original post explained that the
> FileSystemObject object has methods to parse a string to extract the
> information. You really should download the scripting help file
> (script56.chm) if it's not already on your system.
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Wscript.Echo "Filename : " & objFSO.GetFileName(myfile)
> Wscript.Echo "Path : " & objFSO.GetParentFolderName(myfile)
> Wscript.Echo "File basename : " & objFSO.GetBaseName(myfile)
> Wscript.Echo "Extension : " & objFSO.GetExtensionName(myfile)
> Wscript.Echo "Drive : " & objFSO.GetDriveName(myfile)
>
> As the documentation says: (Honestly, install the docs!) the methods
> work only on the provided path string. They do not attempt to resolve
> the path, nor check for the existence of the specified path.
>
> --
> Steve
If the file in question does not exist then the OP will not be able to
create an object based on the file name. As he correctly says, this will
prevent him from extracting the various file name components using FSO
methods. He must use string manipulation techniques, as he suspected.


My System SpecsSystem Spec
Old 04-01-2009   #5 (permalink)
Armin Zingler


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???

"Thomas Lebrecht" <t.lebrecht@xxxxxx> schrieb
Quote:

> Recently I asked here for functions to extract the individual
> parts (drive, path, filebase and extension) from a string like:
Recently I told you, NOT to post vbscript questions to the VB.net group. I
tell you again, now.


Armin

My System SpecsSystem Spec
Old 04-01-2009   #6 (permalink)
Michael Williams


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???

"Armin Zingler" <az.nospam@xxxxxx> wrote in message
news:Od0T0grsJHA.3928@xxxxxx
Quote:

> Recently I told you [Thomas Lebrecht], NOT to post vbscript
> questions to the VB.net group. I tell you again, now.
Why not? It's got "vb" in its name, so it's fair game ;-) At least that's
what the various dotnet evangelists such as the idiot McCarthy tell us when
we ask people not to post vb.net questions to the class Visual Basic group!

Mike




My System SpecsSystem Spec
Old 04-01-2009   #7 (permalink)
T Lavedas


 
 

Re: Extracting drive, path, filebasename WITH string functions andNOT system function ???

On Apr 1, 5:57*am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> "Steve" <cerberu...@xxxxxx> wrote in message
>
> news:eRg268qsJHA.5912@xxxxxx
>
>
>
Quote:

> > Thomas Lebrecht wrote:
Quote:

> >> Recently I asked here for functions to extract the individual
> >> parts (drive, path, filebase and extension) from a string like:
>
Quote:
Quote:

> >> myfile="D:\aaa\bbb\ccc\ddd.txt"
>
Quote:
Quote:

> >> I was told to use built-in functions like:
>
Quote:
Quote:

> >> Set objFSO = CreateObject("Scripting.FileSystemObject")
> >> Set objFile = objFSO.GetFile(strFile)
> >> Set objFolder = objFile.ParentFolder
> >> Wscript.Echo "Filename * * * * *: " & objFile.Name
> >> Wscript.Echo "Path * * * * * * *: " & objFile.ParentFolder
>
Quote:
Quote:

> >> But the big disadvantage here is that the file MUST exist.
>
Quote:
Quote:

> >> But what if the filenamestring is passed as parameter and tells which
> >> log file should be created (in the future). At the moment of
> >> processing the logfile does not exist yet.
>
Quote:
Quote:

> >> So all the built-in funtion don't work and I get an error.
>
Quote:
Quote:

> >> I must use some string manipulation functions like:
>
Quote:
Quote:

> >> Wscript.Echo "Path * * * * * * *: " & Left(strFile, InstrRev(strFile,
> >> "\"))
>
Quote:
Quote:

> >> But how can I extract similarly filebase name ("ddd"), extension
> >> ("txt"),
> >> filename ("ddd.txt") and drive ("D:") with string search and copy
> >> functions?
>
Quote:

> > Paul Randall's reply to your original post explained that the
> > FileSystemObject object has methods to parse a string to extract the
> > information. You really should download the scripting help file
> > (script56.chm) if it's not already on your system.
>
Quote:

> > *Set objFSO = CreateObject("Scripting.FileSystemObject")
> > *Wscript.Echo "Filename * * * : " & objFSO.GetFileName(myfile)
> > *Wscript.Echo "Path * * * * * : " & objFSO.GetParentFolderName(myfile)
> > *Wscript.Echo "File basename *: " & objFSO.GetBaseName(myfile)
> > *Wscript.Echo "Extension * * *: " & objFSO.GetExtensionName(myfile)
> > *Wscript.Echo "Drive * * * * *: " & objFSO.GetDriveName(myfile)
>
Quote:

> > As the documentation says: (Honestly, install the docs!) the methods
> > work only on the provided path string. They do not attempt to resolve
> > the path, nor check for the existence of the specified path.
>
Quote:

> > --
> > Steve
>
> If the file in question does not exist then the OP will not be able to
> create an object based on the file name. As he correctly says, this will
> prevent him from extracting the various file name components using FSO
> methods. He must use string manipulation techniques, as he suspected.
I believe you're assertion is incorrect. No file needs to exist to
use FSO to *parse strings* that look like file paths. (Which is what
the OP states is his objective.) Because the path does not exist does
not limit the ability to parse it. IMHO, the practicalities of using
the parsed information is another issue, entirely. The trick, as
Steve's examples illustrate, is to use the appropriate FSO parsing
methods, as opposed to trying to access the non-existent file's
properties. There is a difference.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
My System SpecsSystem Spec
Old 04-01-2009   #8 (permalink)
Pegasus [MVP]


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???


"T Lavedas" <tglbatch@xxxxxx> wrote in message
news:ea0c50b8-6fae-456a-adb8-f009c77b5f94@xxxxxx
On Apr 1, 5:57 am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

> "Steve" <cerberu...@xxxxxx> wrote in message
>
> news:eRg268qsJHA.5912@xxxxxx
>
>
>
Quote:

> > Thomas Lebrecht wrote:
Quote:

> >> Recently I asked here for functions to extract the individual
> >> parts (drive, path, filebase and extension) from a string like:
>
Quote:
Quote:

> >> myfile="D:\aaa\bbb\ccc\ddd.txt"
>
Quote:
Quote:

> >> I was told to use built-in functions like:
>
Quote:
Quote:

> >> Set objFSO = CreateObject("Scripting.FileSystemObject")
> >> Set objFile = objFSO.GetFile(strFile)
> >> Set objFolder = objFile.ParentFolder
> >> Wscript.Echo "Filename : " & objFile.Name
> >> Wscript.Echo "Path : " & objFile.ParentFolder
>
Quote:
Quote:

> >> But the big disadvantage here is that the file MUST exist.
>
Quote:
Quote:

> >> But what if the filenamestring is passed as parameter and tells which
> >> log file should be created (in the future). At the moment of
> >> processing the logfile does not exist yet.
>
Quote:
Quote:

> >> So all the built-in funtion don't work and I get an error.
>
Quote:
Quote:

> >> I must use some string manipulation functions like:
>
Quote:
Quote:

> >> Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile,
> >> "\"))
>
Quote:
Quote:

> >> But how can I extract similarly filebase name ("ddd"), extension
> >> ("txt"),
> >> filename ("ddd.txt") and drive ("D:") with string search and copy
> >> functions?
>
Quote:

> > Paul Randall's reply to your original post explained that the
> > FileSystemObject object has methods to parse a string to extract the
> > information. You really should download the scripting help file
> > (script56.chm) if it's not already on your system.
>
Quote:

> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Wscript.Echo "Filename : " & objFSO.GetFileName(myfile)
> > Wscript.Echo "Path : " & objFSO.GetParentFolderName(myfile)
> > Wscript.Echo "File basename : " & objFSO.GetBaseName(myfile)
> > Wscript.Echo "Extension : " & objFSO.GetExtensionName(myfile)
> > Wscript.Echo "Drive : " & objFSO.GetDriveName(myfile)
>
Quote:

> > As the documentation says: (Honestly, install the docs!) the methods
> > work only on the provided path string. They do not attempt to resolve
> > the path, nor check for the existence of the specified path.
>
Quote:

> > --
> > Steve
>
> If the file in question does not exist then the OP will not be able to
> create an object based on the file name. As he correctly says, this will
> prevent him from extracting the various file name components using FSO
> methods. He must use string manipulation techniques, as he suspected.
I believe you're assertion is incorrect. No file needs to exist to
use FSO to *parse strings* that look like file paths. (Which is what
the OP states is his objective.) Because the path does not exist does
not limit the ability to parse it. IMHO, the practicalities of using
the parsed information is another issue, entirely. The trick, as
Steve's examples illustrate, is to use the appropriate FSO parsing
methods, as opposed to trying to access the non-existent file's
properties. There is a difference.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
==================
Sounds good but how exactly would you do it? The following script fails as
expected:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.GetFile("c:\SomePath\Some File.txt")
WScript.Echo oFile.Path, oFile.Name, oFile.Type

And how would you get the file extension as opposed to the file type?


My System SpecsSystem Spec
Old 04-01-2009   #9 (permalink)
Paul Randall


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???


"Thomas Lebrecht" <t.lebrecht@xxxxxx> wrote in message
news:49d328f1$0$30236$9b4e6d93@xxxxxx-online.net...
Quote:

> Recently I asked here for functions to extract the individual
> parts (drive, path, filebase and extension) from a string like:
>
> myfile="D:\aaa\bbb\ccc\ddd.txt"
>
> I was told to use built-in functions like:
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.GetFile(strFile)
> Set objFolder = objFile.ParentFolder
> Wscript.Echo "Filename : " & objFile.Name
> Wscript.Echo "Path : " & objFile.ParentFolder
>
> But the big disadvantage here is that the file MUST exist.
>
> But what if the filenamestring is passed as parameter and tells which log
> file should be created
> (in the future). At the moment of processing the logfile does not exist
> yet.
>
> So all the built-in funtion don't work and I get an error.
>
> I must use some string manipulation functions like:
>
> Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile,
> "\"))
>
> But how can I extract similarly filebase name ("ddd"), extension ("txt"),
> filename ("ddd.txt") and drive ("D:") with string search and copy
> functions?
>
> Thomas
Hi, Thomas
As I said in my previous post, the names of the FSO path parsing methods
start with GET and end with NAME. These methods take strings as arguments
and return strings as results. The FSO does not care whether the path
actually exists.

GetAbsolutePathName
GetBaseName
GetDriveName
GetExtensionName
GetFileName
GetParentFolderName

The FSO path-parsing methods are also happy to parse strings that are
similar to file paths, such as URLs. In my experience, it treats forward
and back slashes the same.

-Paul Randall


My System SpecsSystem Spec
Old 04-01-2009   #10 (permalink)
Paul Randall


 
 

Re: Extracting drive, path, filebasename WITH string functions and NOT system function ???


"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%23ZHAYDusJHA.1212@xxxxxx
Quote:

>
> "T Lavedas" <tglbatch@xxxxxx> wrote in message
> news:ea0c50b8-6fae-456a-adb8-f009c77b5f94@xxxxxx
> On Apr 1, 5:57 am, "Pegasus [MVP]" <n...@xxxxxx> wrote:
Quote:

>> "Steve" <cerberu...@xxxxxx> wrote in message
>>
>> news:eRg268qsJHA.5912@xxxxxx
>>
>>
>>
Quote:

>> > Thomas Lebrecht wrote:
>> >> Recently I asked here for functions to extract the individual
>> >> parts (drive, path, filebase and extension) from a string like:
>>
Quote:

>> >> myfile="D:\aaa\bbb\ccc\ddd.txt"
>>
Quote:

>> >> I was told to use built-in functions like:
>>
Quote:

>> >> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> >> Set objFile = objFSO.GetFile(strFile)
>> >> Set objFolder = objFile.ParentFolder
>> >> Wscript.Echo "Filename : " & objFile.Name
>> >> Wscript.Echo "Path : " & objFile.ParentFolder
>>
Quote:

>> >> But the big disadvantage here is that the file MUST exist.
>>
Quote:

>> >> But what if the filenamestring is passed as parameter and tells which
>> >> log file should be created (in the future). At the moment of
>> >> processing the logfile does not exist yet.
>>
Quote:

>> >> So all the built-in funtion don't work and I get an error.
>>
Quote:

>> >> I must use some string manipulation functions like:
>>
Quote:

>> >> Wscript.Echo "Path : " & Left(strFile, InstrRev(strFile,
>> >> "\"))
>>
Quote:

>> >> But how can I extract similarly filebase name ("ddd"), extension
>> >> ("txt"),
>> >> filename ("ddd.txt") and drive ("D:") with string search and copy
>> >> functions?
>>
Quote:

>> > Paul Randall's reply to your original post explained that the
>> > FileSystemObject object has methods to parse a string to extract the
>> > information. You really should download the scripting help file
>> > (script56.chm) if it's not already on your system.
>>
Quote:

>> > Set objFSO = CreateObject("Scripting.FileSystemObject")
>> > Wscript.Echo "Filename : " & objFSO.GetFileName(myfile)
>> > Wscript.Echo "Path : " & objFSO.GetParentFolderName(myfile)
>> > Wscript.Echo "File basename : " & objFSO.GetBaseName(myfile)
>> > Wscript.Echo "Extension : " & objFSO.GetExtensionName(myfile)
>> > Wscript.Echo "Drive : " & objFSO.GetDriveName(myfile)
>>
Quote:

>> > As the documentation says: (Honestly, install the docs!) the methods
>> > work only on the provided path string. They do not attempt to resolve
>> > the path, nor check for the existence of the specified path.
>>
Quote:

>> > --
>> > Steve
>>
>> If the file in question does not exist then the OP will not be able to
>> create an object based on the file name. As he correctly says, this will
>> prevent him from extracting the various file name components using FSO
>> methods. He must use string manipulation techniques, as he suspected.
>
> I believe you're assertion is incorrect. No file needs to exist to
> use FSO to *parse strings* that look like file paths. (Which is what
> the OP states is his objective.) Because the path does not exist does
> not limit the ability to parse it. IMHO, the practicalities of using
> the parsed information is another issue, entirely. The trick, as
> Steve's examples illustrate, is to use the appropriate FSO parsing
> methods, as opposed to trying to access the non-existent file's
> properties. There is a difference.
>
> Tom Lavedas
> ***********
> http://there.is.no.more/tglbatch/
> ==================
> Sounds good but how exactly would you do it? The following script fails as
> expected:
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oFile = oFSO.GetFile("c:\SomePath\Some File.txt")
> WScript.Echo oFile.Path, oFile.Name, oFile.Type
>
> And how would you get the file extension as opposed to the file type?
Don't GetFile; GetFileName

-Paul Randall


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Create a path string VB Script
Extracting drive, path filebasename or extension from complete filename? VB Script
Re: Extracting the last char from the string VB Script
Sub String / Trim functions PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46