Skip to content

Instantly share code, notes, and snippets.

@emersonbroga
Created June 26, 2013 15:56
Show Gist options
  • Save emersonbroga/5868644 to your computer and use it in GitHub Desktop.
Save emersonbroga/5868644 to your computer and use it in GitHub Desktop.
<?php
// pasta onde se encontram os arquivos. Caminho completo ou relativo
$dir = './selecionadas/';
// total de arquivos a serem renomeados.
$total = 106;
// prefixo do nome do arquivo
$prefix = 'arquivo';
// extensão
$ext = 'jpg';
// embaralhar?
$shuffle = false;
// NAO EDITAR DAQUI PRA BAIXO
$i = 0;
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $oldFileName)
{
// para nao ficar em loop infinito
if($i >= $total){
break;
}
$attempts = 0;
do{
if($attempts >= $total){
die(PHP_EOL . 'Todas os arquivos ja foram renomeadas.' . PHP_EOL);
}
if($shuffle){
$number = rand(1, $total);
}else{
$number = $i + 1;
}
$number = str_pad($number, 3, '0', STR_PAD_LEFT);
$newFileName = sprintf('%s_%s.%s', $prefix, $number, $ext);
$attempts++;
}while(file_exists($dir . $newFileName ));
rename($oldFileName, $dir . $newFileName);
$i++;
}
die(PHP_EOL . 'Arquivos renomeadas' . PHP_EOL);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment