Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch file to comment specific lines into a text file

Posted on 2012-12-26
4
Medium Priority
?
778 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

Expert Comment

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

~bp
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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