Hello All
I am new on here and no expert so please be patient!
I have a script to read a text file (the source) and copy all of the lines of text into another file (the destination). At the same time it will replace one particular line with different content if a match is found. The text file contains around 40,000 lines and only two matches will be found. This all works ok within a for /f routine with the text line being stored in the variable %%i and then echo 'ed into the destination file. However, when the text line (and therefore the variable %%i) contains the following:
/MI&T!A?&(/c_+8OpR52NJidTMi*@Is+^i4Jq/?"I=?SDHN8BlG6N/4FE2;=s).mTs(qmTD/='*
it is not echo'ed into destination file and instead I get
"Displays messages, or turns command-echoing on or off.
ECHO [ON | OFF]
ECHO [message]
Type ECHO without parameters to display the current echo setting."
Of the 40,000 lines this is the only one that behaves this way, but in another file the following text also produces the same result
/ginmA6*P]@UN/?C27F3Bj,e"H"1_f4YJcd;ai]*1eU>7EF<p"?VOs*LL429@W%E,@8LfCIS=$j
I realise that the content of the text lines appears to be random gibberish, but it is meaningful and needs to be copied "as-is" into the destination file.
I am using
echo %%i>>%%a.new
or
echo !myline!>>%%a.new (where myline=%%i)
Can anyone throw any light on what is happening here and what method I might use to successfully echo the above.
Any help appreciated!
Stuart
Help with echo
Moderator: DosItHelp
Re: Help with echo
The text passages you have shown are not plain text but look like binary lines.
If the file contains an ascii binary executable file then more information about the file, and the task is needed
as echo is designed for ASCII text.
If the file contains an ascii binary executable file then more information about the file, and the task is needed
as echo is designed for ASCII text.
Re: Help with echo
Hi foxidrive, thanks for your response
Some more background. The text files in question are mostly made up of lines similar to the ones mentioned, but apparently random content. However, the first few lines are header information which is more or less in plain English. My task is to find the string (Gray) and replace it with the string (V). All other lines are unaltered. If I make this simple change in e.g. Wordpad then all works well.
I agree that nearly all of the lines of text are not "plain" but all of the characters are ASCII characters. My inspections of the file are all using Wordpad and I can copy and paste the lines of text from one text file to another. In the case of the first example it appears that the first "&" character on the line may be the source of the problem.
If, at the prompt I type "echo /MI"
the result is
/MI
If I type "echo /MI&"
The result is
/MI
For "echo /MI&T"
The result is
/MI
'T' is not recognised as an internal or external command, operable or batch file.
If I use the whole line in my first example the result is ..
More?
And the system is waiting for input
So it would seem that the command processor is treating the & in a special way. But there is something odd about this particular line since there are more than 30,000 other instances of "&" in the file as a whole and all of these others echo correctly.
I suppose my question is, suppose my variable contains the value "/MI&T" or more specifically the whole line mentioned, how can I echo it ?
Stuart
Some more background. The text files in question are mostly made up of lines similar to the ones mentioned, but apparently random content. However, the first few lines are header information which is more or less in plain English. My task is to find the string (Gray) and replace it with the string (V). All other lines are unaltered. If I make this simple change in e.g. Wordpad then all works well.
I agree that nearly all of the lines of text are not "plain" but all of the characters are ASCII characters. My inspections of the file are all using Wordpad and I can copy and paste the lines of text from one text file to another. In the case of the first example it appears that the first "&" character on the line may be the source of the problem.
If, at the prompt I type "echo /MI"
the result is
/MI
If I type "echo /MI&"
The result is
/MI
For "echo /MI&T"
The result is
/MI
'T' is not recognised as an internal or external command, operable or batch file.
If I use the whole line in my first example the result is ..
More?
And the system is waiting for input
So it would seem that the command processor is treating the & in a special way. But there is something odd about this particular line since there are more than 30,000 other instances of "&" in the file as a whole and all of these others echo correctly.
I suppose my question is, suppose my variable contains the value "/MI&T" or more specifically the whole line mentioned, how can I echo it ?
Stuart
Re: Help with echo
& are a special character and there are different ways to deal with them, but for a large file you might prefer a more robust and swift method:
This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
Code: Select all
type file.txt | repl "(Gray)" "(V)" L >newfile.txt
Re: Help with echo
Hi foxidrive
That worked like a dream. Thank you for your help. I am still intrigued as to what my problem was...believe me there are thousands and thousands of special characters in these files and I didn't need to escape any of the others - they echo'ed just fine. I will try not to lose any sleep over it though. Thanks once again for the solution.
Stuart
That worked like a dream. Thank you for your help. I am still intrigued as to what my problem was...believe me there are thousands and thousands of special characters in these files and I didn't need to escape any of the others - they echo'ed just fine. I will try not to lose any sleep over it though. Thanks once again for the solution.
Stuart
Re: Help with echo
& will echo correctly in a for metavariable, but not in a string literal
There are various characters that will fail in an echo command without being escaped with ^ (and % is a special case) plus some that will not echo when at the end of a line.
For example this will fail with the redirection used at the end of the line because CMD uses 0 to 9 as stream designators when alone, before redirection.
& is a command separator in a cmd batch file.
There are various characters that will fail in an echo command without being escaped with ^ (and % is a special case) plus some that will not echo when at the end of a line.
For example this will fail with the redirection used at the end of the line because CMD uses 0 to 9 as stream designators when alone, before redirection.
Code: Select all
echo 2>file.txt
& is a command separator in a cmd batch file.
Re: Help with echo
foxidrive wrote:For example this will fail with the redirection used at the end of the line because CMD uses 0 to 9 as stream designators when alone, before redirection.Code: Select all
echo 2>file.txt
Code: Select all
:: I.
:: That's how we can:
@echo+0>file.txt
@echo[1>>file.txt
@echo]2>>file.txt
@echo\3>>file.txt
@echo:4>>file.txt
@echo.5>>file.txt
@echo/6>>file.txt
:: But we can't do:
@echo(7>>file.txt
@echo=8>>file.txt
@echo;9>>file.txt
@echo,0>>file.txt
:: II.
:: That's how we can:
@set x=0
@call echo %%x%%>>file.txt
@set x=1
@cmd /v:on /c echo !x!>>file.txt
@set x=2
@for %%i in (%x%) do @echo %%i>>file.txt
:: But we can't do:
@set x=4
@echo %x%>>file.txt