views:

1651

answers:

6

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?

A: 

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
A: 

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
Giving me not recognizes as internal or external command error. Using single % for command line test. Seems pretty close, but it's just not quite doing it.
Alan
you are using the single % for both the for loop and the rename?
akf
Correct. Any luck on your end?
Alan
yes, it is working fine. can you copy/paste the command you are using?
akf
A: 

Try this

for /R c:\temp %I in (*. ) DO Echo rename %I "%~nI.bla"

Rihan Meij
+2  A: 

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
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)
Alan
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
+1  A: 
@echo off
for /f "tokens=* delims= " %%i in ('dir /b/s/A-d') DO (
  if "%%~xi" == "" rename "%%~fi" "%%~ni.bla"
)

Thanks @Wadih M. http://stackoverflow.com/questions/1025410/find-and-rename-files-with-no-extention

Alan
A: 

I need to make this aswell, but in my case i need to edit all .log files to .txt files within C: and all subfolders how do you do this with batch ?

stian