Hello,
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
Hello,
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
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
you could give a try to batchrename: http://batchrename.foryoursoft.com/
The best batch renamer is mmv.
And see also How to do a mass rename?
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.