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:
- Inconsistent extension case. Links use
.mp4and.MOV(mixed case). Naive scripts often force lowercase and break the original format marker. - Completeness. Network blips can stall or fail silently. A few missing files among 176 are expensive to hunt down.
- 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:
import osimport csvimport requestsdef download_and_rename(csv_path, target_dir):if not os.path.exists(target_dir):os.makedirs(target_dir)with open(csv_path, mode='r', encoding='utf-8') as f:reader = csv.DictReader(f)for row in reader:name = row.get('name')id_card = row.get('id_card')video_url = row.get('video_url')if not video_url:continue# Keep original extension case_, ext = os.path.splitext(video_url.split('?')[0])if not ext:ext = '.mp4' # fallbackfile_name = f"{name}-{id_card}{ext}"file_path = os.path.join(target_dir, file_name)try:response = requests.get(video_url, stream=True, timeout=30)with open(file_path, 'wb') as vf:for chunk in response.iter_content(chunk_size=8192):vf.write(chunk)except Exception as e: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.pyand auser_verifi_videos.csvcopy 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:
- Weak long-context instruction following. Multi-rule prompts often dropped “preserve original extension case,” and suffixes were lowercased.
- Fragile tool use. Timeouts or CSV encoding errors rarely led to self-correction; the job stopped.
- 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:
- 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.
- 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. - 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.
