Open a random bookmark from a datafile

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bonaqua
Posts: 1
Joined: 19 Nov 2023 23:34

Open a random bookmark from a datafile

#1 Post by bonaqua » 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
        )
    )
)


Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Open a random bookmark from a datafile

#2 Post by Aacini » 20 Nov 2023 00:55

Mmm... Practically every line in this code have some type of error or bad construction... However, have not much sense to explain to you these problems because you have NOT wrote the code!

I think this code do what you want:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "file_path=C:\mydrive\bookmarks.txt"

rem Count the number of lines in the file
for /F "tokens=2 delims=:" %%a in ('find /C /V "" "%file_path%"') do set "count=%%a"

rem Generate an even random line number
set /A "random_line=(%random% %% count) & ~1"

rem Get the number of lines to skip in the file
set "skip="
if %random_line% gtr 0 set "skip=skip=%random_line%"

rem Get the random pair from line number
set "URL="
for /F "usebackq %skip% delims=" %%a in ("%file_path%") do (
   if not defined URL (
      set "URL=%%a"
   ) else (
      set "Description=%%a"
      goto break
   )
)
:break

rem Open the URL
ECHO start "" "%url%"

rem Display the description
echo Description: %Description%
Antonio

PS: On a very personal note, I don't understand what the purpose of saying that "ChatGPT wrote the code" is. IMHO ChatGPT almost never writes correct code. If I were you, it would be very awkward for me to say that I didn't wrote the code, but I'm asking for help to fix it...

Post Reply