Archived from groups: microsoft.public.windowsxp.general (
More info?)
EricP wrote:
> I am trying to write a batch file to strip the 4 leading
> characters from the name of all files in a directory. I tried
> using rename but was not having any luck getting the pattern
> matching to strip the characters off. Please let me know if
> anyone knows a way to do this in a batch file.
Hi
You can use a VBScript (.vbs file) to do this:
'--------------------8<----------------------
' Folder where files are located
sFolderPath = "c:\test"
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Get all file names that are 5 characters or
' more (excluding file extension) into an Array
arFiles = Array()
Set oFolder = oFSO.GetFolder(sFolderPath)
For Each oFile In oFolder.Files
If Len(oFSO.GetBaseName(oFile.Name)) >= 5 Then
iCount = UBound(arFiles) + 1
ReDim Preserve arFiles(iCount)
Set arFiles(iCount) = oFile
End If
Next
For i = 0 To UBound(arFiles)
sFileName = arFiles(i).Name
sBaseName = oFSO.GetBaseName(sFileName)
sFileExtension = ""
If InStr(sFileName, ".") > 0 Then
sFileExtension = "." & oFSO.GetExtensionName(sFileName)
End If
sNewBaseName = LTrim(Mid(sBaseName, 5))
If sNewBaseName <> "" Then
sNewName = sNewBaseName & sFileExtension
On Error Resume Next
arFiles(i).Name = sNewName
If Err.Number <> 0 Then
WScript.Echo "Error when renaming " & sFileName & " to " & sNewName _
& vbCrLf & "Reason: " & Err.Description
End If
Else
WScript.Echo "Not possible to rename: " & sFileName
End If
Next
MsgBox "Done!", vbSystemModal+vbInformation, "Rename files"
'--------------------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