Clean-up code and some help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nickodemos
Posts: 16
Joined: 29 Jan 2013 08:17

Clean-up code and some help

#1 Post by nickodemos » 26 Mar 2016 09:01

First thing, I am not very good at writing it on my own but, I tend to get what I need to work after a great deal of trial and error. Below will be the code that I was able to get working as well as I can but one part keeps refusing to work. The rename a file to remove part of a file name.

What I have here is a script to create a torrent for specific sites (I am into classic movies of the 1950's and older) and it renames them with that sites initials in the new torrent. Right now it will rename the file:

Donald And Pluto (1936).avi.torrent ----> Donald And Pluto (1936). -- ILC.torrent

Code: Select all

REN *mkv.torrent *.a || REN *avi.torrent *.a || REN *mp4.torrent *.a
REN *.a *." -- ILC
REN *." -- ILC" *." -- ILC.torrent


For personal reasons I would like to remove the first period out of the name but have been having issues with it. I tried using this code and while it would show it renamed in the dos window it never actually did anything.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set deletestring=.avi
for /f "delims==" %%F in ('dir /b /l *.torrent ^| find "%deletestring%"') do (
   set oldfilename=%%F
   set newfilename=!oldfilename:%deletestring%=!
   echo Ren "!oldfilename!" "!newfilename!"
   )


While the code below works well (even though it adds that first period in the name) I would like some help on how to clean it up if possible. Try and make the script a bit shorter so I can let others I know use it. I am not sure how I could do this so I am here asking for help on it.

Code: Select all

@ECHO OFF

:INTRO
COLOR 8F
CLS
REM Set to where you have mktorrent installed.
SET MKT=C:\Keydrive\Media\MKTorrent\mktorrent.exe
REM Add your trackers here.
SET AT=-a "http://animetorrents.me/announce.php" -c AnimeTorrents -p -l 17
SET CG=-a "http://cinemageddon.net/announce.php" -c Cinemageddon -p -l 20
SET KG=-a "http://karagarga.in/tracker.php/PersonalHashKey/announce" -c Karagara -p
SET SC=-a "http://www.secret-cinema.net/announce.php?" -c SecretCinema -p
SET BTN=-a "http://landof.tv/PersonalHashKey/announce" -c BroadcastTheNet -p
SET ILC=-a "http://www.iloveclassics.com/announce.php" -c ILoveClassics -p
SET PTP=-a "http://please.passthepopcorn.me:2710/PersonalHashKey/announce" -c PassThePopcorn -p
SET THC=-a "http://horrorcharnel.kicks-ass.org/announce.php" -c TheHorrorChannel -p -l 19
SET TVV=-a "http://tracker.tv-vault.me:34000/PersonalHashKey/announce" -c TV-Vault -p
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
REM You can change the list of trackers but,
REM make sure they match the counter below.
ECHO                          ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
ECHO                          ³  1. ILC                     ³
ECHO                          ³  2. KG                      ³
ECHO                          ³  3. PTP                     ³
ECHO                          ³  4. SC                      ³
ECHO                          ³  5. TVV                     ³
ECHO                          ³  6. BTN                     ³
ECHO                          ³  7. AT                      ³
ECHO                          ³  8. CG                      ³
ECHO                          ³  9. THC                     ³
ECHO                          ³ 10. EXIT                    ³
ECHO                          ³ 11. SS                      ³
ECHO                          ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
 
set /a counter=0
set choice=
set /p choice=The torrent you want to make:
set /a counter= %counter%+%choice%
if %counter% ==1 (goto :ILC)
if %counter% ==2 (goto :KG)
if %counter% ==3 (goto :PTP)
if %counter% ==4 (goto :SC)
if %counter% ==5 (goto :TVV)
if %counter% ==6 (goto :BTN)
if %counter% ==7 (goto :AT)
if %counter% ==8 (goto :CG)
if %counter% ==9 (goto :THC)
if %counter% ==10 (goto :EOF)
if %counter% ==11 (goto :SS)
 
ECHO.
ECHO "%choice%" is not valid...please try again
pause
goto :INTRO
 
:EOF
EXIT

REM Modify these as needed. They can be in any order.
:ILC
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %ILC% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- ILC.torrent"
goto intro
 
:PTP
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %PTP% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- PTP.torrent"
goto intro
 
:KG
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %KG% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- KG.torrent"
goto intro
 
:TVV
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %TVV% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- TVV.torrent"
goto intro
 
:BTN
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %BTN% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- BTN.torrent"
goto intro
 
:AT
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %AT% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- AT.torrent"
goto intro
 
:THC
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %THC% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- THC.torrent"
goto intro
 
:SC
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %SC% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- SC.torrent"
goto intro
 
:CG
FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% %CG% "%%I"
for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- CG.torrent"
goto intro

:SS
FOR %%I IN (*.MP4 *.mkv *.avi) DO CALL \\NICKODEMOS-PC\Users\nickodemos\AppData\Roaming\Microsoft\Windows\SendTo\_SShots.bat "%%I"


Edited to reflect the help given.
Last edited by nickodemos on 26 Mar 2016 10:59, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clean-up code and some help

#2 Post by foxidrive » 26 Mar 2016 09:24

Someone might mosey along with a simpler technique using wildcards, but this should work too.
It will process all torrents in the folder.

Code: Select all

for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb.torrent"

nickodemos
Posts: 16
Joined: 29 Jan 2013 08:17

Re: Clean-up code and some help

#3 Post by nickodemos » 26 Mar 2016 10:55

Either way that helped me out a bit. Was able to eliminate about 32 lines of unneeded code with that.

Sad thing is I can see easily how you got there, I just could not see it myself. I tried a few versions like that but looks like I gave up to easily.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clean-up code and some help

#4 Post by foxidrive » 26 Mar 2016 12:37

Test this on some sample files: I was confused initially with the names, so didn't examine the code closely.

Code: Select all

@ECHO OFF

:INTRO
COLOR 8F
CLS
REM Set to where you have mktorrent installed.
SET "MKT=C:\Keydrive\Media\MKTorrent\mktorrent.exe"

for /L %%a in (1,1,10) do ECHO.

ECHO                          ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
ECHO                          ³  1. ILC                     ³
ECHO                          ³  2. KG                      ³
ECHO                          ³  3. PTP                     ³
ECHO                          ³  4. SC                      ³
ECHO                          ³  5. TVV                     ³
ECHO                          ³  6. BTN                     ³
ECHO                          ³  7. AT                      ³
ECHO                          ³  8. CG                      ³
ECHO                          ³  9. THC                     ³
ECHO                          ³ 10. EXIT                    ³
ECHO                          ³ 11. SS                      ³
ECHO                          ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

for /L %%a in (1,1,7) do ECHO.

REM You can change the list of trackers
REM but make sure they match the list above.

set "torrent="
set "choice="
set /p "choice=The torrent you want to make: "
if "%choice%"=="1"  set torrent=ILC -a "http://www.iloveclassics.com/announce.php" -c ILoveClassics -p
if "%choice%"=="2"  set torrent=KG  -a "http://karagarga.in/tracker.php/PersonalHashKey/announce" -c Karagara -p
if "%choice%"=="3"  set torrent=PTP -a "http://please.passthepopcorn.me:2710/PersonalHashKey/announce" -c PassThePopcorn -p
if "%choice%"=="4"  set torrent=SC  -a "http://www.secret-cinema.net/announce.php?" -c SecretCinema -p
if "%choice%"=="5"  set torrent=TVV -a "http://tracker.tv-vault.me:34000/PersonalHashKey/announce" -c TV-Vault -p
if "%choice%"=="6"  set torrent=BTN -a "http://landof.tv/PersonalHashKey/announce" -c BroadcastTheNet -p
if "%choice%"=="7"  set torrent=AT  -a "http://animetorrents.me/announce.php" -c AnimeTorrents -p -l 17
if "%choice%"=="8"  set torrent=CG  -a "http://cinemageddon.net/announce.php" -c Cinemageddon -p -l 20
if "%choice%"=="9"  set torrent=THC -a "http://horrorcharnel.kicks-ass.org/announce.php" -c TheHorrorChannel -p -l 19
if "%choice%"=="10" goto :EOF
if "%choice%"=="11" (FOR %%I IN (*.MP4 *.mkv *.avi) DO CALL "\\NICKODEMOS-PC\Users\nickodemos\AppData\Roaming\Microsoft\Windows\SendTo\_SShots.bat" "%%I") & goto :EOF

if not defined torrent (ECHO. & ECHO "%choice%" is not valid...please try again & echo. & pause & goto :INTRO)

for %%a in (*.mp4 *.mkv *.avi) do (
   for /f "tokens=1,*" %%b in ("%torrent%") do "%MKT%" %%c "%%a"
   ren "%%a.torrent" "%%~na -- %%b.torrent"
)
goto :intro

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Clean-up code and some help

#5 Post by penpen » 26 Mar 2016 13:00

You may also want to connect the menue with the choice numbers:

Code: Select all

@ECHO OFF
setlocal enableExtensions enableDelayedExpansion
COLOR 8F
REM Set to where you have mktorrent installed.
SET MKT=C:\Keydrive\Media\MKTorrent\mktorrent.exe
REM Add your trackers here.
SET AT=-a "http://animetorrents.me/announce.php" -c AnimeTorrents -p -l 17
SET CG=-a "http://cinemageddon.net/announce.php" -c Cinemageddon -p -l 20
SET KG=-a "http://karagarga.in/tracker.php/PersonalHashKey/announce" -c Karagara -p
SET SC=-a "http://www.secret-cinema.net/announce.php?" -c SecretCinema -p
SET BTN=-a "http://landof.tv/PersonalHashKey/announce" -c BroadcastTheNet -p
SET ILC=-a "http://www.iloveclassics.com/announce.php" -c ILoveClassics -p
SET PTP=-a "http://please.passthepopcorn.me:2710/PersonalHashKey/announce" -c PassThePopcorn -p
SET THC=-a "http://horrorcharnel.kicks-ass.org/announce.php" -c TheHorrorChannel -p -l 19
SET TVV=-a "http://tracker.tv-vault.me:34000/PersonalHashKey/announce" -c TV-Vault -p
set "choice=0"
for %%a in ("ILC " "KG  " "PTP " "SC  " "TVV " "BTN " "AT  " "CG  " "THC " "EXIT" "SS  ") do set /a "choice+=1" & set "choice[!choice!]=%%~a"
set /A "exit=choice-1", "ss=choice"
set "message="

:INTRO
cls
for /L %%a in ( 1, 1, 10) do echo(
ECHO                          ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
for /L %%a in ( 1, 1,  9) do echo(                         ³   %%~a. !choice[%%~a]!                   ³
for /L %%a in (10, 1, 11) do echo(                         ³  %%~a. !choice[%%~a]!                   ³
ECHO                          ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
for /L %%a in ( 1, 1,  6) do echo(
echo(!message!
set "message="
set "choice="
set /p "choice=The torrent you want to make: "

if "%choice%" == "%exit%" (
   rem nop
) else if "%choice%" == "%ss%" (
   FOR %%I IN (*.MP4 *.mkv *.avi) DO CALL \\NICKODEMOS-PC\Users\nickodemos\AppData\Roaming\Microsoft\Windows\SendTo\_SShots.bat "%%I"
) else if defined choice[%choice%] for %%c in ("!choice[%choice%]: =!") do (
   FOR %%I IN (*.MP4 *.mkv *.avi) DO %MKT% !%%~c! "%%~I"
   for %%a in (*.???.torrent) do for %%b in ("%%~na") do ren "%%a" "%%~nb -- %%~c.torrent"
   goto :intro
) else (
   set "message=Unknown choice: "!choice!""
   goto :intro
)
endlocal
The advantage is, that you could easily extend the menue, or change the order of the menue items.


penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clean-up code and some help

#6 Post by foxidrive » 26 Mar 2016 13:16

I've been fiddling with an automated menu too, though not as straight forward.

Code: Select all

@ECHO OFF
COLOR 8F
:INTRO
CLS
REM Set to where you have mktorrent installed.
SET "MKT=C:\Keydrive\Media\MKTorrent\mktorrent.exe"

REM You can change the list of trackers below
call :printmenu
set "torrent="
set "choice="
set /p "choice=Choose the torrent you want to make: "
:getmenuchoices

if "%choice%"=="1"  set torrent=ILC -a "http://www.iloveclassics.com/announce.php" -c ILoveClassics -p
if "%choice%"=="2"  set torrent=KG  -a "http://karagarga.in/tracker.php/PersonalHashKey/announce" -c Karagara -p
if "%choice%"=="3"  set torrent=PTP -a "http://please.passthepopcorn.me:2710/PersonalHashKey/announce" -c PassThePopcorn -p
if "%choice%"=="4"  set torrent=SC  -a "http://www.secret-cinema.net/announce.php?" -c SecretCinema -p
if "%choice%"=="5"  set torrent=TVV -a "http://tracker.tv-vault.me:34000/PersonalHashKey/announce" -c TV-Vault -p
if "%choice%"=="6"  set torrent=BTN -a "http://landof.tv/PersonalHashKey/announce" -c BroadcastTheNet -p
if "%choice%"=="7"  set torrent=AT  -a "http://animetorrents.me/announce.php" -c AnimeTorrents -p -l 17
if "%choice%"=="8"  set torrent=CG  -a "http://cinemageddon.net/announce.php" -c Cinemageddon -p -l 20
if "%choice%"=="9"  set torrent=THC -a "http://horrorcharnel.kicks-ass.org/announce.php" -c TheHorrorChannel -p -l 19

if defined getmenuchoices goto :EOF
if "%choice%"=="10" (FOR %%I IN (*.MP4 *.mkv *.avi) DO CALL "\\NICKODEMOS-PC\Users\nickodemos\AppData\Roaming\Microsoft\Windows\SendTo\_SShots.bat" "%%I") & goto :EOF
if "%choice%"==""   goto :EOF
if not defined torrent (ECHO. & ECHO "%choice%" is not valid...please try again & pause & goto :INTRO)

for %%a in (*.mp4 *.mkv *.avi) do (
   for /f "tokens=1,*" %%b in ("%torrent%") do "%MKT%" %%c "%%a"
   ren "%%a.torrent" "%%~na -- %%b.torrent"
)
goto :intro


:printmenu

for /L %%a in (1,1,10) do ECHO.
ECHO                          ┌─────────────────────────────┐
ECHO                          │ Press ENTER alone to QUIT   │
ECHO                          │                             │
rem This uses the list of trackers above to print them in the menu
setlocal enabledelayedexpansion
set "getmenuchoices=1"
for /L %%a in (1,1,9) do (
   set "choice=%%a"
   call :getmenuchoices
   for /f %%b in ("!torrent!") do set "tracker=%%b  "
   ECHO                          │  %%a. !tracker:~0,3!                     │
   )
)
set "getmenuchoices="
endlocal
ECHO                          │ 10. SS                      │
ECHO                          └─────────────────────────────┘
for /L %%a in (1,1,7) do ECHO.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Clean-up code and some help

#7 Post by thefeduke » 30 Mar 2016 00:08

My favorite menu approach for small lists adapts OK to a list of nine or less:

Code: Select all

   @Echo Off
Rem. Author - John Andrejsons - accepting PM to user: thefeduke at DOStips forum
Rem. acknowledges code and influences from DOStips.com and its forum
:: Post subject: Re: Clean-up code and some help
    SetLOCAL EnableDelayedExpansion

Rem. Establish Color Functions
Rem. by Carlos - Post subject: Re: Color Function 23
Rem. http://www.dostips.com/forum/viewtopic.php?p=41155#p41155
    Set "ColorCall=Call :Color"
    Set "\n= \n"
    Set "ColorD=7"

   (
    Echo.ILC -a "http://www.iloveclassics.com/announce.php" -c ILoveClassics -p
    Echo.KG -a "http://karagarga.in/tracker.php/PersonalHashKey/announce" -c Karagara -p
    Echo.PTP -a "http://please.passthepopcorn.me:2710/PersonalHashKey/announce" -c PassThePopcorn -p
    Echo.SC -a "http://www.secret-cinema.net/announce.php?" -c SecretCinema -p
    Echo.TVV -a "http://tracker.tv-vault.me:34000/PersonalHashKey/announce" -c TV-Vault -p
    Echo.BTN -a "http://landof.tv/PersonalHashKey/announce" -c BroadcastTheNet -p
    Echo.AT -a "http://animetorrents.me/announce.php" -c AnimeTorrents -p -l 17
    Echo.CG -a "http://cinemageddon.net/announce.php" -c Cinemageddon -p -l 20
    Echo.THC -a "http://horrorcharnel.kicks-ass.org/announce.php" -c TheHorrorChannel -p -l 19
    )>"%Temp%\%~n0_Items.txt"

Rem. Presnt menu list
    Echo.
    Echo.This is a list of the available torrent codes: 
    Echo.
    Set "Ctr=0"
    for /f "usebackq tokens=1-1* delims= " %%H in (
        `Type "%Temp%\%~n0_Items.txt"`
    ) do (
        Set /A "Ctr+=1"
        Set "Item=%%H"                                                            r
        Set "SlashC=!SlashC!!Ctr!"
        Set "ColorD=A"
        %ColorCall% E "'!Ctr!' " !ColorD! "!Item!"%\n%
        Set "ColorD=7"
Rem.    Echo.H=%%H I=%%I
    )
    %ColorCall% E "'X' " F "Exit" 7 " without a selection "%\n%

Rem. Choose item from selection menu of above torrents.
    Echo(
    Set /A "MaxCtr=%Ctr%+1"
    Set "Ctr=0"

    If "%SlashC%" EQU "1" (
        Set "Chose=1"
        Echo.Automatic selection made.
        Goto :Chosen
    )
    %ColorCall% 7 "(Default:" E "'X'" 7 ") "
    Choice /C %SlashC%X /M "Choose from the torrent list above:" /T 20 /D X
    Set "Chose=%errorlevel%"
    IF .%Chose% EQU .%MaxCtr% GoTo :Xt
:Chosen
Rem. Process menu list
    for /f "usebackq tokens=1-1* delims= " %%H in (
        `Type "%Temp%\%~n0_Items.txt"`
    ) do (
        Set /A "Ctr+=1"
        IF '!Ctr!' EQU '%Chose%' (
            set "Torrent=%%H %%I"
            Set "ColorD=A"
            Echo(
            %ColorCall% F "Selected: " B "!Torrent!"%\n%
            Set "ColorD=7"
        )
    )

:Xt
    EndLocal
    Exit /B

:Color
:: v23c
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: This version works using Cmd /U
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
Echo(|(Pause >Nul &Findstr "^" >`)
Cmd /A /D /C Set /P "=." >>` <Nul
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do (
Cmd /A /D /C Set /P "=%%# %%#" <Nul >`.1
Copy /Y `.1 /B + `.1 /B + `.1 /B `.3 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.3 /B `.5 /B >Nul
Copy /Y `.1 /B + `.1 /B + `.5 /B `.7 /B >Nul
)
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
For %%_ In ("&" "|" ">" "<"
) Do Set "Text=!Text:%%~_=^%%~_!"
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! EndLocal
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Cmd /A /D /C Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Shift
Echo()
Shift
Shift
If ""=="%~1" Del ` `.1 `.3 `.5 `.7 `.dat &Goto :Eof
Goto :__Color


John A.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Clean-up code and some help

#8 Post by foxidrive » 30 Mar 2016 00:59

That's neat, John.

Post Reply