Renaming multiple files in a folder [Solved/Closed]

Report
Posts
5
Registration date
Wednesday January 30, 2013
Status
Member
Last seen
February 5, 2013
-
Posts
5
Registration date
Wednesday January 30, 2013
Status
Member
Last seen
February 5, 2013
-
Hi all,

I require a help herewith. Actually I require to develop a Batch Script for renaming following 4 files kept in a folder "C:\files"

Actual File:

1) ABC_XCV_2013-01-30-07-15-29.xls
2) ZXC_VBN_2013-01-30-06-18-28.xls
3) OLK_JHU_2013-01-30-02-22-33.pdf
4) NKI_SJS_2013-01-30-01-12-29.pdf

Required file after execution of Script:

1) ABC_XCV_2013-01-29.xls
2) ZXC_VBN_2013-01-29.xls
3) OLK_JHU_2013-01-29.pdf
4) NKI_SJS_2013-01-29.pdf

I sincerely request to pls support me in this development.

Thanks,
Mohit

5 replies

Posts
5
Registration date
Wednesday January 30, 2013
Status
Member
Last seen
February 5, 2013
1
Yes rizvisa1... u r rite in the understanding... only catch is that initial count is not fixed to 8... it could be variable length.....

However the ending date time stamp length will remain constant ie 20 (_2013-01-30-07-15-29) and yes I need to rename it to a date earlier and drop the time stamp.


KINDLY HELP!!!!!!
1
Thank you

A few words of thanks would be greatly appreciated. Add comment

CCM 2942 users have said thank you to us this month

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
766
Would a vb script work ?
Posts
5
Registration date
Wednesday January 30, 2013
Status
Member
Last seen
February 5, 2013
1
Yes ... it will work... I just need it to be done.... :)

Pls support!!!
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
766
try this

Copy the code in a file and save with with extension VBS
Double click on file. It will ask for a file path, give it the full path where the files are
it will loop through all files in that folder and rename it as you wanted


Option Explicit 

'calls main method 
Call MainMethod() 

'this is main method 
Sub MainMethod() 
Dim filePath 
Dim FSO 
Dim FolderObject 
Dim fileObject 
Dim orgFileName 
Dim newFileName 
Dim periodPosition 

Dim fileNamePart 
Dim fileExtensionPart 
Dim fileDatePart 


    'Define file path. 
    filePath = InputBox("Enter full path to the folder/directory where files are") 
    If (Right(filePath, 1) <> "\") Then filePath = filePath & "\" 


    'Create the instance of the FSO. 
    Set FSO = CreateObject("Scripting.FileSystemObject") 
     
    'Set the folder you want to search. 
    Set FolderObject = FSO.GetFolder(filePath) 

    'Loop through each file in the folder. 
    For Each fileObject In FolderObject.Files 
     
        'Get complete file name with path. 
        orgFileName = fileObject.Path 
         
        'find position of last instance of . 
        periodPosition = InStrRev(orgFileName, ".") 
         
        'using the posion of last . , get the file extension 
        fileExtensionPart = Mid(orgFileName, periodPosition) 
         
        'get the none constant part of file including the extension 
        fileDatePart = Mid(orgFileName, periodPosition - 19) 
         
        'get the constant part of file 
        fileNamePart = Left(orgFileName, periodPosition - 19 - 1) 
         
        'get the date part of the file 
        fileDatePart = Left(fileDatePart, 10) 
         
        'shift back date by a day 
        fileDatePart = formatDate(CDate(fileDatePart) - 1) 
         
        'Build the new file name. 
        newFileName = fileNamePart & fileDatePart & fileExtensionPart 
         

        'Use the MoveFile method to rename the file. 
        FSO.MoveFile orgFileName, newFileName 
    Next 

    'Cleanup the objects. 
    Set FolderObject = Nothing 
    Set FSO = Nothing 

End Sub 

Function formatDate(rawDate) 
Dim newDate 

    newDate = Year(rawDate) 
    newDate = newDate & "-" 
    newDate = newDate & Right("0" & Month(rawDate), 2) 
    newDate = newDate & "-" 
    newDate = newDate & Right("0" & Day(rawDate), 2) 

    formatDate = newDate 
End Function 

Posts
5
Registration date
Wednesday January 30, 2013
Status
Member
Last seen
February 5, 2013
1
simply WOW.... Thanks a lot... and this is working perfectly fine for me.


May Godbless u!!!
:)
Posts
2368
Registration date
Sunday September 23, 2012
Status
Moderator
Last seen
December 13, 2018
25
Hi Mohit,

Please check the below mentioned link and see if it helps:

https://ccm.net/faq/2032-renaming-multiple-files-in-batch

The article in this link explains renaming multiple files in batch.

Kindly get back if there are any further queries.
Posts
5
Registration date
Wednesday January 30, 2013
Status
Member
Last seen
February 5, 2013
1
Thanks Zohaib... I have browsed thru the link and found the Ant Renamer 2 software very useful. However I still willing to get a small batch code to fulfill my requirement.

Also here too I am first deleting last 19 characters and then adding required 10 characters in files.

Altough I am deeply thankful to you for this help however pls support me for the required batch script.
Posts
2368
Registration date
Sunday September 23, 2012
Status
Moderator
Last seen
December 13, 2018
25
Hi Mohit,

I am glad to know that "Ant Renamer" helped you in fixing the issue to an extent. I will definitely try and look for the batch script. In case I find one, I will surely test it and post it here.

You can start following me on this forum, this will keep you updated if there are any updates regarding this from me.

Thanks & Regards
Zohaib R
#iworkfordell
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
766
From the example that you gave, it seems to me that, your file has three parts. The first 8 characters are constant, then a date and then a time. You want to keep the constant part, take the date and make it one day earlier and drop the time part?. Is this the case ?

Subscribe To Our Newsletter!

The Best of CCM in Your Inbox

Subscribe To Our Newsletter!