Computing.Net > Forums > Programming > VBS script to shorten file name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VBS script to shorten file name

Reply to Message Icon

Name: baidwan
Date: September 17, 2009 at 14:53:57 Pacific
OS: Windows XP
CPU/Ram: 4gb
Subcategory: General
Tags: vbs, script, file
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: September 18, 2009 at 05:31:17 Pacific
Reply:

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


0

Response Number 2
Name: baidwan
Date: September 18, 2009 at 06:24:22 Pacific
Reply:

Yes, delete either one of them


0

Response Number 3
Name: Razor2.3
Date: September 18, 2009 at 06:42:34 Pacific
Reply:

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


0

Response Number 4
Name: baidwan
Date: September 18, 2009 at 07:04:09 Pacific
Reply:

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.


0

Response Number 5
Name: Razor2.3
Date: September 18, 2009 at 07:16:50 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: baidwan
Date: September 18, 2009 at 07:24:58 Pacific
Reply:

Works like a charm :)

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: VBS script to shorten file name

VBS script to delete files www.computing.net/answers/programming/vbs-script-to-delete-files/14805.html

VB Script to ftp files www.computing.net/answers/programming/vb-script-to-ftp-files/17951.html

VBS - How to read file contents www.computing.net/answers/programming/vbs-how-to-read-file-contents/14629.html