Batch file to comment specific lines into a text file

Posted on 2012-12-26
Medium Priority
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 :

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

   Containe all the lines

   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

Exemple :

MyFile.txt Containe :

Open in new window


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:

Open in new window

Hope you can help me

Question by:Joesephlebon
  • 2
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.


LVL 60

Accepted Solution

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" (
    ) else (

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

Open in new window


Author Closing Comment

ID: 38728037
Thanks so much Experts ;)
It's work fine :)
LVL 60

Expert Comment

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


Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses
Course of the Month5 days, 9 hours left to enroll

589 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