function main_v112
Entry point function that parses command-line arguments to fix file timestamps by setting them to the oldest date found, either for a single file or recursively through a directory.
/tf/active/vicechatdev/mailsearch/fix_file_dates.py
165 - 238
moderate
Purpose
This main function serves as the CLI interface for a file date fixing utility. It configures an argument parser to handle various options including directory/file processing, pattern matching, dry-run mode, and recursive/non-recursive directory traversal. The function validates inputs and delegates to either fix_file_dates() for single files or process_directory() for directory operations.
Source Code
def main():
parser = argparse.ArgumentParser(
description="Fix file dates by setting all timestamps to the oldest date",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Dry run on all files in a directory (recursive by default)
python3 fix_file_dates.py /path/to/folder --dry-run
# Actually fix the dates for all files in directory
python3 fix_file_dates.py /path/to/folder
# Process with custom pattern
python3 fix_file_dates.py /path/to/folder --pattern "*.pdf"
# Process only specific pattern
python3 fix_file_dates.py /path/to/folder --pattern "*_fully_signed.pdf"
# Process single file
python3 fix_file_dates.py --file /path/to/file.pdf
# Process without recursing subdirectories
python3 fix_file_dates.py /path/to/folder --no-recursive
"""
)
parser.add_argument(
'directory',
nargs='?',
help='Directory to process (required unless using --file)'
)
parser.add_argument(
'--pattern',
default='*',
help='File pattern to match (default: * = all files)'
)
parser.add_argument(
'--file',
help='Process a single file instead of a directory'
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Show what would be done without making changes'
)
parser.add_argument(
'--no-recursive',
action='store_true',
help='Do not search subdirectories'
)
args = parser.parse_args()
if args.file:
# Process single file
if not os.path.exists(args.file):
print(f"Error: File {args.file} does not exist")
sys.exit(1)
fix_file_dates(args.file, args.dry_run)
else:
# Process directory
if not args.directory:
parser.error("directory is required unless using --file")
process_directory(
args.directory,
pattern=args.pattern,
dry_run=args.dry_run,
recursive=not args.no_recursive
)
Return Value
This function does not return a value (implicitly returns None). It either exits with sys.exit(1) on error or completes execution after calling the appropriate processing functions.
Dependencies
argparseossyspathlibdatetimesubprocess
Required Imports
import argparse
import os
import sys
from pathlib import Path
from datetime import datetime
import subprocess
Usage Example
# This function is typically called as the script entry point:
# if __name__ == '__main__':
# main()
# Command-line usage examples:
# python3 fix_file_dates.py /path/to/folder --dry-run
# python3 fix_file_dates.py /path/to/folder --pattern '*.pdf'
# python3 fix_file_dates.py --file /path/to/file.pdf
# python3 fix_file_dates.py /path/to/folder --no-recursive
Best Practices
- Always use --dry-run flag first to preview changes before modifying file timestamps
- Ensure the calling script defines fix_file_dates() and process_directory() functions before calling main()
- Use pattern matching (--pattern) to limit processing to specific file types
- The function expects to be called with no arguments as it uses argparse to parse sys.argv
- Error handling exits with code 1 for file not found errors
- Either --file or directory argument must be provided, but not both
- Default behavior is recursive directory traversal unless --no-recursive is specified
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function fix_file_dates 66.2% similar
-
function main_v69 58.5% similar
-
function set_file_dates 57.6% similar
-
function process_directory 57.1% similar
-
function main_v90 56.5% similar