使用批处理脚本从文本文件批量创建带日期命名的URL快捷方式
Solution: Create Date-Named .URL Shortcuts from a Text File of URLs
Let's fix your batch script to extract the start_date from each URL, reformat it, and use it as the shortcut filename. Here's a complete, tested solution that addresses all your pain points:
Complete Batch Script
@Echo Off SetLocal EnableDelayedExpansion CLS REM --- Configuration (Update these paths to match your setup!) --- Set "URL_FILE=C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt" Set "SAVE_DIR=C:\Users\UserName\OneDrive\Desktop\Report Creation\All URLS Here" Set "SHORTCUT_SUFFIX= mileage report" REM --------------------------------------------------------------- REM Create the save directory if it doesn't already exist If Not Exist "%SAVE_DIR%" MD "%SAVE_DIR%" REM Process each URL line in the input text file for /F "tokens=* delims=" %%A in ('Type "%URL_FILE%" ^| Find /V ""') do ( REM Step 1: Extract the raw start_date value from the URL Set "URL=%%A" Set "DATE_PART=!URL:*start_date=!" &REM Remove everything before "start_date" For /F "tokens=1 delims=;" %%B in ("!DATE_PART!") do Set "RAW_DATE=%%B" &REM Grab everything before the next ";" REM Step 2: Convert YYYY-mm-DD to mm-DD-YYYY format For /F "tokens=1,2,3 delims=-" %%C in ("!RAW_DATE!") do ( Set "FORMATTED_DATE=%%D-%%E-%%C" &REM Swap year with month/day ) REM Step 3: Create the .URL shortcut file Set "SHORTCUT_NAME=!FORMATTED_DATE!!SHORTCUT_SUFFIX!.URL" Echo [InternetShortcut] > "%SAVE_DIR%\!SHORTCUT_NAME!" Echo URL=%%A >> "%SAVE_DIR%\!SHORTCUT_NAME!" Echo IconFile=C:\Program Files\Google\Chrome\Application\chrome.exe >> "%SAVE_DIR%\!SHORTCUT_NAME!" Echo IconIndex=0 >> "%SAVE_DIR%\!SHORTCUT_NAME!" REM Optional: Print progress to the console Echo Created shortcut: !SHORTCUT_NAME! ) Echo. Echo All shortcuts created successfully! Pause Exit /B
How This Solves Your Problems
1. Batch For Loop & Special Character Handling
SetLocal EnableDelayedExpansion: This fixes variable substitution issues inside loops (critical when working with dynamic values like URLs and dates).for /F "tokens=* delims=": Ensures we read the entire line of the URL, even if it contains spaces or special characters.Type "%URL_FILE%" ^| Find /V "": Skips any empty lines in your URL text file to avoid errors.
2. Extracting the Date from Variable-Length URLs
Set "DATE_PART=!URL:*start_date=!": Uses batch's string substitution to strip off everything in the URL before thestart_datekeyword.- The second
for /Fcommand splits the remaining text at the;character, grabbing just theYYYY-mm-DDvalue we need.
3. Converting Date Format
- The final
for /Fcommand splits the raw date by-into three parts: year (%%C), month (%%D), day (%%E). We then reorder these to create themm-DD-YYYYformat you need.
How to Use
- Copy the script into Notepad.
- Update the
URL_FILEandSAVE_DIRpaths to match your actual file locations. - Save the file as
CreateReportShortcuts.bat(make sure "Save as type" is set to "All Files"). - Double-click the batch file to run it – your .URL shortcuts will appear in the specified directory!
内容的提问来源于stack exchange,提问作者TruckDriverOnPC
相关产品推荐
相关产品推荐

