Renaming multiple files in a folder

Solved/Closed
Hurricane_G Posts 5 Registration date Wednesday January 30, 2013 Status Member Last seen February 5, 2013 - Jan 30, 2013 at 06:01 AM
Hurricane_G Posts 5 Registration date Wednesday January 30, 2013 Status Member Last seen February 5, 2013 - Feb 5, 2013 at 12:55 AM
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 responses

Hurricane_G Posts 5 Registration date Wednesday January 30, 2013 Status Member Last seen February 5, 2013 1
Feb 3, 2013 at 09:44 AM
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
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Feb 3, 2013 at 05:46 PM
Would a vb script work ?
0
Hurricane_G Posts 5 Registration date Wednesday January 30, 2013 Status Member Last seen February 5, 2013 1
Feb 3, 2013 at 08:28 PM
Yes ... it will work... I just need it to be done.... :)

Pls support!!!
0
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
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 

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


May Godbless u!!!
:)
0
Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 69
Jan 30, 2013 at 06:54 AM
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.
0
Hurricane_G Posts 5 Registration date Wednesday January 30, 2013 Status Member Last seen February 5, 2013 1
Jan 31, 2013 at 03:44 AM
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.
0
Zohaib R Posts 2368 Registration date Sunday September 23, 2012 Status Member Last seen December 13, 2018 69
Feb 2, 2013 at 07:02 AM
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
0

Didn't find the answer you are looking for?

Ask a question
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 766
Feb 3, 2013 at 06:52 AM
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 ?
0