You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于首文件lastmodified日期筛选14天内指定匹配文件的技术咨询

Got it, let's tackle this problem step by step. Since you can't just sort files by date and pick the first one (thanks to needing to target specific pattern-matching files), here are two solid, flexible solutions using Python and PowerShell—both let you use a specific file's last modified date as the anchor to pull files from the prior 14 days.


Solution 1: Python Script

This script gives you precise control over which files to target, and lets you either specify an anchor file manually or pick one based on non-date rules (like filename order):

import os
import fnmatch
from datetime import datetime, timedelta

# Configuration - tweak these values to match your needs
target_dir = "/path/to/your/target/folder"  # Replace with your folder path
file_pattern = "report_*.csv"  # Replace with your specific file pattern (supports wildcards)
output_file = "filtered_files_list.txt"

# Optional: Choose your anchor file (pick one method below)
## Method 1: Manually specify the anchor file name
# anchor_file_name = "Q3_sales_report.csv"

## Method 2: Pick the first matching file sorted by filename (not date)
matching_files = [f for f in os.listdir(target_dir) if fnmatch.fnmatch(f, file_pattern)]
matching_files_sorted_by_name = sorted(matching_files)
anchor_file_name = matching_files_sorted_by_name[0]

# Validate anchor file exists
anchor_path = os.path.join(target_dir, anchor_file_name)
if not os.path.exists(anchor_path):
    print(f"Oops: Anchor file '{anchor_file_name}' wasn't found in the target folder!")
    exit(1)

# Calculate the 14-day window
anchor_last_modified = datetime.fromtimestamp(os.path.getmtime(anchor_path))
window_start = anchor_last_modified - timedelta(days=14)

# Filter all matching files within the window
filtered_files = []
for file in matching_files:
    file_path = os.path.join(target_dir, file)
    file_last_modified = datetime.fromtimestamp(os.path.getmtime(file_path))
    if window_start <= file_last_modified <= anchor_last_modified:
        filtered_files.append(file)

# Write the results to a text file
with open(output_file, "w") as f:
    for file in filtered_files:
        f.write(f"{file}\n")

print(f"Done! Filtered file list saved to '{output_file}'")

Solution 2: PowerShell Script

Great for Windows users (or cross-platform with PowerShell Core), this script works similarly and is easy to adapt:

# Configuration
$targetDir = "C:\path\to\your\target\folder"  # Replace with your folder path
$filePattern = "report_*.csv"  # Replace with your specific file pattern
$outputFile = "filtered_files_list.txt"

# Optional: Choose your anchor file (pick one method below)
## Method 1: Manually specify the anchor file name
# $anchorFile = Get-Item (Join-Path $targetDir "Q3_sales_report.csv")

## Method 2: Pick the first matching file sorted by filename (not date)
$anchorFile = Get-ChildItem -Path $targetDir -Filter $filePattern | Sort-Object Name | Select-Object -First 1

# Validate anchor file exists
if (-not $anchorFile) {
    Write-Error "Oops: Anchor file not found! Check your pattern or folder path."
    exit 1
}

# Calculate the 14-day window
$windowStart = $anchorFile.LastWriteTime.AddDays(-14)

# Filter matching files in the window
$filteredFiles = Get-ChildItem -Path $targetDir -Filter $filePattern | Where-Object {
    $_.LastWriteTime -ge $windowStart -and $_.LastWriteTime -le $anchorFile.LastWriteTime
} | Select-Object -ExpandProperty Name

# Write results to text file
$filteredFiles | Out-File -FilePath $outputFile -Encoding UTF8

Write-Host "Done! Filtered file list saved to '$outputFile'"

Quick Notes

  • Both scripts let you swap out the anchor file selection method to fit your needs (manual name, filename-sorted first, or even add custom logic like picking the file with a specific keyword in its name).
  • The window is inclusive: it includes files modified on the anchor's last modified date and all dates 14 days prior up to that point.

内容的提问来源于stack exchange,提问作者Mira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:06:05