3

I need to create a batch file using which we can modify the content of a file.

For example

Testing.txt file contains a line Description=MAN_Human change to Description=MAN_Human_V2

only add (_V2) at the end of the MAN

So when we open the file we have description name MAN_Human_V2 instead MAN only.

i have multiple files to work on. Please help.

12
  • There is no good way to answer this. We can tell you how to add a text at the end of the file or the end of a line, or how to parse it and look for specific markers to be replaced by a new text... But as long as you don't specify the exact criteria, this question cannot be properly answered. Commented Jun 28, 2013 at 20:14
  • Hi..only need to add _V2 to the description name Description=MAN_Human change to Description=MAN_Human_V2 please help with the batch file. Commented Jun 28, 2013 at 20:20
  • Please let me know for any questions. Commented Jun 28, 2013 at 20:21
  • Take a look at this one: stackoverflow.com/questions/60034/… Commented Jun 28, 2013 at 20:26
  • i have seen this but it is not useful. Description name is not common for all the files so find and replace will not work. Please help me Commented Jun 28, 2013 at 20:31

2 Answers 2

4

You can do it by using for loop to move through each line of the file, and when you find the line with the 'Description', you append '_V2' to the output.

@echo off

for %%f in (*.txt) do (
  for /f "tokens=1* delims=:" %%g in ('type "%%f" ^| findstr /n /v "BoGuSsTrInG"') do (
    if "%%hx"=="x" (
      echo.>>"%%~nf.newtxt"
    ) else (
      for /f "tokens=1* delims==" %%i in ('echo.%%h') do (
        if "%%i"=="Description" (
          echo.%%i=%%j_V2>>"%%~nf.newtxt"
        ) else (
          if "%%jx"=="x" (
            echo.%%i>>"%%~nf.newtxt"
          ) else (
            echo.%%i=%%j>>"%%~nf.newtxt"
          )
        )
      )
    )
  )
)

ren *.txt *.oldtxt
ren *.newtxt *.txt

Of course, you have to output the content to a new file while you process the test.txt file. Then you replace the original file with the newly created one...

Edit #1

You have to copy the contents of the TXT files to new files completely before you rename any of them back to their original names. This is because the for loop will pick them up again after they are modified if you immediately rename them and some of them will get the _V2 appended several times.

I did not remove the .oldtxt files at the end, because you should never destroy your original files until you know that the process worked!

Edit #2

There is one workaround that I found that will preserve the blank lines. The workaround pipes the content of the text file to findstr, searching for all lines that don't contain BoGuSsTrInG. The output includes the line numbers (the only way to get it to print the blank lines). The line numbers have a trailing colon (:), so we can divide the output into different variables splitting the content on the colon. The tokens=1* split the line number into the %%g variable, and the result of the line is in the %%h variable. We can test %%h to see if it is empty and add a blank line to the new file. Otherwise, we process %%h as before.

Sign up to request clarification or add additional context in comments.

11 Comments

FYI - People will be more willing to help you in the future if you upvote answers that are helpful, and mark the answer that answers your question as correct.
but i do not want to change the file name
There are no batch file methods to change the content of a file. You can however, analyze each line of a file one at a time, and send each line to a new file (modifying the desired line when you come to it). Then, once you've created the modified file, you can replace the original file with the modified file. That is what the above code does...
i understand and it is working fine for the single file, but the thing is that i do not want a separate file and also does not change the actual file name. How about multiple file? Please advise and help me
Also, the description name is present in second line of each and every file. It is fixed.
|
1

You could download sed (a unix utility - google sed windows)

sed -i 's/Description=MAN_Human/Description=MAN_Human_V2/' test.txt

the -i means in-place, i.e. write the result back to the same file (instead of showing on screen). The second parameter says substitute ... to ..., and the rest is a (list of) file(s) to do this to. Google man sed for more information

2 Comments

Actually, this can be done with just batch files. See my answer.
thanks - removed line that says it's impossible with just batch files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.