Hi,

Can someone create a short code that will rename a files extension in a sub directory using a command line option -r?

So if i have a file a.txt in folder called loc. I enter this "change -r txt doc" and the file will become a.doc in the loc sub directory (as in loc/a.doc).

I need to add this to a code of mine. Also the reason why I want to use a case statement is because I will be adding more options in the future.

Using bash: This worked fine for my baby test. I can't see any need for the case statement. Of course you will need to poke it into shape to do your particular task.

change() {
  mv $1 ${1%$2}$3
}
for f in $(find $toplocation -iname '*.txt' -print) ; do
  change $f txt doc
done

Using bash: This worked fine for my baby test.


But it will fail in several places if any filenames contain whitespace.

find ... -print0 | xargs -0 somecommand works fine with whitespace in paths. However you do have to muck about with somecommand if you want it to have parameters other than the name of the one "found" file at a time. One option is to use a sub-shell to pass parameters as environment variables: ( cmdarg1=1;cmdarg2='TWO'; find ... -print0 | xargs -0 command-uses-2-params ) Note the parentheses around the entire line

Thanks for the help guys. I should be able to getting going with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.