Computer Hope

Microsoft => Microsoft DOS => Topic started by: LesterCrestV on July 28, 2020, 07:21:05 AM

Title: Read multiple specific lines in txt file - save each line into a variable -Batch
Post by: LesterCrestV on July 28, 2020, 07:21:05 AM
Hello everyone, I'm writing a batch file to organize and manage my movies downloaded on my computer. The idea is simple, I start with the main page with 2 options, either watch a movie, or add one to the library. The library is basically a txt file with all the information about each movie. The add movie feature asks users to insert movie name, year, description, and then opens a dialog box for the user to select the location where the movie is downloaded. it will then save the movie into movies.txt like this:

Code: [Select]
Name: Test 1
Year: Test 1
Desc: Test 1
Path: C:\Desktop\Movie1.mp4
----------------------------------------------------------------
Name: Test 2
Year: Test 2
Desc: Test 2
Path: C:\Desktop\Movie2.mp4
----------------------------------------------------------------
Name: Test 3
Year: Test 3
Desc: Test 3
Path: C:\Desktop\Movie3.mp4
----------------------------------------------------------------

My batch file already counts how many movies there is in the library, but I need it to:

1)Read the names of the movies
2)List only the movie names with a number next to each one
3)Let the user type the number he wants (for example if he chooses the second movie, he types 2)
4)Take the user to a new page showing the information of the movie(name,year,desc)
5)Have the option to directly start the movie using a command from the batch file ( for example, if the user types "w" the batch file will open the movie in whatever media player)

The library will be constantly updated by the user depending on the movies he has. So the program should be able to show all the movies in the library, and make a page automatically for each movie in the library which does what I wrote above.

I will add the whole code I have written since I don't care about anything except help. I know it's messy but I'm new to this thing.

Code: [Select]
@echo off
title DanFlix 1.3

:main
cls
echo -------------------------
echo       DanFlix 1.3
echo -------------------------
echo What do you want to do?
echo Press W to Watch
echo Press A to Add Movie/TV Show to database
echo Press S to go to Settings
echo Press Q to Quit
set /p act2=
if %act2% == W goto Watch
if %act2% == A goto add
if %act2% == S goto set
if %act2% == Q exit
if %act2% == w goto Watch
if %act2% == a goto add
if %act2% == s goto set
if %act2% == q exit


:set
cls
echo We are updating our program to support more Features
echo ----------------------------------------------------
echo                    Coming Soon
echo ----------------------------------------------------
PAUSE
goto main

:Watch
cls
echo Press M to watch a Movie
echo Press T to watch a TV Show
echo Press B to go back
set /p watchact=
if %watchact% == M goto movie
if %watchact% == T goto tvshow
if %watchact% == B goto main
if %watchact% == m goto movie
if %watchact% == t goto tvshow
if %watchact% == b goto main

:movie
echo Select a Movie from your Library
set file=Movies.txt
set /a moviecnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a moviecnt=%%a
set /a movienumber=%moviecnt%/5
echo You have %movienumber% Movies
echo ---------------------------------------------------------------------------
more Movies.txt
PAUSE


:tvshow
CLS
echo Select a TV Show from your Library
set file=TVShows.txt
set /a tvshowcnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a tvshowcnt=%%a
set /a tvshownumber=%tvshowcnt%/7
echo You have %tvshownumber% TV Shows
echo ---------------------------------------------------------------------------
more TVShows.txt
PAUSE



:add
cls
echo DanFlix allows users to add Movies and TV Shows to their local database for easier organization and viewing
echo ___________________________________________________________________________________________________________
echo Let's add a file to your database
echo Is your file a Movie or a TV Show?
echo If Movie: Enter M
echo If TV Show: Enter T
set /p addact=
if %addact% == M goto addmovie
if %addact% == T goto addtvshow
if %addact% == m goto addmovie
if %addact% == t goto addtvshow

:addmovie
echo ---------------------
echo    Adding Movie
echo ---------------------
echo What is the name of the movie?
set /p moviename=
cls
echo ---------------------
echo    Adding Movie
echo ---------------------
echo Movie Name: %moviename%
echo What is the release year of the movie?
set /p movieyear=
cls
echo ---------------------
echo    Adding Movie
echo ---------------------
echo Movie Name: %moviename%
echo Movie Year: %movieyear%
echo Insert a Movie description?
set /p moviedesc=
cls
echo ---------------------
echo    Adding Movie
echo ---------------------
echo Movie Name: %moviename%
echo Movie Year: %movieyear%
echo Description: %moviedesc%
echo Please Find Your Video File, make sure you don't move it in the future
PAUSE
cls
goto filedialogmovie

:filedialogmovie
FOR /F "tokens=*" %%A IN ('OpenFileBox.exe ^|^| ECHO Error^& IF ERRORLEVEL 2 ECHO Cancel') DO SET File=%%A
IF "%File%"=="Cancel" (
    ECHO You clicked "Cancel"
    PAUSE
    goto main
) ELSE (
    IF "%File%"=="Error" (
        ECHO An error occurred
    ) ELSE (
        ECHO You selected "%File%"
    )
)

PAUSE

(echo=Name: %moviename%) >> Movies.txt
(echo=Year: %movieyear%) >> Movies.txt
(echo=Desc: %moviedesc%) >> Movies.txt
(echo=Path: %File%) >> Movies.txt
(echo=----------------------------------------------------------------) >> Movies.txt
PAUSE
cls
echo ---------------------
echo     Movie Added
echo ---------------------
PAUSE
echo Press A to add another movie or tv show
echo Press G to go back to the main menu
set /p addedmovieact=
if %addedmovieact% == A goto add
if %addedmovieact% == G goto main
if %addedmovieact% == a goto add
if %addedmovieact% == g goto main




:addtvshow
echo ---------------------
echo    Adding TV Show
echo ---------------------
echo What is the name of the TV Show?
set /p tvshowname=
cls
echo ---------------------
echo    Adding TV Show
echo ---------------------
echo TV Show Name: %tvshowname%
echo What is the start and end year of the TV Show?
echo Example: 2015-2020 or 2020- if still renewing?

set /p tvshowyear=
cls
echo ---------------------
echo    Adding TV Show
echo ---------------------
echo TV Show Name: %tvshowname%
echo TV Show Year: %tvshowyear%
echo Which season are you adding?
set /p tvshowseason=
cls
echo ---------------------
echo    Adding TV Show
echo ---------------------
echo TV Show Name: %tvshowname%
echo TV Show Year: %tvshowyear%
echo TV Show Season: %tvshowseason%
echo How many episodes does this season have?
set /p tvshowepisodes=
cls
echo ---------------------
echo    Adding TV Show
echo ---------------------
echo TV Show Name: %tvshowname%
echo TV Show Year: %tvshowyear%
echo TV Show Season: %tvshowseason%
echo TV Show Season: %tvshowepisodes%
echo Insert a TV Show description?
set /p tvshowdesc=
cls
echo ---------------------
echo    Adding TV Show
echo ---------------------
echo TV Show Name: %tvshowname%
echo TV Show Year: %tvshowyear%
echo TV Show Season: %tvshowseason%
echo TV Show Season: %tvshowepisodes%
echo TV Show Description: %tvshowdesc%
echo --------------------------------------------------------------
PAUSE
cls
echo Please Find Your Video File, make sure you don't move it in the future
PAUSE
cls
goto filedialogtvshow

:filedialogtvshow
FOR /F "tokens=*" %%A IN ('OpenFolderBox.exe ^|^| ECHO Error^& IF ERRORLEVEL 2 ECHO Cancel') DO SET FileTV=%%A
IF "%FileTV%"=="Cancel" (
    ECHO You clicked "Cancel"
    PAUSE
    goto main
) ELSE (
    IF "%FileTV%"=="Error" (
        ECHO An error occurred
    ) ELSE (
        ECHO You selected "%FileTV%"
    )
)

PAUSE



(echo=%tvshowname%) >> TVShows.txt
(echo=%tvshowyear%) >> TVShows.txt
(echo=Season %tvshowseason%) >> TVShows.txt
(echo=%tvshowepisodes% Episodes) >> TVShows.txt
(echo=%tvshowdesc%) >> TVShows.txt
(echo=%FileTV%) >> TVShows.txt
(echo=----------------------------------------------------------------) >> TVShows.txt
PAUSE
cls
echo ---------------------
echo    TV Show Added
echo ---------------------
PAUSE
echo Press A to add another movie or tv show
echo Press G to go back to the main menu
set /p addedtvshowact=
if %addedtvshowact% == A goto add
if %addedtvshowact% == G goto main
if %addedtvshowact% == a goto add
if %addedtvshowact% == g goto main

Note, everything is basically done, except the watch feature. The watch feature goes in the :movie and :tvshow. The :movie should show the movies in the libraries with the ability for the user to click the specific number to open the movie's page and view the movie from there.

Thanks everyone in advance.

And, if any of you knows an application for android or windows that lets me do what I'm trying to do in batch, I would really love a suggestion. I've been looking for an app that lets me make a library and add my movies to it and watch the movies, but I cant find any. I dont want a normal media player, I want an app that lists the movies and lets me open the media player through it or whatever.
Title: Re: Read multiple specific lines in txt file - save each line into a variable -Batch
Post by: Sidewinder on July 29, 2020, 10:09:23 AM
This is written as a stand alone batch file, but you can remove any duplicate code and copy it into your existing code. Batch language does not support collections, so this steals an idea from Classic Rexx using a stem.tail construct.

Code: [Select]
@echo off
setlocal enabledelayedexpansion
set x=0
echo.

for /f "tokens=1* delims=:" %%i in (g:\wfc\testlib\movies.txt) do (
  if %%i==Name (
    call set /a x+=1
    call set name.%%x%%=%%j
    call echo !x!. %%j
  )

  if %%i==Year call set year.%%x%%=%%j
  if %%i==Desc call set desc.%%x%%=%%j
  if %%i==Path call set folder.%%x%%=%%j
)

echo.
set /p sel=Enter Selection:
if %sel% EQU 0 goto :eof
if %sel% GTR !x! goto error
if %sel% LSS 0 goto error

cls
echo You Selected:
echo    Name: !name.%sel%!
echo    Year: !year.%sel%!
echo    Desc: !desc.%sel%!
echo.

set /p view=Type "w" to Watch Movie:
if %view%==w !folder.%sel%!
goto :eof


:error
  echo Selection Out Of Range

You will need to change line 6 to point to your movie file, but other than that you should be good to go.

Happy Viewing  8)