Home > other >  Create multiple URL shortcuts from a text file using a batch script
Create multiple URL shortcuts from a text file using a batch script

Time:01-20

Summary: I have a set of reports I must to run, and would like to create links to each report online. The problem I'm having is how to make a batch file which can successfully loop through the file containing my URLs, and make an internet shortcut for each URL with a name based on data from URL itself.

Example URL exactly as shown, each URL is a line in a text file, (one report per day of the year):

<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-02;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-03;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>

Breaking the URL down to understand the static vs dynamic data and how to manipulate it.

  • https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date= (static data)
  • 2023-01-01 (dynamic data - format YYYY-mm-DD)
  • ;end_date= (static data)
  • 2023-01-31 (dynamic data - format YYYY-mm-DD)
  • ;vehicle_ids=1103007;report_id=25;report_type=normal (static data)

I'm trying to make the URL shortcut names based off the line itself for example, with the URL https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal, I'd like to create a shortcut in directory named: C:\Users\UserName\OneDrive\Desktop\Folder for the links\01-01-2023 mileage report.URL. The filename of 01-01-2023 being dynamic based on the date stated in the URL itself. Also I found out the hard way, the other day, this computer "%UserProfile%\Desktop\Anywhere Else" is not a valid directory, not sure if this is a new Windows 11 trait, (moved from Win 7), or a OneDrive issue. Either way, any old code I had that used the %UserProfile% variable is needing re-done, or I need to make a new environment variable.

Here are my issue(s):

    • I have no experience using for looping command, let alone the restrictions of batch when using special characters. I thought I'd best ask before goofing something up terribly (special characters in the text file containing all the URLS).
    • The URL can vary on length ,so I'm not sure how to extract the month/day/year to use in the filename correctly.
    • Batch is the only language I have any experience with, and that is limited too. So whilst I'm sure there are much better options to do the task I'm wanting, I don't know them, to try them.

What I have done:

I used spreadsheet to break the URL down, modify the dates for the entire year, concatenate the data in a manner which made a proper working URL for each day of the year, added a column for the filename, and Echo @Echo Off> To the filename, Echo Start (the URL)>> To the filename, Echo Exit>> To the filename. At which point, I was able to copy the crude columns of code from the spreadsheet, and paste them into the Command Prompt, which created a batch file for each URL, (file name was based on date) like so:

@Echo OFF
Start "" "https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-
01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal"
Exit

Making a working batch file for each link but surely not the right way to do it. (It's not the first time I've used a spreadsheet to break apart a directory or URL and re-constitute it for a batch file).

However I feel making actual internet shortcuts would be a much better way to do this across all forms of devices, (I think one of the people who may look at the reports from time to time use MacOS, and I'm assuming a batch file to "start" the URL won't work on their Mac, let alone not being the most friendly way of doing the job.

What my mind is telling me so far to do, (this is the batch file in my mind at the moment):

@Echo Off
CLS
REM Batch file create URLs based on text file
REM Filenames based on URL data from line in file
REM File containing URLs C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt
REM Save directory C:\Users\UserName\OneDrive\Desktop\Report Creation\All URLS Here
for /F "tokens=*" %%A in (C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt)      
do (Echo [InternetShortcut] > "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL" 
echo URL=%%A >> "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL" 
echo IconFile=C:\Program Files\Google\Chrome\Application\chrome.exe >>     
"C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo IconIndex=0 >> "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
)
Exit

But I'm lost on how to extract the mm-DD-YYYY from the URL in the text file, and set it as the "option1", as I feel there is need for a second for command somewhere, to make this possible.

I have not ran my potential code, as shown above, through this system yet as I'm not sure how to make the filenames based on the data in the URL of the file filenames.txt, and I'm not sure this is even possible.

I did break the URLs down (365x) in spreadsheet, re-constitute them with proper dates, and make a column for filenames based on those dates, where I was able to further concatenate lines like Echo @Echo Off>FileName(s).bat, Echo Start "" "The URL with updated dates">>FileName(s).bat and Echo Exit>>FileName(s).bat, effectively creating a crude batch file containing the proper link, and having a correct name based on the date of the URL via the Command Prompt. However, making 365 batch files per year, and being locked to only Windows OS, not able to run those batch files on my phone either, makes them half as useful as if they were a .url file which any device can open.

CodePudding user response:

Here is how I would think, based upon your provided text file content, and your unattempted code, it could be done:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "URLFile=C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt"
Set "SaveDir=C:\Users\UserName\OneDrive\Desktop\Report Creation"

For /F "UseBackQ Delims=<>" %%G In ("%URLFile%") Do (
    For /F "Tokens=3 Delims=;=" %%H In ("%%~G") Do (
        Echo [InternetShortcut]
        Echo URL="%%~G"
        Echo IconFile="C:\Program Files\Google\Chrome\Application\chrome.exe"
        Echo IconIndex=0
    ) 1>"%SaveDir%\%%H.url"
)

As you had assumed, an additional for loop was needed.


Please be aware however, that it is not very likely that your phone, or a Mac will have a copy of chrome.exe in the location stipulated in the IconFile entry. So you may be better advised to miss out those two Icon* keys, or at least their values.

  • Related