I have a folder with files named 20060408AL.txt, 20060409AL.txt,
20060410AL.txt, etc. The files use the date and add an "AL" to the end. Can
someone help me with a script that renames each file within a folder and
"strips" the "AL" part at the end?

The end result would convert 20060417AL.txt to 20060417.txt.

Re: Renaming files in a folder by \

\
Mon May 08 19:46:50 CDT 2006

> I have a folder with files named 20060408AL.txt, 20060409AL.txt,
> 20060410AL.txt, etc. The files use the date and add an "AL" to the end. Can
> someone help me with a script that renames each file within a folder and
> "strips" the "AL" part at the end?

> The end result would convert 20060417AL.txt to 20060417.txt.

There is no "rename" method, but "move" has the same effect if the file is moved
to the same directory with a new name. The script below will remove the "AL"
from the end of all filenames in a specified root folder and its subfolders.

'-----------Rename.vbs---------------
Set fso = CreateObject("Scripting.FileSystemObject")
set root=fso.getFolder("d:\path")

call folderlist(root)

sub folderlist(grp)
call filelist(grp)
for each fldr in grp.subFolders
set nf=fso.GetFolder(fldr.path)
call folderlist(nf)
set nf=nothing
next
end sub

sub filelist(grp)
for each file in grp.files
name=file.name
path=file.path
path=mid(path,1,len(path)-len(name))
name=replace(ucase(name),"AL.TXT",".txt")
file.move path & name
next
end sub
'------------------------------------------

It can be simplified if you are not concerned with subdirectories.
--
Crash

"In war there is no substitute for victory."
~ General Douglas MacArthur ~



Re: Renaming files in a folder by Michael

Michael
Mon May 08 23:35:01 CDT 2006

> There is no "rename" method, ...

But the Name property of a File or Folder object is a read/write property...


--
Michael Harris
Microsoft MVP Scripting



Re: Renaming files in a folder by Alexander

Alexander
Tue May 09 04:59:32 CDT 2006

scott schrieb:

> I have a folder with files named 20060408AL.txt, 20060409AL.txt,
> 20060410AL.txt, etc. The files use the date and add an "AL" to the end. Can
> someone help me with a script that renames each file within a folder and
> "strips" the "AL" part at the end?
>
> The end result would convert 20060417AL.txt to 20060417.txt.


RegExp is one way to do it

Schönen Gruss aus Hessen,
Alex


'-----------Rename.vbs---------------
Option Explicit
Dim fso, root, rgAL, renames

Set fso = CreateObject("Scripting.FileSystemObject")
Set root = fso.GetFolder("d:\path")

Set rgAL = New RegExp
rgAL.pattern = "^\(d{8})AL\.txt$"
rgAL.ignorecase = True
renames = 0

Call folderlist(root)
WSH.Echo "renamed " & renames & " file(s)"

Sub folderlist(grp)
Dim File
For each File in grp.Files
If rgAL.Test(File.Name) Then
File.Name = rgAL.Replace (File.Name, "$1.txt")
renames = renames + 1
End If
Next
End Sub
'------------------------------------------

Re: Renaming files in a folder by Alexander

Alexander
Tue May 09 05:53:09 CDT 2006

Alexander Mueller schrieb:

> RegExp is one way to do it

where

> rgAL.pattern = "^\(d{8})AL\.txt$"

reads as follows:

rgAL.pattern = "^(\d{8})AL\.txt$"

Re: Renaming files in a folder by \

\
Tue May 09 07:51:36 CDT 2006

> But the Name property of a File or Folder object is a read/write property...

Ah! Didn't know that. I thought it was read only.
--
Crash