2

A large number of file have been "renamed" with a timestamp in the file name. I cannot find a way to mass rename them. I was thinking of using a mass rename like "Bulk Rename Utility" and using the regex command, however I cannot create a command

The bad filename is: [130bpm] salsaish (2020_11_04 13_32_49 UTC).wav the file should be: [130bpm] salsaish.wav

so the string that has be be removed is: (2020_11_04 13_32_49 UTC). However these strings may change depending on the time it was renamed, also file may contain special characters.

Is there a way to rename them all?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287

3 Answers3

1

On my Synology, there's no Perl nor rename by default. By security concern of attack surface, you shouldn't add any Perl on your device. There's an addon, but you can use pure sh that is the default shell (sh is linked to bash):

for i in *.wav; do echo mv -- "$i" "${i% (*}.wav"; done

or substitution in parameter expansion:

for i in *.wav; do echo mv -- "$i" "${i// (*)/}"; done

This use shell parameter expansion See: http://mywiki.wooledge.org/BashFAQ/073 and "Parameter Expansion" in man bash. Also see http://wiki.bash-hackers.org/syntax/pe

% (*

means remove right part of [[:space:]](* as a glob. It's not a regex.

Remove echo when the output looks satisfactory.

0

Using Perl implementation of rename program (there is another program called rename which is a part of util-linux package):

$ perl-rename  -n 's, \(.+?\),,' *
[130bpm] salsaish (2020_11_04 13_32_49 UTC).wav -> [130bpm] salsaish.wav

It will remove the first occurrence of whitespace followed by parenthesis and everything between them in a non-greedy manner. Check if the new name is what you want and remove -n to really perform renaming.

  • To guide you with Perl's rename: https://unix.stackexchange.com/a/727288/12574 – Gilles Quénot Dec 25 '22 at 21:22
  • 2
    @GillesQuenot posting a link to your answer on every question or answer that mentions perl rename is, at the very least, verging on spam. IMO, it's way beyond "verging" and definitely crossed over to spam territory - you've posted the exact same comment to three of my (ancient) answers so far. – cas Dec 26 '22 at 02:21
  • I add context, explanations, required command to install for a bunch of OS. If you feel it annyoning, don't read – Gilles Quénot Dec 26 '22 at 13:39
0

Sorry for the long delay, but here is the code ive used in PHP:

<?php
function getDirContents($dir, &$results = array()) {
    $files = scandir($dir);
foreach ($files as $key =&gt; $value) {
    $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
    if (!is_dir($path)) {
        $results[] = $path;
    } else if ($value != &quot;.&quot; &amp;&amp; $value != &quot;..&quot;) {
        getDirContents($path, $results);
        $results[] = $path;
    }
}

return $results;

}

$dir = getDirContents('./DBM5000');

echo '<table equalcells="1">';

foreach($dir as $file) {

$good_file = preg_replace(&quot;/ \([^)]+\)/&quot;,&quot;&quot;, $file);    

$isfile = is_file($good_file);

echo '&lt;tr&gt;';
echo '&lt;td&gt;'.$file.'&lt;/td&gt;';
echo '&lt;td&gt;'.$good_file.'&lt;/td&gt;';

if(pathinfo($good_file, PATHINFO_EXTENSION))
{
    echo '&lt;td&gt;FILE&lt;/td&gt;';

    rename($file, $good_file);

}
else
{
    echo '&lt;td&gt;DIRECTORY&lt;/td&gt;';
}

echo '&lt;/tr&gt;';   

}

echo '</table>';

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Feb 15 '23 at 16:39