请求协助:为目录遍历.bat脚本添加文件修改日期至.csv文件
Hey there! Let's tweak your batch script to include the file's last modified date alongside the filename and path. Here's a robust, CSV-friendly version that handles special characters (like spaces or quotes in filenames) properly:
@echo off setlocal enabledelayedexpansion :: Set your output CSV filename here set "output_file=file_metadata.csv" :: Clean up any existing output file to avoid appending to old data if exist "%output_file%" del "%output_file%" :: Write the CSV header row echo "Filename","Full File Path","Last Modified Date/Time" > "%output_file%" :: Recursively loop through every file in the current directory and subdirectories for /r %%f in (*) do ( :: Extract the required metadata set "file_name=%%~nxf" set "full_path=%%~f" set "mod_date=%%~tf" :: Escape double quotes in filenames/paths for valid CSV formatting set "file_name=!file_name:"=""!" set "full_path=!full_path:"=""!" :: Append the row to the CSV echo "!file_name!","!full_path!","!mod_date!" >> "%output_file%" ) echo Success! Metadata saved to %output_file% endlocal
Key Details Explained:
@echo off: Hides command prompts from being printed, keeping your output clean.setlocal enabledelayedexpansion: Lets us handle special characters (spaces, quotes, exclamation marks) in filenames without breaking the script.for /r %%f in (*): Recursively scans every file in the root directory and all its subfolders.- Variable Extensions:
%%~nxf: Grabs just the filename (including its extension, e.g.,report.pdf).%%~f: Gets the full absolute path of the file (e.g.,C:\Projects\2024\report.pdf).%%~tf: Extracts the file's last modified date and time (format matches your system's regional settings).
- CSV Escaping: The lines that replace
"with""ensure that any quotes in filenames don't break the CSV structure—this is crucial for tools like Excel to read the file correctly.
Just save this as a .bat file in your root directory, run it, and you'll get a CSV with all three pieces of data you need!
内容的提问来源于stack exchange,提问作者Susy Martin Anderson
相关产品推荐
相关产品推荐

