Articles

VBS script to shorten file name

September 17, 2009 at 14:53:57
Specs: Windows XP, 4gb

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.pdf

if 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 :(


See More: VBS script to shorten file name

Report •


#1
September 18, 2009 at 05:31:17

In that case i want to delete one of them.
What, at random?

Report •

#2
September 18, 2009 at 06:24:22

Yes, delete either one of them

Report •

#3
September 18, 2009 at 06:42:34

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


Report •

Related Solutions

#4
September 18, 2009 at 07:04:09

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.


Report •

#5
September 18, 2009 at 07:16:50

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
With

If Not .FileExists("C:\Documents and Settings\gbaidwan\Desktop\Copy\" & name) Then


Report •

#6
September 18, 2009 at 07:24:58

Works like a charm :)

Thankyou so so much!
You are a life saver..


Report •


Ask Question