Script help . bat does not read spaced words

Asked

Viewed 110 times

2

How to make the script work?

Basically it reads names that contain in.tag Ex files: file1.tag, file2.tag... after reading these names it searches by name in one or another.txt file that contains all the information of the searched name, in turn it generates a Favorites.txt file by pulling all the information from the.txt file it read, every time incrementing on the next line, the problem I’m having is that when it tries to read space names it gives an error and does not write to the favorite.txt file, follows below the code and the error that occurs, if anyone can help me would be grateful.

Note. 1.: in the file Arcade.tag the name is so: 64street ai it searches in Arcade.txt by name and copies to Favorites.txt file like this: "64street;64th. Street - A Detective Story (World);Arcade;;1991;C.P. Brain;Briga de rua-Luta / 2.5D-Luta;1-2;;;;;;;;0.5;;;;;" that would be the correct format.

Note. 2.: Already in the next Sega Genesis.tag file the name is so: Duke Nukem 3D (Brazil) in which case you should copy this Duke Nukem 3D (Brazil);10 Super Jogos;Sega Genesis;;2002;Epyx;Compilação-Estratégia-Esporte;8+;;;;;;;;0.8; but the error happens and does not copy.

Error:

==========================================================
    AM favorites romlist generator script for Windows
==========================================================
- Backing up old Favorites.txt file
- Gathering list of favorites (tag files)
- Processing tag file: Arcade.tag
- Searching rom list: Arcade.txt for rom: 64street
- Favorites.txt romlist generated. 1 romlists read, 1 favorites written to disk
- Done

Add a display in Attract Mode and set its romlist to the newly created Favorites.txt
- Processing tag file: Sega Genesis.tag
- Searching rom list: Sega Genesis.txt for rom: Duke Nukem 3D (Brazil)

>FINDSTR: não foi possível abrir Nukem
>FINDSTR: não foi possível abrir 3D
>FINDSTR: não foi possível abrir (Brazil)
>FINDSTR: não foi possível abrir Nukem
>FINDSTR: não foi possível abrir 3D
>FINDSTR: não foi possível abrir (Brazil)
>FINDSTR: não foi possível abrir Nukem

Code Used:

@echo off 
setlocal EnableDelayedExpansion EnableExtensions 
rem     ================================================================================
rem     This script basically does the same thing as DM's favorites romlist generator
rem     but on Windows [Shouts to him for the pi version ;)].
rem     How it works:
rem     It grabs all the (tagged) favorites from every tag file, searches for them
rem     through all the romlists, then generates a romlist called Favorites.txt
rem     Written by Steve Sherrod, 05/20/17, as part of project HyperPie Expanded
rem     ================================================================================

rem     Default path (on windows). This should point to your attract modes romlists dir
cd "D:\AM-2.6.1\romlists\"

echo ==========================================================
echo     AM favorites romlist generator script for Windows
echo ==========================================================

echo - Backing up old Favorites.txt file

rem     remove the old Favorites.txt and create the backup
if exist Favorites.txt (
    move /y Favorites.txt Favorites.txt.backup
)

echo - Gathering list of favorites (tag files)

set /a numfavorites=0
set /a romlist=0

rem     loop through each tag file
for %%f in (*.tag) do (
    echo - Processing tag file: %%~nf.tag
    set /a numtagfiles=numtagfiles+1
    
    rem     loop through favorites stored in tag file
    for /f "tokens=*" %%a in ('type "%%~nf.tag"') do (
    
        set /a numfavorites=numfavorites+1
        set favorite=%%a

        echo - Searching rom list: %%~nf.txt for rom: %%a
        
        rem     loop through each rom file and parse favorited (tagged) roms
        for /f "tokens=*" %%s in ('type "%%~nf.txt"') do (
            set /a romlist=romlist+1
            set str=%%s
            rem     Search current line (for current favorite substring) and if found, append it to Favorites.txt
            echo."!str!" | findstr /c:%%a>nul && (
                echo "!str!">>"Favorites.txt"
            )
        )
    )

echo - Favorites.txt romlist generated. !numtagfiles! romlists read, !numfavorites! favorites written to disk
echo - Done
echo.
echo Add a display in Attract Mode and set its romlist to the newly created Favorites.txt
)

  • 1

    In this part: echo."! str!" | findstr /c: %a>nul && ( tries to quote %a -> "%a"

  • I know this does not answer the question but, pq not replace this batch with a power shell script that has thousands of more functions and would make this job a lot easier?

  • Show!!! you found the problem in the vein, thank you very much for the help, about the poweshell I have no notion of it, could you kindly give me an example?

1 answer

1


@echo off 

setlocal EnableDelayedExpansion 
set/a "_tag=0,_num=0" && pushd "D:\AM-2.6.1\ROMlists" 

echo==========================================================
echo=    AM Favorites ROMlist Generator Script for Windows     
echo==========================================================

if not exist .\favorites.txt (
     set/p "'= - Cretating .\Favorites.txt File.."<nul & echo\
   ) else 2>nul (set/p "'= - Backing up your old Favorites.txt: "<nul
     move/y .\Favorites.txt .\Favorites.txt.backup >nul && echo\Done^!!)
      
echo/- Gathering list of your Favorites: Tags-Files
for %%G in (*.tag)do set/a "_tag+=1" && for /f useback^delims^= %%i in (`
more "%%~G"`)do set/p "'=- ROM Found in File %%~nxG: %%~i"<nul & echo= & ( 
     <con: find "%%~i;" <"%%~nG.txt" >>".\Favorites.txt" && set/a "_num+=1")

set/a "_num+=10000,_tag+=10000" && <con: call echo/- Romlist Files [tag=txt]: !_tag:~-3!
echo/- Favorites added to Disc: !_num:~-3! & echo/- ROMlist .\Favorites.txt: New && popd
endlocal & echo/- is Done!.. & timeout 3|findstr /ec:\.\Favorites\.txt "%~f0" & goto:EOF

========================================================================================
- Add a display in Attract Mode and set its ROMlist to the newly created .\Favorites.txt
========================================================================================
  This script basically does the same thing as DM's Favorites ROMlist generator but 
  on Windows [ Shouts to him for the pi version d;) ].
                                                         
  How it works:
  It grabs all the (tagged) favorites from every tag file, searches for them through
  all the ROMlists, then generates a ROMlist called Favorites.txt Written by Steve 
  Sherrod, 05/20/17, as part of project HyperPie Expanded
========================================================================================
  • Exit:
=========================================================
    AM Favorites ROMlist Generator Script for Windows
=========================================================
- Backing up your old Favorites.txt: Done
- Gathering list of your Favorites: Tags-Files
- ROM Found in File Arcade.tag: 64street
- ROM Found in File Arcade.tag: mslug
- ROM Found in File Arcade.tag: 40love
- ROM Found in File Sega Genesis.tag: blkdrgon
- ROM Found in File Sega Genesis.tag: Duke Nukem 3D (Brazil)
- ROM Found in File Sega Genesis.tag: afighter
- ROM Found in File Sega Genesis.tag: airduel
- Romlist Files [tag=txt]: 002
- Favorites added to Disc: 007
- ROMlist .\Favorites.txt: New
- is Done!..
- Add a display in Attract Mode and set its ROMlist to the newly created .\Favorites.txt
  • Favourites.txt:
64street;64th. Street - A Detective Story (World) (1991);6.91
mslug;Metal Slug - Super Vehicle-001 (1996);9.18
40love;Forty-Love (1984);0.00
blkdrgon;Black Dragon (Japan) (1987);9.40782122909
Duke Nukem 3D (Brazil);10 Super Jogos;Sega Genesis;;2002;Epyx;Compilaçao-Estratégia-Esporte;8+;;;;;;;;0.8;
afighter;Action Fighter (FD1089A 317-0018) (1986);7.00
airduel;Air Duel (Japan) (1990);6.74

1. Use /D with the cd /D, that makes the cmd.exe|CHDIR enter the folder and the /Drive pointed by argument, which will not occur without adding the /D when it runs pointing to drive other than the current one.

cd /d "D:\AM-2.6.1\ROMlists"

Obs.: Can also replace cd /d for pushd, which changes the order in the folder queue to search for its commands by entering the folder passed by the argument already placing the current folder to the first in the queue/stack, and also saving the Drive Folder where it was originally executed to return when run with popd.

pushd "D:\AM-2.6.1\ROMlists" 
          ...    ...
       ...seus códigos...
          ...    ...
popd

2. The command set /a can be used to define multiple variables already by assigning their values passed after the equals symbol, only using as delimiters, a space plus a comma (or just a comma), leaving its command to define the values of _num_favorites=0 and of _ROMlist=0:

set "variável_1=valor_1, variável_2=valor_2, variável_3=valor_3"

rem :: Ou sem o espaço

set "variável_1=valor_1,variável_2=valor_2,variável_3=valor_3"

Obs.: The variable romlist is delcarada set /a romlist=0 but does not figure in use in your code posted...

3. The original author of the code used a if exist favorites.txt() without a else(), ignoring (for some reason), the possible absence of the file or an action of deletion of the file by the user, which make false the message Backing up old favorites.txt file present at first run, or for when user resolve to delete that file.

4. The command set /a "variavel=variavel + 1" may be replaced by set /a "variável+=1" to apply the increment of its variable (to decrease just reverse the signal set /a "variavel-=1").

5. The loopings For (1x) and For /F (2x) used in the original bat are very slow, they will catch a Mesmonomearquivo.TAG and compare row by row with each row of Mesmonomearquivo.TXT.

6. You can change the loop to search the other file (all at once):

listar o linha por linha do arquivo.tag 
... for /f "usebackq delims=" %%i in (`more "%%~G"`)do (
    ...
    procurar a linha do arquivo.tag no arquivo.txt de uma vez já gravando o resultado
    find "%%~i;" <"%%~nG.txt">>".\Favorites.txt"
    ...

Obs.: But note that this will speed up processing between the two files, if the Mesmonomearquivo.TAG has 10 lines and the Mesmonomearquivo.TXT has 1,000 lines, so above, will be 10 lines x 1 file = 10 runs, and in the original is being 10 lines x 1000 lines = 10,000 runs, it is very time consuming.

7. Use of quotation marks, remember what @Ricardo Bohner...

In that part: echo."!str!" | findstr /c:%%a>nul && ( tries to put an quotation mark on %%a -> "%%a"

Obs.: The Finstr will interpret the contents of the string passed as a Regex (. *, $, ^, , 9-0, <, />, [, ], etc..., because it tends and treats everything like Regex (unless you use one of the explicit flags so it doesn’t, or the /L or the /C:), and also his For /F will immediately close the loop/loop in progress if the string contains any ):

64street;64th. Street - A Detective Story (World);Arcade;;1991;C.P. Brain;Briga de rua-Luta / 2.5D-Luta;1-2;;;;;;;;0.5;;;;;Duken

8. Replace the Findstrt by a find "simple*, it is easier to use the find for a literal search, where no regex is in use.

find "string/linhaDo.Tag" redireciona o arquivo.TXT // para o find ler na busca pela string...  
  
find "%%~i;" <"%%~nG.txt" 

9. Use quotes in your loop for /F adding usebackq, or abbreviated (undocumented) useback:

Obs.: Because of my paranoia, I also use it in some codes where special characters can appear in the processing, and yet, I will use the set /p "'=String Com Caracteres Especias [espacos] , &, (, ), etc.. " <nul , but as this causes the output of the next command to occur on the same line, I am obliged to add a echo\ when it is necessary to write on screen/file already breaking the current line. See this in the first code posted, (code in a compact version):

rem :: uso do usebackq no loop For /F aplica-se em echo, set, etc...
rem :: e o uso do set /p "'=String Escapada" <nul escapando as strings 
rem :: com aspas duplas "usecakq" + set /p "=String" mas sem ecoar/printar 
rem :: a duplicidade da aspas duplas: ""...""  em: ""String escapada""


for /f useback^delims^= %%i in (`
more "%%~G"`)do set/p "'=- ROM Found in File %%~nxG: %%~i"<nul & echo= & ( 
     <con: find "%%~i;" <"%%~nG.txt" >>".\Favorites.txt" && set/a "_num+=1")

10. Same code, but ported to a more version/layout covencional:

@echo off 

set /a "_tag=0"
set /a "_num=0"

cd /d "D:\AM-2.6.1\ROMlists" 
setlocal EnableDelayedExpansion

echo==========================================================
echo=    AM Favorites ROMlist Generator Script for Windows     
echo==========================================================

if not exist .\favorites.txt (
     
      set /p "'= - Cretating .\Favorites.txt File.."<nul
      echo\
     
    )  else (
    
      set /p "'= - Backing up your old Favorites.txt: "<nul
      move /y .\Favorites.txt .\Favorites.txt.backup 2>nul >nul
      if %errorlevel% equ 0 echo\Done^!!
    )
      
echo/- Gathering list of your Favorites: Tags-Files

for %%G in (*.tag)do (
     
     set /a "_tag=!_tag! + 1"
     
     for /f "usebackq delims=" %%i in (`more "%%~G"`)do (
     
         set/p "'=- ROM Found in File %%~nxG: %%~i"<nul 
         echo= 
         
         find "%%~i;" <"%%~nG.txt" >>".\Favorites.txt"
         if %errorlevel% equ 0 set /a "_num=!_num! + 1"
         
        )
    )

set /a "_num=!_num! + 1000"
set /a "_tag=!_tag! + 1000"

call echo/- Romlist Files [tag=txt]: !_tag:~-3!
echo/- Favorites added to Disc: !_num:~-3!
echo/- ROMlist .\Favorites.txt: New

endlocal
echo/- is Done!.. 

findstr /ec:\.\Favorites\.txt "%~f0"
goto :EOF

========================================================================================
- Add a display in Attract Mode and set its ROMlist to the newly created .\Favorites.txt
========================================================================================
  This script basically does the same thing as DM's Favorites ROMlist generator but 
  on Windows [ Shouts to him for the pi version d;) ].
                                                         
  How it works:
  It grabs all the (tagged) favorites from every tag file, searches for them through
  all the ROMlists, then generates a ROMlist called Favorites.txt Written by Steve 
  Sherrod, 05/20/17, as part of project HyperPie Expanded
========================================================================================

Some additional readings

  • Simply spectacular his explanation and his code, he does in seconds what took minutes thanks for helping me. + 1 like.

  • @arthurvalenca Thank you for reading/commenting/testing. Take the time to read the linked content.

  • 1

    can leave Thank you very much!!!!!

  • @arthurvalenca if remove timeout 3| of the first code posted, still gained - 3 seconds in execution. From the second code I removed...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.