Report

Renaming multiple files in a folder [Solved/Closed]

Ask a question 5Posts Wednesday January 30, 2013Registration date February 5, 2013 Last seen - Last answered on Feb 5, 2013 at 12:55 AM by Hurricane_G
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
Helpful
+1
plus moins
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!!!!!!
Was this answer helpful?  
4482Posts Thursday January 28, 2010Registration date ContributorStatus January 6, 2016 Last seen - Feb 3, 2013 at 05:46 PM
Would a vb script work ?
5Posts Wednesday January 30, 2013Registration date February 5, 2013 Last seen - Feb 3, 2013 at 08:28 PM
Yes ... it will work... I just need it to be done.... :)

Pls support!!!
4482Posts Thursday January 28, 2010Registration date ContributorStatus January 6, 2016 Last seen - Feb 4, 2013 at 07:51 AM
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 

5Posts Wednesday January 30, 2013Registration date February 5, 2013 Last seen - Feb 5, 2013 at 12:55 AM
simply WOW.... Thanks a lot... and this is working perfectly fine for me.


May Godbless u!!!
:)
Helpful
+0
plus moins
Hi Mohit,

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

http://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.
Helpful
+0
plus moins
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.
Helpful
+0
plus moins
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
Helpful
+0
plus moins
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 ?

Member requests are more likely to be responded to.

Members can monitor the statuses of their requests from their account pages.

A CCM membership gives you access to additional options.

Not a member yet?

Sign up now. It takes less than a minute and is completely free!