Hi All,

I am trying to strip the first 8 characters from all files in a directory.

The code I am using is below.

I would greatly appreciate any assistance.

Best Regards

Leon


Sub NameStrip
Dim sDir , sFile , sNewFile

sDir = "C:/tasks/TempDocumentsFolder"

ChDir sDir
sFile = Dir( "document*.*" )
Do While sFile <> ""
' We have a file, so build the new filename for it

' First strip off the leading "document_"
sNewFile = left( sFile, 8 )

' Now rename the file
Name sFile As sNewFile

' Next file, please
sFile = Dir()
Loop
End Sub

Re: Stripping first 8 characters out of filename by Bob

Bob
Wed Sep 15 13:38:20 CDT 2004

Stan S. wrote:
> Your formula for the new name should look like:
> sNewFile = Right(sFile, len(sFile)-8)
>
Mid is a little easier.
sNewFile=Mid(sFile,8)
And isn't he forgetting the Path portion of the filename in his Name
statement?

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Stripping first 8 characters out of filename by Christoph

Christoph
Wed Sep 15 15:23:13 CDT 2004

15.09.2004 20:38, Bob Barrows [MVP] schrieb:

> Stan S. wrote:
>> Your formula for the new name should look like:
>> sNewFile = Right(sFile, len(sFile)-8)
>>
> Mid is a little easier.
> sNewFile=Mid(sFile,8)

If Len(sFile)>9 Then
sNewFile = Mid (sFile, 9)
..
End If


--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979

Re: Stripping first 8 characters out of filename by Bob

Bob
Wed Sep 15 15:39:49 CDT 2004

Which code? The mid function? I use that in vbscript all the time. It's the
Mid statement that can't be used in vbscript:

Mid statement example:
dim str
str="abc"
Mid(str,2,1) = "2"
'When run in VB, str will contain "a2c"
'attempting to run it in vbscript will cause an error


I'm using the Mid function here:
sNewFile=Mid(sFile,8)

It works fine in vbscript. The third argument (size) is optional: when left
out, it defaults to the remaining characters in the string.

Bob Barrows


Tom Lavedas wrote:
> Overlooking the fact that the code is VB specific and not VBScript,
> AFAICT.
>
> Tom Lavedas
> ===========
>
> "Bob Barrows [MVP]" wrote:
>
>> Stan S. wrote:
>>> Your formula for the new name should look like:
>>> sNewFile = Right(sFile, len(sFile)-8)
>>>
>> Mid is a little easier.
>> sNewFile=Mid(sFile,8)
>> And isn't he forgetting the Path portion of the filename in his Name
>> statement?
>>
>> Bob Barrows
>> --
>> Microsoft MVP -- ASP/ASP.NET
>> Please reply to the newsgroup. The email account listed in my From
>> header is my spam trap, so I don't check it very often. You will get
>> a quicker response by posting to the newsgroup.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Stripping first 8 characters out of filename by Bob

Bob
Wed Sep 15 15:41:04 CDT 2004

Christoph Basedau wrote:
> 15.09.2004 20:38, Bob Barrows [MVP] schrieb:
>
>> Stan S. wrote:
>>> Your formula for the new name should look like:
>>> sNewFile = Right(sFile, len(sFile)-8)
>>>
>> Mid is a little easier.
>> sNewFile=Mid(sFile,8)
>
> If Len(sFile)>9 Then
> sNewFile = Mid (sFile, 9)
> ..
> End If
Oops! That's right! I should have used 9. Thanks for the catch.

Bob
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Stripping first 8 characters out of filename by Bob

Bob
Wed Sep 15 15:44:54 CDT 2004

Oh! Now I see what you are talking about! Boy, am I dense today! (no
comments)
To the OP:
Dir, ChDir and Name are VB, not vbscript. In vbscript, you have to use the
FileSystemObject. Here is an example (it's an asp example, but it's
vbscript):
http://www.aspfaq.com/show.asp?id=2039

Bob Barrows


Tom Lavedas wrote:
> Overlooking the fact that the code is VB specific and not VBScript,
> AFAICT.
>
> Tom Lavedas
> ===========
>
> "Bob Barrows [MVP]" wrote:
>
>> Stan S. wrote:
>>> Your formula for the new name should look like:
>>> sNewFile = Right(sFile, len(sFile)-8)
>>>
>> Mid is a little easier.
>> sNewFile=Mid(sFile,8)
>> And isn't he forgetting the Path portion of the filename in his Name
>> statement?
>>
>> Bob Barrows
>> --
>> Microsoft MVP -- ASP/ASP.NET
>> Please reply to the newsgroup. The email account listed in my From
>> header is my spam trap, so I don't check it very often. You will get
>> a quicker response by posting to the newsgroup.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Stripping first 8 characters out of filename by Bob

Bob
Wed Sep 15 15:53:48 CDT 2004

Bob Barrows [MVP] wrote:
> Oh! Now I see what you are talking about! Boy, am I dense today! (no
> comments)
> To the OP:
> Dir, ChDir and Name are VB, not vbscript. In vbscript, you have to
> use the FileSystemObject. Here is an example (it's an asp example,
> but it's vbscript):
> http://www.aspfaq.com/show.asp?id=2039
>

If you are confused by the asp aspect of the examples, go right to the
source. Yu can read the documentation online here:
http://msdn.microsoft.com/library/en-us/script56/html/FSOoriFileSystemObject.asp

or you can download the documentation here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.



Re: Stripping first 8 characters out of filename by leonsharkey

leonsharkey
Thu Sep 16 05:35:08 CDT 2004

"Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message news:<uBVvTR2mEHA.3428@TK2MSFTNGP11.phx.gbl>...
> Christoph Basedau wrote:
> > 15.09.2004 20:38, Bob Barrows [MVP] schrieb:
> >
> >> Stan S. wrote:
> >>> Your formula for the new name should look like:
> >>> sNewFile = Right(sFile, len(sFile)-8)
> >>>
> >> Mid is a little easier.
> >> sNewFile=Mid(sFile,8)
> >
> > If Len(sFile)>9 Then
> > sNewFile = Mid (sFile, 9)
> > ..
> > End If
> Oops! That's right! I should have used 9. Thanks for the catch.
>
> Bob



Thanks Bob and Everyone else,

sub fsoNameStrip

startFSO()

srcFolder = Map("c:/tasks")
sOldFile = File.Name
sNewFile = Right(sOldFile, len(sOldFile)-8)

Set folder = fso.GetFolder(srcFolder)
set sOldFile =File.Name
set sNewFile = Right(sOldFile, len(sOldFile)-8)
For Each File In folder.Files
fso.moveFile Map(sOldFile).Map(sNewFile)
set srcFolder = Nothing
set folder = Nothing
set sOldFile= Nothing
set sNewFile = Nothing
Next

killFSO()

End Sub



Doesn't work though.

Re: Stripping first 8 characters out of filename by Bob

Bob
Thu Sep 16 05:54:28 CDT 2004

Leon wrote:
>
> Thanks Bob and Everyone else,
>
> sub fsoNameStrip
>
> startFSO()
>
> srcFolder = Map("c:/tasks")

Where did you find this Map function? Is it something you wrote yourself?
It's not something that's built in to either vbscript or FileSystemObject

> sOldFile = File.Name
> sNewFile = Right(sOldFile, len(sOldFile)-8)
>
> Set folder = fso.GetFolder(srcFolder)

Is fso a global variable that was created outside of this sub?

> set sOldFile =File.Name
> set sNewFile = Right(sOldFile, len(sOldFile)-8)
> For Each File In folder.Files
> fso.moveFile Map(sOldFile).Map(sNewFile)
> set srcFolder = Nothing
> set folder = Nothing
> set sOldFile= Nothing
> set sNewFile = Nothing
> Next
>
> killFSO()
>
> End Sub
>
>
>
> Doesn't work though.

Well that's helpful .... NOT! :-)
What does "doesn't work" mean? Error messages? Incorrect results?

Here's another page from aspfaq.com that may prove helpful:
http://www.aspfaq.com/show.asp?id=2074

Bob Barrows


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"