Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I am new to VBS.I am trying to write a script that would shorten the names of all the files in a folder and then delete multiple entries.
For example:187766501.pdf
I only want to keep first 7 numbers in the file name. Some files will be duplicated after removing the last two numbers.
For Example:187766501.pdf
187766502.pdfif i remove last two numbers from both the files then it creates a duplicate entry. In that case i want to delete one of them. Please Help :(

With CreateObject("Scripting.FileSystemObject") For Each file In .GetFolder(".").Files name = Left(.GetBaseName(file),7) & "." & .GetExtensionName(file) If name <> file.Name Then If Not .FileExists(name) Then file.Name = name Else file.Delete End If End If Next 'file End With

Thankyou for quick response. I tried that script, and it gives me this error:
---------------------------------------------------------------------
Script: C:\Desktop\scp.vbs
Line: 6
Char: 9
Error: File Already Exists
Code: 800A003A
Source: Microsoft VBScript runtime error
----------------------------------------------------------------------This is the what in my script file:
With CreateObject("Scripting.FileSystemObject")
For Each file In .GetFolder("C:\Documents and Settings\gbaidwan\Desktop\Copy\").Files
name = Left(.GetBaseName(file),7) & "." & .GetExtensionName(file)
If name <> file.Name Then
If Not .FileExists(name) Then
file.Name = name
Else
file.Delete
End If
End If
Next 'file
End Wit
I think when it deletes the last two numbers then it creates duplicate files, and it wont let them save in the same folder.

The script was designed to run within the folder in question. You need to keep the path as ".", and you need to move that script into that folder.
Alternatively, you could replace
If Not .FileExists(name) Then
WithIf Not .FileExists("C:\Documents and Settings\gbaidwan\Desktop\Copy\" & name) Then

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |