0

I have this text document (txt1.txt):

&()[]{}^=;!'+,`~.mp3
¿¡áéíóú!.mp3

When processing it with a FOR command I lose characters like "^" and "!".

The bat file is as follows (it has two FOR commands that maybe can be simplified into just one, and the second one has an ECHO command at the end that I don't know if it can be implemented better):

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
CHCP 1252
SET TXTFILE1=txt1.txt
SET TXTFILE2=txt2.txt
(
    FOR /F "usebackq delims=" %%I IN (%TXTFILE1%) DO (
        SET LINE=%%I
        ECHO !LINE:.mp3=!
    )
) > %TXTFILE2%
SET LINENUMBER=1
SET TXTFILE3=txt3.bat.txt
FOR /F "usebackq eol=| delims=" %%J IN (%TXTFILE2%) DO (
    ECHO SET TRACK!LINENUMBER!=%%J>> %TXTFILE3%
    SET /A LINENUMBER+=1
)
ECHO EXIT /B>> %TXTFILE3%
ENDLOCAL
PAUSE > NUL | SET /P =Presione una tecla para salir . . . & ECHO(
EXIT
E_net4
  • 27,810
  • 13
  • 101
  • 139
user84131
  • 1
  • 1

1 Answers1

1

Use this code for the batch file:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFile=txt1.txt"
set "OutputFile=txt2.txt"
set "LineNumber=0"
(
    for /F usebackq^ delims^=^ eol^= %%I in ("%SourceFile%") do (
        set "Line=%%I"
        set /A LineNumber+=1
        setlocal EnableDelayedExpansion
        echo set TRACK!LineNumber!=!Line:.mp3=!
        endlocal
    )
) >"%OutputFile%"
endlocal

For an explanation of the FOR loop see my answer on How to read and print contents of text file line by line? Empty lines in source file are ignored by FOR which should be no problem here as it looks like the source file contains a list of MP3 file names.

The question does not contain an example content of source file and an example of expected content of output file. So the code was tested with a source file created by myself which are the lines as posted at How to read and print contents of text file line by line? with some lines with .mp3 appended and one line with ^ added.

If all lines end with .mp3 it would be better to use:

echo set TRACK!LineNumber!=!Line:~0,-4!

The line read from file is in this case output without the last four characters.

It is important to have delayed expansion NOT enabled on assigning the line read from file to environment variable Line. Otherwise this line is parsed by cmd.exe a second time as explained by How does the Windows Command Interpreter (CMD.EXE) parse scripts? On second parsing of SET LINE=%%I each ^ in line read from file is interpreted as escape character and each ! is interpreted as begin/end of an environment variable reference.

For that reason it is necessary to assign first the line read from file to environment variable Line while delayed expansion is disabled to avoid parsing the line before execution command SET by Windows command processor. Then delayed environment variable expansion is enabled which results in creating a copy of current list of environment variables, pushing current directory path on stack and pushing also current states of command extensions and delayed environment variable expansion on stack before enabling also delayed expansion. Then the line can be output with the additional text at beginning with line number and with case-insensitive removing all .mp3 occurrences from line. Finally command ENDLOCAL must be used to discard the copy of all environment variables, pop current directory path back from stack and pop also the states of command extensions and delayed expansion from stack and set those two features accordingly which means here disabling delayed expansion. Read this answer for details about the commands SETLOCAL and ENDLOCAL.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143