Open a random bookmark from a datafile
Posted: 19 Nov 2023 23:41
I'm currently working on a batch script that should open a random link from my bookmarks data file, bookmarks.txt. Each pair of lines in the file consists of a URL on odd-numbered lines, followed by a description on the next even line. I've made progress with the script with the help of ChatGPT, but it seems there might be an issue. Could you please assist me in troubleshooting and refining the script? Thank you.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "file_path=C:\mydrive\bookmarks.txt"
set /a count=0
set /a line_number=0
:: Count the number of lines in the file
for /f %%a in ('type "%file_path%" ^| find /c /v ""') do set /a count=%%a
:: Generate a random line number
set /a "random=!random! %% count + 1"
:: Loop through the file to find the random pair
for /f "delims=" %%b in ('type "%file_path%" ^| findstr /n "^"') do (
set /a "line_number+=1"
set "line=%%b"
set "line=!line:*:=!"
if !line_number! equ !random! (
set /a "next_line=line_number+1"
:: Extract URL and Description
for /f "skip=%next_line% tokens=1,* delims=," %%c in ('type "%file_path%"') do (
set "url=%%c"
set "description=%%d"
:: Open the URL
start "" "!url!"
:: Display the Description
echo Description: !description!
exit /b
)
)
)