Rename files in a directory using names already defined through PHP

Asked

Viewed 1,271 times

0

I need to rename over a thousand files with the . bmp extension located in a "screen" directory".

The files to be renamed follow this logic "2017-08-06 19-29-58.bmp", "2017-08-06 19-29-59.bmp", "2017-08-06 19-30-00.bmp"... according to their creation datum.

I already have a name for each of these files, but the new names do not follow a sequence, for example: the file "2017-08-06 19-29-58.bmp" will be called "White Set.bmp", the following file "2017-08-06 19-29-59.bmp" will be renamed to "Black Set.bmp"the next file "2017-08-06 19-30-00.bmp" will become "Red Set.bmp" and so on.

I don’t know how to specify through PHP code so that the web page checks all the files listed in the directory "screen" and renames them according to my list of already defined names (array).

  • If you will have to manually set all the file names, it is not easier to rename them directly?

  • It is that I can define the names through Excel, some parts of the name will be fixed. Whenever I have to rename a new batch, I will only need to exchange a word, using the mentioned example, I would exchange "Set" for "Pieces", in the future for "Parts"... but I will repeat the process many times, so it would not be good to do it manually.

  • @Isac yours I don’t know if you can see this comment, but I was able to adapt your response that was removed, and it would be the solution to my problem. Someone removed it and I can’t see it anymore.

  • @I’m the one who removed it because it apparently doesn’t match what you want. It would have to be rewritten according to its objectives, but it will have to be much more specific in them.Moreover it seems to me a solution propitiates error.

  • @Isac I understand, but I can work together with this answer https://answall.com/a/108663/33945, I will take the names through PHP and insert them into Excel to sort as needed, and together with the formula Concatenate, create the full array automatically. Unfortunately you removed the answer before I could copy the code passed :(

  • @Wesley I put the answer back so I can see the code I had, and I also took the opportunity to add the logic I think I was looking for.

Show 1 more comment

1 answer

1


If you have predefined names for the source and destination you can use a key array for the old name and value for the new name, and rename through the rename.

Example:

$caminhoBase = "/algumaPasta/"; //ou usar ./ para a pasta onde corre este arquivo php

//A chave é o nome antigo e o valor é o novo
$renomeacoes = Array(
"2017-08-06 19-29-58.bmp" => "Conjunto Branco.bmp",
"2017-08-06 19-29-59.bmp" => "Conjunto Preto.bmp",
"2017-08-06 19-30-00.bmp" => "Conjunto Vermelho.bmp");

//para cada elemento do array fazer a renomeação
foreach($renomeacoes as $antigo => $novo){
    rename("$caminhoBase$antigo","$caminhoBase$novo");
}

If instead you only have the list of new names to assign and want to rename based on the order of the files you already need to get the list of files with readdir check the extension and rename one to one based on the array:

$caminhoBase = "./";  //diretorio de leitura dos arquivos

//agora só os novos
$novosNomes = Array(
    "Conjunto Branco.bmp",
    "Conjunto Preto.bmp",
    "Conjunto Vermelho.bmp");

if ($handle = opendir($caminhoBase)) { //abrir o diretorio
    $i = 0;

    while (false !== ($arquivo = readdir($handle))) { //percorrer os arquivos do diretorio

        if (strpos($arquivo, ".bmp")){ //ver se é .bmp
            rename ($caminhoBase . $arquivo, $caminhoBase . $novosNomes[$i++]);
            if ($i >= count($novosNomes)){ //se já esgotou os nomes do array sai do while
                break;
            }
        }
    }
}
  • The problem in this case is depending on having the names, because the files have a random interval (creation date used as name) between one and the other. It is possible to adapt so that PHP will examine the existing files and set them as key automatically in ascending alphabetical order?

  • @Wesley Secalhar misunderstood the question. So the existing names are random ? but are all the files in a directory? and how do you know which ones to rename? and how do you establish the relationship between them ?

  • The names are automatically generated according to the creation date, in the example 2017-08-06 19-29-58, indicates that it was created on August 6, 2017 at 19:29:58, all images will follow the same format, based on the moment they were created. They are all . bmp files located in a single directory. The relation established as follows: following the alphabetical order, the first file will always receive the name "Black Set", the second file "White Set, the third "Red Set" and so on. I’m sorry I didn’t express myself correctly.

  • @Wesley But is the first file listed alphabetically in a folder ? This seems to me a little propitious to error and difficult to hit with your list of new names built manually. I can however change the code to that if that’s really the point

  • is yes, by applying the Windows filter itself, ordering the folder view, the first file is the old one. Since the name of the files is equal to their respective creation dates, PHP could also be based on the file creation date, so it would be indifferent to the name used, correct?

  • @Wesley not so much because the reading is done by php and has to be done with the function readdir, which reads in the order established by the operating system. Naturally you can try to build an order based on the results by ordering and applying logic on top, but it already complicates considerably

  • thank you very much for the response and adaptation, in the second example works exactly as desired. I will keep an eye out if there is no error as you are concerned by how much is this even. Thank you again.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.