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?
for /r c:\path\ %G in (*.) do ren "%G" *.bla
@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?
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"
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";
}
}
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
Try this
for /R c:\temp %I in (*. ) DO Echo rename %I "%~nI.bla"
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"