Question:
Batch: A replace command?
2010-06-26 10:50:59 UTC
I'm working on a mini converter. So lets say I want to convert 1.bat to 2.txt. And maybe I do that with a really simple "REN" command.
1.bat content this:

@echo off
echo Hello
pause>nul
exit

Then how can I "replace" example the "@ECHO OFF" with an empty line, and the same with "Pause", and "EXIT".
So the 2.txt would look like this:

Hello

Is that possible? :D
Three answers:
ʃοχειλ
2010-06-26 13:08:50 UTC
---- Edited (2) :



I just wrote a Windows Script Host demo for you. See the code in the following pastebin link:



http://pastebin.com/zFcxeZwP



Cut that code and paste it in a file name filter.wsf (make sure the extension of file is WSF). You won't need any compiler to exceute it. It is a ready-to-exceute script file in Windows systems. This demo would extract the message of the first echo command (@echo on or off is NOT included) in a batch file, and write it in a text file. reads the comments inside the demo for more information.



You could execute the command by simply double-clicking its icon. But that is not useful. You should execute it in cmd window like



cscript filter.wsf filename



You should replace filename with the name of your batch file without extension.



I hope it is useful.









---- Edited:



Your English is fine, and I could understand it fair enough.



Ok, it is what I meant in basic English:



There should be some common part in your batch files. You probably want to either keep that common part in a text file or delete that. If there is no common part, no programmer could write a code for it. I just gave you for examples. I rewrite them to make them clear. THESE ARE ONLY EXAMPLES to help you explain what you want to do. Look at them carefully, compare them with what is in your mind and make a new question in which explain how you would like to decide which part must be copied to the text file and which part must be deleted:



I just want to:

-Delete all lines in batch file which starts with a @ symbol (and keep all the other lines in the text file).

-Or, delete all lines containing the batch (or system) commands @echo, pause, goto, exit, … (this command is always the first word of the line) (and keep the rest lines in the text file)



As an example:



Exit



Will be deleted, but although this line has the word exit in it:



Echo Press any key to exit the program…



It won’t be deleted because exit is not the first word of the line.



-Look for the first line starting with command echo, when it is found, keep the message in the front of it, and delete everything else.

-Similar the previous line, but this time, for all lines starting with echo (NOT just the first line).



As an example for the last two examples, if your batch file contains these three lines:



@echo off

Echo Hello my friend

Echo Have a nice day



Then the first filter (working on the first echo) would write this line to the text file:



Hello my friend



(It ignores the other echo command). But the second one would write:



Hello my friend

Have a nice day









----

I approved the other answer perfectly. It has a really good point. (I am giving it a thumb-up)



However, I wish not leave your question without a direct answer. What you are looking for is definitely possible, and it is an ordinary task of a computer programmer. However, you need to be extremely specific if you expect that your program works precisely on a wide range of similar batch (.BAT) files.



As an example: if you just want it to work on this very specific file, you could write a program to fool with writing a simple text Hello on the file (as the other answer mentioned.)



You should normally specify some criteria to filter text lines. (Extremely precise) criteria such as:

- dropping all lines starting with @

- dropping all ines containing @echo, pause, goto, exit (as the first word of line) ... but not including echo (that is echo without @).

- extract the message of the first echo command of the file (for example, if the first echo command is echo hello world, it would simply extracts hello world).

- extract all the messages of all echo commands with dropping the heading echo word; that is, like the previous one but not only for the first echo but for all of them.



So, how could you describe the lines (and the text) you would like to filter in human language?

(like the examples I made.)
eli porter
2010-06-26 10:58:12 UTC
why don't you just write to the file?



echo Hello >2.txt



you don't really need to "convert" anything, a batch file IS a text file, .bat extension just tells windows to try and run it as a script, but there is no difference from a file format perspective.
wilebski
2016-10-13 09:33:22 UTC
In Batch records, a p.c. sign is unquestionably one of those get away character. What this suggests, is that it's going to not exhibit the 1st % sign. basically what's displayed after it. So, in case you have been to write down echo %20, it may pop out as: 20. to repair it, only use 2 p.c. indications: echo %%20 could pop out as %20. right it relatively is a code which will do what you're searching for: echo off setlocal enabledelayedexpansion cls set /p string=enter String: for /l %%a in (0,a million,a hundred) do ( if "!string:~%%a,a million!"==" " set NewString=!NewString!%%20 if not "!string:~%%a,a million!"==" " ( if not "!string:~%%a,a million!"=="" set NewString=!NewString!!string:~%%a,a million! ) ) echo !NewString!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...