Home > Back-end >  Why don't I get the correct number of lines from .txt and also random number does not change
Why don't I get the correct number of lines from .txt and also random number does not change

Time:01-09

two things I don't understand what I can do more.

  1. %%C instead of showing me that in output-mp3.txt (%%B) I have one single line -> the output is 124000 lines. If I make the for /f %%C alone in another .bat is showing me the corect number of lines.
  2. I need if %%C == 1 to get !exportname! (%%~nB) which is the name of the song and if is not equal with 1 to be generated a random name. My conditional command doesn't work also %RANDOM% show all the time the same number when is looped.
  3. If someone have a more professional and optimized approach of what I need I will very gladly to hear any directions.
@echo off
Setlocal EnableDelayedExpansion

for /F "delims=" %%A in (mp3list-1.txt) DO (
    for /F "tokens=2 delims='" %%B in (%%A) DO (
        for /f %%C in ('Find /V /C "" ^< %%B')  do (
            echo %%A
            echo %%B
            echo %%~nB
            echo %%C
            Echo Notepad file has %%C lines and the name of the song is %%~nB
            if "%%C"=="1" (
                set /p exportname=%%~nB 
                Echo Name of the song

            ) else (
                set /p exportname=!RANDOM!
                Echo Random number
            )
            Echo !exportname!
        )
    )
)

mp3list-1.txt contain

F:\.....\Videos-1\output-mp3.txt

output-mp3.txt in this case %%B contain

file 'F:\....\Canal-1\Videos-1\name of the song.mp3'

LATER EDIT:

I tried like this also with the third FOR, but same not showing me that I have 1 single line .. batch tell me I have 50k lines, witch is no true... also changed make some other changes.. no luck ..

@echo off
Setlocal EnableDelayedExpansion

for /F "UseBackQ delims=" %%A in ("mp3list-1.txt") DO (
    for /F "UseBackQ tokens=2 delims='" %%B in ("%%A") DO (
          set "cmd=findstr /R /N "^^" "%%B" | find /C ":""
                 for /f  %%C in ('!cmd!') DO (
            echo %%A
            echo %%B
            echo %%~nB
            echo %%C
            Echo Notepad file has %%C lines and the name of the song is %%~nB
            if "%%C"=="1" (
                set /p exportname=%%~nB 
                Echo Name of the song

            ) else (
                set /p exportname=!RANDOM!
                Echo Random number
            )
            Echo !exportname!
        )
    )
)

CodePudding user response:

Because your script and comments/questions do not match I have made some guesses / assumptions.
This is what your script does:
%%A is a line from file 'mp3list-1.txt'
%%B is the full path and filename extracted from %%a, this is an mp3 file
%%C is the number of lines in the mp3 file! (this is a binary file not text).

Your comments mention a file named 'output-mp3.txt'.
Just for clarity I have added the file output-mp3.txt to keep the changes close to your code.
But the output-mp3.txt is not needed.
Also the check for number of lines is not needed because this file will have just 1 line (%%B).
Because this mystery file "output-mp3.txt" is neither created, nor used in your code this is a bit of a guessing game.

    @echo off
Setlocal EnableDelayedExpansion

for /F "delims=" %%A in (mp3list-1.txt) DO (
    for /F "tokens=2 delims='" %%B in (%%A) DO (
        > "output-mp3.txt" echo %%B
        rem type "output-mp3.txt"
        rem for /f %%C in ('Find /V /C "" ^< %%B')  do (
        for /f %%C in ('Find /V /C "" ^< "output-mp3.txt"')  do (
            echo %%A
            echo %%B
            echo %%~nB
            echo %%C
            Echo Notepad file has %%C lines and the name of the song is %%~nB  **
            if "%%C"=="1" (
                set /p exportname=%%~nB 
                Echo Name of the song

            ) else (
                set /p exportname=!RANDOM!
                Echo Random number
            )
            Echo !exportname!
        )
    )
)
  • Related