I am automating FTP tranfers, and need to read a log file and extract the
name of the files transferred.

An example line of text is:
150 Opening ASCII mode data connection for testfile.txt(0 bytes).

All that I want to extract from the line is "testfile.txt".

The following code works - (the wscript.echo is the relevant line):
If Left (strline, 3) = "150" then
TestString = strline
TestArray = Split(TestString , " ",-1, 1)
wscript.echo
Left(TestArray(7),InStr(TestArray(7),"(")-1)
Else
'script not included in example
End If

However, if the file name is more than one word long

150 Opening ASCII mode data connection for new testfile.txt(0 bytes).

The script fails.

Can anyone suggest a better way to extract the file name?

Thanks
Jo

Re: Extract information from text file by Roland

Roland
Fri Jul 22 07:53:41 CDT 2005

"Jo Winchester" wrote in message
news:E57B28AA-7E65-4F09-BE0C-481E4689555E@microsoft.com...
:I am automating FTP tranfers, and need to read a log file and extract the
: name of the files transferred.
:
: An example line of text is:
: 150 Opening ASCII mode data connection for testfile.txt(0 bytes).
:
: All that I want to extract from the line is "testfile.txt".
:
: The following code works - (the wscript.echo is the relevant line):
: If Left (strline, 3) = "150" then
: TestString = strline
: TestArray = Split(TestString , " ",-1, 1)
: wscript.echo
: Left(TestArray(7),InStr(TestArray(7),"(")-1)
: Else
: 'script not included in example
: End If
:
: However, if the file name is more than one word long
:
: 150 Opening ASCII mode data connection for new testfile.txt(0 bytes).
:
: The script fails.
:
: Can anyone suggest a better way to extract the file name?

That's probably because you're splitting by spaces and a file name with a
space gets put into two different elements in your array. Please show a
line of data, at least out to element 9.

Also, these are all defaults:

, " ",-1, 1)

in...
TestArray = Split(TestString , " ",-1, 1)
You can just use:
TestArray = Split(TestString)

You can also eliminate this line:
: TestString = strline

Just change the next line to:
TestArray = Split(strline)

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: Extract information from text file by JoWinchester

JoWinchester
Fri Jul 22 09:16:19 CDT 2005

Thanks a lot for your reply - but I have looked at this again and found a
simpler way to extract the file name. This is an example of the code I have
used.

path = _ "C:\mydir\mysubdir\myfile.ext"
posA = InStr(1, path, "\") +1
posB = InStrRev(path, "\")
MsgBox Mid(path, posA, posB - posA)


"Roland Hall" wrote:

> "Jo Winchester" wrote in message
> news:E57B28AA-7E65-4F09-BE0C-481E4689555E@microsoft.com...
> :I am automating FTP tranfers, and need to read a log file and extract the
> : name of the files transferred.
> :
> : An example line of text is:
> : 150 Opening ASCII mode data connection for testfile.txt(0 bytes).
> :
> : All that I want to extract from the line is "testfile.txt".
> :
> : The following code works - (the wscript.echo is the relevant line):
> : If Left (strline, 3) = "150" then
> : TestString = strline
> : TestArray = Split(TestString , " ",-1, 1)
> : wscript.echo
> : Left(TestArray(7),InStr(TestArray(7),"(")-1)
> : Else
> : 'script not included in example
> : End If
> :
> : However, if the file name is more than one word long
> :
> : 150 Opening ASCII mode data connection for new testfile.txt(0 bytes).
> :
> : The script fails.
> :
> : Can anyone suggest a better way to extract the file name?
>
> That's probably because you're splitting by spaces and a file name with a
> space gets put into two different elements in your array. Please show a
> line of data, at least out to element 9.
>
> Also, these are all defaults:
>
> , " ",-1, 1)
>
> in...
> TestArray = Split(TestString , " ",-1, 1)
> You can just use:
> TestArray = Split(TestString)
>
> You can also eliminate this line:
> : TestString = strline
>
> Just change the next line to:
> TestArray = Split(strline)
>
> --
> Roland Hall
> /* This information is distributed in the hope that it will be useful, but
> without any warranty; without even the implied warranty of merchantability
> or fitness for a particular purpose. */
> Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
> WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
> MSDN Library - http://msdn.microsoft.com/library/default.asp
>
>
>

Re: Extract information from text file by James

James
Fri Jul 22 10:09:30 CDT 2005

Use could also use the 'Scripting.FileSystemObject' to do the work for you:

Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
path = "C:\mydir\mysubdir\myfile.ext"
MsgBox FSO.GetFileName(path)

"Jo Winchester" <JoWinchester@discussions.microsoft.com> wrote in message
news:C89C7498-E972-49BF-B6F8-D4213DA56098@microsoft.com...
> Thanks a lot for your reply - but I have looked at this again and found a
> simpler way to extract the file name. This is an example of the code I
have
> used.
>
> path = _ "C:\mydir\mysubdir\myfile.ext"
> posA = InStr(1, path, "\") +1
> posB = InStrRev(path, "\")
> MsgBox Mid(path, posA, posB - posA)
>
>
> "Roland Hall" wrote:
>
> > "Jo Winchester" wrote in message
> > news:E57B28AA-7E65-4F09-BE0C-481E4689555E@microsoft.com...
> > :I am automating FTP tranfers, and need to read a log file and extract
the
> > : name of the files transferred.
> > :
> > : An example line of text is:
> > : 150 Opening ASCII mode data connection for testfile.txt(0 bytes).
> > :
> > : All that I want to extract from the line is "testfile.txt".
> > :
> > : The following code works - (the wscript.echo is the relevant line):
> > : If Left (strline, 3) = "150" then
> > : TestString = strline
> > : TestArray = Split(TestString , " ",-1, 1)
> > : wscript.echo
> > : Left(TestArray(7),InStr(TestArray(7),"(")-1)
> > : Else
> > : 'script not included in example
> > : End If
> > :
> > : However, if the file name is more than one word long
> > :
> > : 150 Opening ASCII mode data connection for new testfile.txt(0 bytes).
> > :
> > : The script fails.
> > :
> > : Can anyone suggest a better way to extract the file name?
> >
> > That's probably because you're splitting by spaces and a file name with
a
> > space gets put into two different elements in your array. Please show a
> > line of data, at least out to element 9.
> >
> > Also, these are all defaults:
> >
> > , " ",-1, 1)
> >
> > in...
> > TestArray = Split(TestString , " ",-1, 1)
> > You can just use:
> > TestArray = Split(TestString)
> >
> > You can also eliminate this line:
> > : TestString = strline
> >
> > Just change the next line to:
> > TestArray = Split(strline)
> >
> > --
> > Roland Hall
> > /* This information is distributed in the hope that it will be useful,
but
> > without any warranty; without even the implied warranty of
merchantability
> > or fitness for a particular purpose. */
> > Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
> > WSH 5.6 Documentation -
http://msdn.microsoft.com/downloads/list/webdev.asp
> > MSDN Library - http://msdn.microsoft.com/library/default.asp
> >
> >
> >



Re: Extract information from text file by Torgeir

Torgeir
Fri Jul 22 12:14:05 CDT 2005

Jo Winchester wrote:

> I am automating FTP tranfers, and need to read a log file and extract the
> name of the files transferred.
>
> An example line of text is:
> 150 Opening ASCII mode data connection for testfile.txt(0 bytes).
>
> All that I want to extract from the line is "testfile.txt".
>
> The following code works - (the wscript.echo is the relevant line):
> If Left (strline, 3) = "150" then
> TestString = strline
> TestArray = Split(TestString , " ",-1, 1)
> wscript.echo
> Left(TestArray(7),InStr(TestArray(7),"(")-1)
> Else
> 'script not included in example
> End If
>
> However, if the file name is more than one word long
>
> 150 Opening ASCII mode data connection for new testfile.txt(0 bytes).
>
> The script fails.
>
> Can anyone suggest a better way to extract the file name?
Hi,

This should work:

'--------------------8<----------------------

strLine = _
"150 Opening ASCII mode data connection for new testfile.txt(0 bytes)."

If Left (strLine, 3) = "150" Then
arrString = Split(strLine)
strFileName = ""
For i = 7 To UBound(arrString) -1
If strFileName = "" Then
strFileName = arrString(i)
Else
strFileName = strFileName & " " & arrString(i)
End If
Next
strFileName = Mid(strFileName, 1, InStrRev(strFileName, "(")-1)
WScript.Echo "File name is: " & strFileName
End If
'--------------------8<----------------------



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Extract information from text file by Roland

Roland
Fri Jul 22 13:29:04 CDT 2005

"Jo Winchester" wrote in message
news:C89C7498-E972-49BF-B6F8-D4213DA56098@microsoft.com...
: Thanks a lot for your reply - but I have looked at this again and found a
: simpler way to extract the file name. This is an example of the code I
have
: used.
:
: path = _ "C:\mydir\mysubdir\myfile.ext"
: posA = InStr(1, path, "\") +1
: posB = InStrRev(path, "\")
: MsgBox Mid(path, posA, posB - posA)

I missed this line in your OP: All that I want to extract from the line is
"testfile.txt"

However, doing it this way, you don't need all that.

If you always know the path will be 2 subfolders deep then you can just do
this:

path = split("C:\mydir\mysubdir\myfile.ext","\")(3)
MsgBox path, 64, "PATH"

If you don't know how deep it will be then:

arrPath = split("C:\mydir\mysubdir\myfile.ext","\")
path = arrPath(ubound(arrPath))
MsgBox path, 64, "PATH"

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: Extract information from text file by Roland

Roland
Fri Jul 22 13:31:55 CDT 2005

"James Whitlow" wrote in message
news:%23UZhd9sjFHA.3300@TK2MSFTNGP15.phx.gbl...
: Use could also use the 'Scripting.FileSystemObject' to do the work for
you:
:
: Dim FSO
: Set FSO = CreateObject("Scripting.FileSystemObject")
: path = "C:\mydir\mysubdir\myfile.ext"
: MsgBox FSO.GetFileName(path)

Hi James...

If he was getting the file information via FSO, this would be the best way
but he's getting it from a text file he's parsing.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: Extract information from text file by Roland

Roland
Fri Jul 22 13:40:02 CDT 2005

"Roland Hall" wrote in message
news:OIUY%23sujFHA.1048@tk2msftngp13.phx.gbl...
: "Jo Winchester" wrote in message
: news:C89C7498-E972-49BF-B6F8-D4213DA56098@microsoft.com...
:: Thanks a lot for your reply - but I have looked at this again and found a
:: simpler way to extract the file name. This is an example of the code I
: have
:: used.
::
:: path = _ "C:\mydir\mysubdir\myfile.ext"
:: posA = InStr(1, path, "\") +1
:: posB = InStrRev(path, "\")
:: MsgBox Mid(path, posA, posB - posA)
:
: I missed this line in your OP: All that I want to extract from the line is
: "testfile.txt"
:
: However, doing it this way, you don't need all that.
:
: If you always know the path will be 2 subfolders deep then you can just do
: this:
:
: path = split("C:\mydir\mysubdir\myfile.ext","\")(3)
: MsgBox path, 64, "PATH"
:
: If you don't know how deep it will be then:
:
: arrPath = split("C:\mydir\mysubdir\myfile.ext","\")
: path = arrPath(ubound(arrPath))
: MsgBox path, 64, "PATH"

Ok, how about all in one statement:

msgbox filter(split("C:\mydir\mysubdir\myfile.ext","\"),".")(0),64,"PATH"

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: Extract information from text file by James

James
Fri Jul 22 13:55:54 CDT 2005

"Roland Hall" <nobody@nowhere> wrote in message
news:OOUjkuujFHA.3656@TK2MSFTNGP09.phx.gbl...
> "James Whitlow" wrote in message
> news:%23UZhd9sjFHA.3300@TK2MSFTNGP15.phx.gbl...
> : Use could also use the 'Scripting.FileSystemObject' to do the work for
> you:
> :
> : Dim FSO
> : Set FSO = CreateObject("Scripting.FileSystemObject")
> : path = "C:\mydir\mysubdir\myfile.ext"
> : MsgBox FSO.GetFileName(path)
>
> Hi James...
>
> If he was getting the file information via FSO, this would be the best way
> but he's getting it from a text file he's parsing.

The filename being parsed does not have to be a real file. Copy the above
into a *.vbs file and execute it. It works just fine on my XP Pro computer.
But, if you don't want to use FSO, you can use:

path = "C:\mydir\mysubdir\myfile.ext"
path = Mid(path, InStrRev(path, "\") + 1)
MsgBox path

Or you can also use a regular expression:

Dim RegEx
Set RegEx = New RegExp
RegEx.Pattern = ".*\\"
path = "C:\mydir\mysubdir\myfile.ext"
path = RegEx.Replace(path, "")
MsgBox path



Re: Extract information from text file by Roland

Roland
Sat Jul 23 02:41:12 CDT 2005

"James Whitlow" wrote in message
news:ek5T%237ujFHA.3148@TK2MSFTNGP09.phx.gbl...
: "Roland Hall" <nobody@nowhere> wrote in message
: news:OOUjkuujFHA.3656@TK2MSFTNGP09.phx.gbl...
: > "James Whitlow" wrote in message
: > news:%23UZhd9sjFHA.3300@TK2MSFTNGP15.phx.gbl...
: > : Use could also use the 'Scripting.FileSystemObject' to do the work for
: > you:
: > :
: > : Dim FSO
: > : Set FSO = CreateObject("Scripting.FileSystemObject")
: > : path = "C:\mydir\mysubdir\myfile.ext"
: > : MsgBox FSO.GetFileName(path)
: >
: > Hi James...
: >
: > If he was getting the file information via FSO, this would be the best
way
: > but he's getting it from a text file he's parsing.
:
: The filename being parsed does not have to be a real file. Copy the above
: into a *.vbs file and execute it. It works just fine on my XP Pro
computer.

I know it does. But I didn't see a need to create an object.

: But, if you don't want to use FSO, you can use:
:
: path = "C:\mydir\mysubdir\myfile.ext"
: path = Mid(path, InStrRev(path, "\") + 1)
: MsgBox path
:
: Or you can also use a regular expression:
:
: Dim RegEx
: Set RegEx = New RegExp
: RegEx.Pattern = ".*\\"
: path = "C:\mydir\mysubdir\myfile.ext"
: path = RegEx.Replace(path, "")
: MsgBox path

Those are good examples. There are quite a few ways.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp