13

I'm renaming empty file extensions with this command:

rename *. *.bla

However, I have a folder with hundreds of such subfolders, and this command requires me to manually navigate to each subfolder and run it.

Is there a command that I can run from just one upper level folder that will include all the files in the subfolders?

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
Glenn
  • 4,195
  • 9
  • 33
  • 41

7 Answers7

7
for /r c:\path\ %G in (*.) do ren "%G" *.bla
abatishchev
  • 98,240
  • 88
  • 296
  • 433
k3beast
  • 71
  • 1
  • 1
4
@echo off
for /f "tokens=* delims= " %%i in ('dir /b/s/A-d') DO (
  if "%%~xi" == "" rename "%%~fi" "%%~ni.bla"
)

Thanks @Wadih M. Find and rename files with no extension?

Community
  • 1
  • 1
Glenn
  • 4,195
  • 9
  • 33
  • 41
2

You can easily do this and many more things with the Perl module File::Find.

#!perl

use strict;
use warnings;
use File::Find;

my $extension = 'bla';
my $directory = '/tmp/test';

print "Files renamed:\n";
find( \&wanted, $directory );

sub wanted {
    return if /\./;
    return unless -f $File::Find::name;

    if ( rename( $File::Find::name, "$File::Find::name.$extension" ) ) {
        print "    $File::Find::name -> $File::Find::name.$extension\n";
    }
    else {
        print "    Error: $! - $File::Find::name\n";
    }
}
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
2

this will allow you to enter dirs with spaces in the names. (note the double % is for batch files, use a single % for command lines.)

 for /f "tokens=* delims= " %%a in ('dir /b /ad /s') do rename "%%a\*." "*.bla"
akf
  • 38,619
  • 8
  • 86
  • 96
1

Try this

for /R c:\temp %I in (*. ) DO Echo rename %I "%~nI.bla"
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Rihan Meij
  • 1,759
  • 1
  • 14
  • 18
1

You can use for to iterate over subdirectories:

for /d %x in (*) do pushd %x & ren *. *.bla & popd

When using it from a batch file you would need to double the % signs:

for /d %%x in (*) do pushd %%x & ren *. *.bla & popd
Joey
  • 344,408
  • 85
  • 689
  • 683
  • so will this start in the current directory if issued from the command line? (just don't want to accidentally spill into the rest of the drive) – Glenn Jun 22 '09 at 04:52
  • Yes. If you need it to be in a fixed directory, you can alter the wildcard in the parentheses to something like for /d %x in (%userprofile%\foo\*) do ... – Joey Jun 22 '09 at 05:09
0

Using find and xargs and basename would make the expression easier to read

Prepare a rename shell script like this (corrected to handle multiple files)

#!/bin/sh
if [ $# -lt 3 ] 
then
    echo "usage: rename.sh sufold sufnew file1 (file2 file3 ...) "
    exit
fi

sufold=$1
sufnew=$2
shift
shift

for f in $*
do
  echo "to mv " $f `dirname $f`"/"`basename $f $sufold`$sufnew
  mv $f `dirname $f`"/"`basename $f $sufold`$sufnew
done

Then you could invoke that to each file matched your criteria, including recursive folder search by find command

find . -name "*." | xargs rename.sh "." ".bla"

A more common sample

find . -name "*.htm" | xargs rename.sh ".htm" ".html"
tomyjwu
  • 5,049
  • 2
  • 15
  • 6