-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove-files.py
More file actions
executable file
·54 lines (42 loc) · 1.7 KB
/
move-files.py
File metadata and controls
executable file
·54 lines (42 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Quick hack to move all the files in subfolders of the current folder into
the current directory and rename them in the order that a lexicographic search
encounters them. Not anywhere near general purpose, and is POSIX-dependent.
Really, I'm just putting this here so I don't have to rewrite it from scratch
some other time.
This script is copyright 2017-20 by Patrick Mooney. It is licensed under the GNU
GPL, either version 3 or (at your option) any later version. See the file
LICENSE.md for details.
"""
import glob
import os
import subprocess
import text_handling as th # https://github.com/patrick-brian-mooney/python-personal-library/blob/master/text_handling.py
debugging = True
current_dir = os.getcwd()
dirs_list = sorted([ d for d in glob.glob('*') if os.path.isdir(d) ])
file_count = 1
if debugging:
th.print_wrapped("DEBUGGING: directories list is %s" % dirs_list)
_ = input("Hit ENTER to continue")
print()
for d in dirs_list:
try:
if debugging:
th.print_wrapped('DEBUGGING: entering directory "%s".' % d)
_ = input("Hit ENTER to continue")
print()
os.chdir(d)
files = sorted(glob.glob('*'))
if debugging:
th.print_wrapped('DEBUGGING: files in that directory are %s.' % files)
_ = input("Hit ENTER to continue")
for f in files:
if debugging: th.print_wrapped(" ...processing %s" % f)
new_name = "%05d%s" % (file_count, os.path.splitext(f)[1])
os.rename(f, new_name)
file_count += 1
subprocess.call('mv "%s" ../' % new_name, shell=True)
finally:
os.chdir(current_dir)