Re: Rename file during copy
- From: Gordon <gordon.mcvey@xxxxxxxxxxxx>
- Date: Thu, 19 Mar 2009 05:44:06 -0700 (PDT)
On Mar 18, 3:36 pm, Michael Fesser <neti...@xxxxxx> wrote:
.oO(Gordon)
I want to add copying to a CMS system I maintain. The problem I'm
running into is deciding the filename when renaming. I have some
rules but can't think of a good way of implementing them.
If a filename doesn't contain "_copy" before its end or before its
extension then append "_copy"
* articlesFolder becomes articlesFolder_copy
* index.html becomes index_copy.html
if the filename contains "_copy" before its end or before its
extension then append "_2"
* articlesFolder_copy becomes articlesFolder_copy_2
* index_copy.html becomes index_copy_2.html
If the filename contains "_copy_n" before its end or before its
extension then replace it with "_copy_n+1"
* articlesFolder_copy_2 becomes articlesFolder_copy_3
* index_copy_2.html becomes insex_copy_3.html
I suspect there's a simple preg_replace regex that could take care of
all these cases but I can't think of it. Other options involve using
lots of different matching tests and a switch or if/else flow control,
but this could get knotty and overly complicated.
You need some if/else or ternary operators anyway to assemble the final
filename. Try this:
<?php
header('Content-Type: text/plain');
// PHP 5.2
function getNewName($filename) {
$info = pathinfo($filename);
$pattern = '/^(.+)(_copy(?:_(\d+))?)?$/U';
if (preg_match($pattern, $info['filename'], $matches)) {
return sprintf('%s%s%s',
$matches[1],
isset($matches[2])
? isset($matches[3]) ? '_copy_'.($matches[3]+1) : '_copy_2'
: '_copy',
isset($info['extension']) ? '.'.$info['extension'] : ''
);
}
}
$tests = array(
'articlesFolder', 'index.html',
'articlesFolder_copy', 'index_copy.html',
'articlesFolder_copy_2', 'index_copy_2.html'
);
foreach ($tests as $testname) {
print "$testname => ".getNewName($testname)."\n";}
?>
Micha
Thanks for the help. I'm not going to be using the provided example
exactly as is, but it was nonetheless extremely helpful, especially
the suggestion of using the pathinfo function, the existence of which
I had simply forgotten!
The current copying method is far from finished, but the renaming
logic is now functioning and seems to work as intended. I'll be
cleaning it up, moving the regex literal to a config file, adding in
more robust checks etc.
public function copyItem (CmsItem $destination, $copyFile = true)
{
// Make sure that the destination is valid
if ($destination instanceof CmsDir)
{
// Clone the current item
$cloned = clone ($this);
// Ensure that a name collision doesn't occur
while ($destination -> childExists ($cloned -> itemProps
['itm_path']))
{
unset ($matches);
unset ($pathParts);
$pathParts = pathinfo ($cloned -> itemProps ['itm_path']);
if (preg_match ('/_copy(_([0-9]+))?$/', $pathParts ['filename'],
$matches))
{
if ($matches [2])
{
$copyNum = $matches [2] + 1;
}
else
{
$copyNum = 2;
}
$pathParts ['filename'] = preg_replace ( '/_copy(_([0-9]+))?$/',
'_copy_' . $copyNum,
$pathParts ['filename']);
}
else
{
$pathParts ['filename'] .= '_copy';
}
$cloned -> itemProps ['itm_path'] = $pathParts ['filename'];
if ($pathParts ['extension'])
{
$cloned -> itemProps ['itm_path'] .= '.' . $pathParts
['extension'];
}
}
}
if ($cloned -> createItem ())
{
return ($cloned);
}
}
.
- References:
- Rename file during copy
- From: Gordon
- Re: Rename file during copy
- From: Michael Fesser
- Rename file during copy
- Prev by Date: Re: php on the command line
- Next by Date: Re: php on the command line
- Previous by thread: Re: Rename file during copy
- Next by thread: managing categories and subcategories
- Index(es):
Relevant Pages
|