Using AI Agents for Bulk File Downloads and Data Cleanup

    This guide walks through using AI agents to download and rename large batches of media files from CSV records, covering format-case pitfalls, task completeness, clean handoff of deliverables, cloud-hosted vs. local models, and a serverless pipeline pattern on Baidu AI Cloud.

    In day-to-day data operations and user verification, teams often need to manage large volumes of media. In one case, 237 user records included 176 video URLs from a face-verification step. The business required each video to be downloaded and archived as name-id_number.ext. Manually, 176 files can consume most of a workday and invite missed files or rename errors. High-repetition, rule-heavy work is a natural fit for AI agents.

    Background and pain points

    The source is a CSV of verification data: name, phone, ID number, and video URLs from a third-party face service. In practice, three issues dominate:

    1. Inconsistent extension case. Links use .mp4 and .MOV (mixed case). Naive scripts often force lowercase and break the original format marker.
    2. Completeness. Network blips can stall or fail silently. A few missing files among 176 are expensive to hunt down.
    3. Messy delivery. Scripts leave temp files, logs, and caches. If those are not cleaned up, the folder handed to the business looks cluttered.

    Requirements: download every row with a video URL, preserve original extension case, and apply the naming rule. The agent must generate code, follow business rules strictly, and close the loop on the environment.

    How agents execute the job

    Typical stacks combine LLM reasoning with a code interpreter. Two agent frameworks were tried, both on mainstream lightweight models, at about 1 yuan in tokens per run.

    1. Parse and generate

    The agent inspects CSV structure, maps fields, and emits Python to download. Core logic:

    1. import os
    2. import csv
    3. import requests
    4. def download_and_rename(csv_path, target_dir):
    5. if not os.path.exists(target_dir):
    6. os.makedirs(target_dir)
    7. with open(csv_path, mode='r', encoding='utf-8') as f:
    8. reader = csv.DictReader(f)
    9. for row in reader:
    10. name = row.get('name')
    11. id_card = row.get('id_card')
    12. video_url = row.get('video_url')
    13. if not video_url:
    14. continue
    15. # Keep original extension case
    16. _, ext = os.path.splitext(video_url.split('?')[0])
    17. if not ext:
    18. ext = '.mp4' # fallback
    19. file_name = f"{name}-{id_card}{ext}"
    20. file_path = os.path.join(target_dir, file_name)
    21. try:
    22. response = requests.get(video_url, stream=True, timeout=30)
    23. with open(file_path, 'wb') as vf:
    24. for chunk in response.iter_content(chunk_size=8192):
    25. vf.write(chunk)
    26. except Exception as e:
    27. print(f"Failed to download {file_name}: {e}")

    2. Results

    Both setups downloaded 176 videos with correct names and extension case. Delivery cleanliness differed:

    • Approach A (closed loop): The download folder contained only the 176 videos. After the run, the agent deleted the generated Python script and a copy of the source CSV. The folder was ready to package and pass on.
    • Approach B (leftovers): Videos succeeded, but download.py and a user_verifi_videos.csv copy remained. Extra cleanup would still be needed before production handoff.

    The gap is not model size; it is how well the agent understands task boundaries—what is intermediate vs. final.

    Limits of local open-source models

    Local open-source models largely failed this class of task:

    1. Weak long-context instruction following. Multi-rule prompts often dropped “preserve original extension case,” and suffixes were lowercased.
    2. Fragile tool use. Timeouts or CSV encoding errors rarely led to self-correction; the job stopped.
    3. No cleanup mindset. Scripts were linear, with no post-run teardown.

    For filesystem work and multi-step logic, instruction-tuned cloud models remain the more reliable choice.

    Enterprise pattern: serverless cleanup

    To productionize this, pair the agent with cloud-native orchestration. Using Baidu AI Cloud as an example:

    1. Trigger and orchestration. Upload the source file to object storage (BOS). Event notifications invoke Cloud Functions (CFC). Inject the agent into the function so the job starts automatically.
    2. Execute and store. The agent generates the download script, pulls videos from the third party, renames them as name-id_number, and writes them to a designated BOS delivery bucket.
    3. Isolation and monitoring. Function instances are isolated and discarded after each run, so leftover scripts do not persist. Logs and alerts can track all 176 downloads and retry on failure.

    Conclusion

    Using an AI agent for bulk download and rename is not only labor replacement; it tests whether the model can ship a clean engineering result. A mature agent must finish the work and leave a clean tree. In practice, pay attention to environment closure, recovery from errors, and delivery purity, and combine that with serverless infrastructure for industrial-grade workflows.