1

I'm looking for a script (Perl, Python or batch would be fine) that will go through a designated file tree and renames all the child files.

For example, Folder1>File1.anytype becomes Folder1>Folder1File1.anytype.

Thanks

Kurt McKee
  • 1,410
  • 13
  • 17
  • Why was this retagged not programming related? –  Apr 05 '09 at 15:49
  • It was probably tagged as not programming related because you appear to just be looking for existing software to accomplish a task and don't intend to do any programming yourself. – brian d foy Apr 05 '09 at 17:51
  • you can remove the tag if you don't like it. – Ian G Apr 05 '09 at 17:54
  • Which system other than PrimeOS (from a very long time ago - circa 1980 when I encountered it) uses '>' to separate components of a path name. – Jonathan Leffler Apr 05 '09 at 17:55
  • Good point Leffler, I didn't mean to imply it was syntax, I will avoid that in the future. –  Apr 05 '09 at 21:38

5 Answers5

2

You mention a batch file, which probably means that you are on Windows (I assume you refer to a .bat file). If you're on a unix system, give this a shot:

find . -mindepth 2 -type f -exec sh -c "mv {} \`dirname {}\`/\`dirname {} | sed 's/^\.//' | sed 's/\///g'\`\`basename {}\`" \;

Alternatively, this Python 3 program may do the trick (should also work on Windows...):

#!/usr/bin/env python3.0

import os
import sys 

def raise_error(e):
    raise e

def full_split(path):
    head, tail = os.path.split(path)

    if head:
        return full_split(head) + [tail]

    return [tail]

def main(args):
    if len(args) != 1:
         print("Please specify one target directory", file=sys.stderr)
         sys.exit(1)

    os.chdir(args[0])
    for dirpath, _, filenames in os.walk('.', onerror=raise_error):
        for f in filenames:
            old = os.path.join(dirpath, f)
            new = os.path.join(dirpath, ''.join(full_split(dirpath[2:]) + [f]))
            os.rename(old, new)

if __name__ == '__main__':
    main(sys.argv[1:])

The directory layout before:

.:
Abc  Def

./Abc:
Foo2.bar  Foo.bar

./Def:
Baz2.quux  Baz.quux  Ghi

./Def/Ghi:
Bar2.foo  Bar.foo

The directory layout after:

.:
Abc  Def

./Abc:
AbcFoo2.bar  AbcFoo.bar

./Def:
DefBaz2.quux  DefBaz.quux  Ghi

./Def/Ghi:
DefGhiBar2.foo  DefGhiBar.foo
Stephan202
  • 59,965
  • 13
  • 127
  • 133
1

The best batch renamer is mmv.

And see also How to do a mass rename?

Community
  • 1
  • 1
bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
0

you could give a try to batchrename: http://batchrename.foryoursoft.com/

pomarc
  • 2,194
  • 3
  • 23
  • 30
0

You can achieve such renames by using a nested for loop at the command line.. albeit a little ugly:

for /D %d in (*) do for %f in ("%d\*.*") do move "%f" "%d\%~nd%~nf"

If you wish to put the above command in a batch script, repeat each % characters once.

for /D %%d in (*) do for %%f in ("%%d\*.*") do move "%%f" "%%d\%%~nd%%~nf"

To understand what the above command does, consult FOR /? at the command line.

I feel like Batch is so arcane these days I need to preserve the BAT wisdom somehow... :-)

I'm kidding :-).. but the command does work, I've tried it.

chakrit
  • 61,017
  • 25
  • 133
  • 162
0

Pretty trivial in Perl using File::Find and File::Copy

jiggy
  • 3,828
  • 1
  • 25
  • 40