Skip to content

Instantly share code, notes, and snippets.

@frock81
Last active May 2, 2023 23:43
Show Gist options
  • Save frock81/06e64e5eeb9767c298f89a58ddce279e to your computer and use it in GitHub Desktop.
Save frock81/06e64e5eeb9767c298f89a58ddce279e to your computer and use it in GitHub Desktop.
This script lists all the files with a ".zip" extension in the current directory and its subdirectories. It then renames any file that matches the pattern "(.*NEO-202[123]{1}-)([0-9]*)(.*\.zip)" to have a zero-padded integer in the middle, with five digits. This script was generated by ChatGPT, a language model trained by OpenAI.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This script lists all the files with a ".zip" extension in the current directory and its subdirectories.
# It then renames any file that matches the pattern "(.*NEO-202[123]{1}-)([0-9]*)(.*\.zip)" to have a zero-padded integer in the middle, with five digits.
# This script was generated by ChatGPT, a language model trained by OpenAI.
import os
import re
def list_files(extension=None):
directory = os.getcwd()
for root, dirs, files in os.walk(directory):
for file in files:
if extension is None or file.endswith(extension):
yield os.path.join(root, file)
def main():
pattern = r'(.*NEO-202[123]{1}-)([0-9]*)(.*\.zip)'
zip_files = list_files('.zip')
for file in zip_files:
match = re.match(pattern, file)
if match:
new_file_name = f"{match.group(1)}{int(match.group(2)):05d}{match.group(3)}"
try:
os.rename(file, new_file_name)
print(f"Renamed {file} to {new_file_name}")
except Exception as e:
print(f"Failed to rename {file}: {str(e)}")
break
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment