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.

Sumit
  • 83
  • 1
  • 3
  • 7
  • 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. – GolezTrol Jun 28 '13 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. – Sumit Jun 28 '13 at 20:20
  • Please let me know for any questions. – Sumit Jun 28 '13 at 20:21
  • Take a look at this one: http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – GolezTrol Jun 28 '13 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 – Sumit Jun 28 '13 at 20:31
  • Duplicates: http://stackoverflow.com/questions/17370879/renaming-file-based-on-its-content-using-batch-file and http://stackoverflow.com/questions/17366009/rename-file-based-on-file-content-batch-file – James L. Jun 28 '13 at 20:37
  • It's best to ask **one** well-formed question and keep the discussion limited to one thread, instead of creating multiple threads hoping that lots of people will answer the same question asked multiple different ways. – James L. Jun 28 '13 at 20:38
  • And that's why I asked you to define the rules. One example doesn't enable us to answer this question. If you cannot specify the boundaries of your problem, we cannot help. – GolezTrol Jun 28 '13 at 20:38
  • James this is a different question. Please let me know if did not understood the question. I need the batch file please help me – Sumit Jun 28 '13 at 20:39
  • GolezTrol - now that you have understood the question can you please help me in creating a batch file please – Sumit Jun 28 '13 at 20:40
  • It's really not a different question -- it's the next step of the other two questions you already posted. It would have been better to modify your original question and add more details to it so that all the information stays in one thread. It sounds like you have 1000 files, with a 'Description=???' in each. Sometimes the 'Description' is not unique, so renaming the files to the description doesn't always work... So you need to edit the TXT file and change the description to make it unique so the rename will work. Is that the unstated need? – James L. Jun 28 '13 at 20:45
  • James - that part is done now i have to edit the Description name by adding _V2 only. please help – Sumit Jun 28 '13 at 20:49

2 Answers2

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.

James L.
  • 9,384
  • 5
  • 38
  • 77
  • 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*. – James L. Jun 28 '13 at 20:58
  • but i do not want to change the file name – Sumit Jun 28 '13 at 21:32
  • 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... – James L. Jun 28 '13 at 21:49
  • 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 – Sumit Jun 28 '13 at 22:00
  • Also, the description name is present in second line of each and every file. It is fixed. – Sumit Jun 28 '13 at 22:02
  • I tweaked the code to do what you need. I recommend that you do a Google search for programming batch files and learn some of the techniques for yourself. Most times, users on Stack Overflow will simply give you pointers to help you solve your own problem -- and won't write the code for you. – James L. Jun 28 '13 at 22:24
  • Hi James Thanks for the below comments. I am trying from my end also. Hope something good will come out. Thanks again – Sumit Jun 28 '13 at 22:28
  • All - i am able to complete my task...a big ton of thanks to all of you..cheers for the weekend. Special thanks to James and Amadanon..really appreciatiable guys. – Sumit Jun 28 '13 at 23:24
  • Can we have a code which make changes to the third line of a file because that DESCRIPTION is fixed(third line of every file) after that pointer does not move forward? – Sumit Jun 29 '13 at 00:29
  • See the last post in this link (http://www.computing.net/answers/programming/for-loop-to-read-blank-lines-from-file/20635.html) to see how someone was able to get a `for` command to **not** skip blank lines. -- I'm afraid I don't understand your last question (about the pointer not moving forward). – James L. Jun 29 '13 at 06:27
  • James..above code is working very well but i want tbe line space to be copied also..please advise – Sumit Jun 29 '13 at 19:24
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

AMADANON Inc.
  • 5,753
  • 21
  • 31