Jump to content

Recommended Posts

Hi All,

I'm a PHP beginning who is trying to find out how to rename filenames in a folder to a specific pattern based on date format.

 

For example, in my folders there are:

 

VN21Nov2008-1.pdf

VN21Nov2008-2.pdf

VN21Nov2008-3.pdf

VN21Nov2008-4.pdf

VN21Nov2008-5.pdf

 

And, I want rename all the filenames to:

 

1VN211108.pdf

2VN211108.pdf

3VN211108.pdf

4VN211108.pdf

5VN211108.pdf

 

The folders contain about 1000+ files with different date.

 

I would like to know how to renames all the files to the pattern I want.

 

However, due to may PHP knowledge limitation, I just don't know where to start. I've done a search in this forum but I couldn't find a way to rename in bulk.

 

Hope somebody out there can help me out. I'll really appreciate it.

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/133625-renaming-filenames-based-on-date-format/
Share on other sites

<?php

$fileList = array( 'VN21Nov2008-1.pdf',
			   'VN21Nov2008-2.pdf',
			   'VN21Nov2008-3.pdf',
			   'VN21Nov2008-4.pdf',
			   'VN21Nov2008-5.pdf'
			 );

foreach ($fileList as $fileName) {
$fileDate = date('dmy',strtotime(substr($fileName,2,9)));
$newFileName = $fileName{12}.substr($fileName,0,2).$fileDate.substr($fileName,-4);
echo $newFileName.'<br />';
}

?>

VN21Nov2008-1.pdf
VN21Nov2008-2.pdf
VN21Nov2008-3.pdf
VN21Nov2008-4.pdf
VN21Nov2008-5.pdf

 

Are they all strictly in this format to start with?

 

Will be there any like:

 

VN1Nov2008-10.pdf  ??

 

I am asking because to use substr() you'd need to know the positioning of each section.

 

Adam

<?php

$fileList = array( 'VN21Nov2008-1.pdf',
                   'VN21Nov2008-2.pdf',
                   'VN21Nov2008-3.pdf',
                   'VN21Nov2008-4.pdf',
                   'VN21Nov2008-5.pdf',
                   'VN21Nov2008-10.pdf',
                   'VN1Nov2008-12.pdf'
			 );

foreach ($fileList as $fileName) {
$prefix = substr($fileName,0,2);
$extension = pathinfo($fileName,PATHINFO_EXTENSION);
$fileName = substr($fileName,strlen($prefix),-1-strlen($extension));
$number = substr($fileName,strpos($fileName,'-')+1);
$fileName = substr($fileName,0,-1-strlen($number));
$fileDate = date('dmy',strtotime($fileName));

$newFileName = $number.$prefix.$fileDate.'.'.$extension;
echo $newFileName.'<br />';
}

?>

VN21Nov2008-1.pdf
VN21Nov2008-2.pdf
VN21Nov2008-3.pdf
VN21Nov2008-4.pdf
VN21Nov2008-5.pdf

 

Are they all strictly in this format to start with?

 

Will be there any like:

 

VN1Nov2008-10.pdf  ??

 

I am asking because to use substr() you'd need to know the positioning of each section.

 

Adam

 

All the filenames shared the same pattern. Some other example are as below:

 

VN01Nov2008-1.pdf

VN04Nov2008-3.pdf

VN21Nov2008-14.pdf

 

The current pattern is:

VNdMY-(PageNo).pdf

 

The new pattern I'm trying to achieve is:

(PageNo)VNdmy.pdf

 

In array, is there any way to list the folder containing the PDF? Because, we have archive of 1000+ since 2007, I don't think I can put all the filenames in array.

 

Meanwhile, I'll try to play around with the code provided by Mark. Thank you Mark. Really appreciate it.

I was only using an array to show my example code.

 

You whould best handle this within a readdir loop, but could also use scandir which would load a list of files for any given directory into an array as well.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.