Solved

Batch file to comment specific lines into a text file

Posted on 2012-12-26
4
763 Views
Last Modified: 2012-12-28
Hi Experts,

Happy cristmas for all, and hope we find all the happiness, health and money (lotof money $$$)  :D

Please, i need to do a batch file like that :

MyBatchFile.bat    file.txt    FileWithLineToComment.txt

Where :

FileWithLineToComment.txt
     it's a text file, this one containall the lines text (not numbers of line) that we need to comment.

MyFile.txt:
   Containe all the lines

MyBatchFile.bat
   Is the program that i need to write. This batch file will :
  1- open FileWithLineToComment.txt, and read line by line
  2- for each line we read, we will search it into the "MyFile.txt"
  3- one time we find it we will add " // " in the first of the find line, and like that it will be
comment.
 


Exemple :

MyFile.txt Containe :
123
34
12
153
123
757

Open in new window

FileWithLineToComment.txt
12
757

Open in new window


if i do this command line :
MyBatchFile.bat    file.txt    FileWithLineToComment.txt

Open in new window


MyFile.txt   will be like that:
 
123
34
//12
153
123
//757

Open in new window


Hope you can help me

Thanks
0
Comment
Question by:Joesephlebon
  • 2
4 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38723048
Are these real world examples, or are they going to be text files with any amount of data, words, spaces, commas, other characters potentially on the line.  Same for the list of lines to check?

Are there any blank lines etc. in the file that are important?

And just to check it seems you are checking the whole line to comment, i.e. if your string was found part way across the line.

If all of those end up being just like your answer it is a relatively simple for loop but no point writing that if not.

thanks

Steve
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 38727187
This should give you a pretty good place to start, let me know what questions you have.

@echo off
setlocal EnableDelayedExpansion

REM Make sure required parms were supplied
if "%~1" EQU "" (
  echo No data file specified to process
  exit /b
)
if "%~2" EQU "" (
  echo No comment file specified to process
  exit /b
)

REM Get input file and comment file from command line parms
set Filename=%~1
set Comments=%~2

REM Define name for temporary file
set Outfile=%Temp%\%~nx1

REM Send changed file to tem file for now
(
  REM Read each line of file
  for /F "usebackq tokens=*" %%L in ("%Filename%") do (
    REM Read each line from comment file
    set MakeComment=N
    for /F "usebackq tokens=*" %%C in ("%Comments%") do (
      REM If this line is to be commented flag it

      if /I "%%L" EQU "%%C" set MakeComment=Y
    )
    if "!MakeComment!" EQU "Y" (
      echo.//%%L
    ) else (
      echo.%%L
    )
  )
)>"%Outfile%"

if exist "%Outfile%" (
  copy /Y "%Outfile%" "%Filename%" >NUL
  del "%Outfile%" >NUL
)

Open in new window

~bp
0
 

Author Closing Comment

by:Joesephlebon
ID: 38728037
Thanks so much Experts ;)
It's work fine :)
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38728305
Welcome, glad that was useful.

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question