ckjian Posted November 21, 2008 Share Posted November 21, 2008 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 More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 <?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 />'; } ?> Link to comment https://forums.phpfreaks.com/topic/133625-renaming-filenames-based-on-date-format/#findComment-695200 Share on other sites More sharing options...
Adam Posted November 21, 2008 Share Posted November 21, 2008 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 Link to comment https://forums.phpfreaks.com/topic/133625-renaming-filenames-based-on-date-format/#findComment-695202 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 <?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 />'; } ?> Link to comment https://forums.phpfreaks.com/topic/133625-renaming-filenames-based-on-date-format/#findComment-695221 Share on other sites More sharing options...
ckjian Posted November 21, 2008 Author Share Posted November 21, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133625-renaming-filenames-based-on-date-format/#findComment-695234 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133625-renaming-filenames-based-on-date-format/#findComment-695242 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.