Re: Rename file during copy



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);
}
}
.



Relevant Pages

  • Re: Keep IE from appending [1] on the end of file downloads?
    ... carfiles have a .cts.car extension, and IE wants to append a to every filename when you try to save them. ... Other browsers do not do this, so it is a problem with IE. I've been trying to find a solutions, but haven't had any luck, I was hoping someone here could help. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: Keep IE from appending [1] on the end of file downloads?
    ... > I run an online racing league, and our carfiles have a .cts.car extension, ... > and IE wants to append a to every filename when you try to save them. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Rename file during copy
    ... running into is deciding the filename when renaming. ... If a filename doesn't contain "_copy" before its end or before its ... extension then append "_copy" ...
    (comp.lang.php)
  • Re: Rename file during copy
    ... If a filename doesn't contain "_copy" before its end or before its ... extension then append "_copy" ... // PHP 5.2 ...
    (comp.lang.php)
  • Re: Using an Excel sheet for batch delete
    ... but wondered what the routine would make of the situation where the input to both source and destination popups was identical. ... When I used it on filenames with no extension, ... Does the filename contain any dots. ... If SourcePath = "" Then Exit Sub ...
    (microsoft.public.excel.worksheet.functions)