No announcement yet.

Using batch script to write\delete symbols into a text file

  • Filter
  • Time
  • Show
Clear All
new posts

  • Using batch script to write\delete symbols into a text file

    Following this thread, I need to perform something similar.
    We have an *.txt file which contain users Information (first and last name, pager number, company name etc). This file reside on some network share and it used by some message system. This system sending messages/alerts to users from this txt file according to their work time (shifts). Bellow some quote of this file:
    ### Shift 1
    #Edward Black:8324:All:Shift 1
    #Melanie Colins:3165:All:shift 1
    #Paul Johns:3159:All:shift 1
    #Igor Gorin:8261:All:Shift 1
    #Nebo McLain:3171:Novelus:shift 1
    #Andrew Smeet:3350:Novelus:shift 1
    #Alex Katz:3182:DHL:shift 1
    Sign # before each line says to the system do not send messages to this user and vice versa, when line with user information doesn’t contain # sign, user will receive all messages.
    Currently each user needs to edit this file twice at day, at beginning of the shift (enable messages) and at end of the shift (disable messages).
    I'm looking for script which will use input method, something like this:
    @echo off
    echo Please insert your input here:
    set INPUT= 
    set /P INPUT=Type input: %=%
    if "%INPUT%"=="" goto input
    and according to the input script will check each line in this file for # sign existence. I.e. if # line exist, it remove it, it not exist it will add #sign to beginning of the line.
    For example, for single user I'll use his first and/or last name (or pager number) as input.
    If I need all shift 1 users, I'll use shift1 as input, or if I need Novellus users, I'll use Novellus as input. So in this way whenever I'll run this script it will add/remove # sign automatically, i.e. if user currently receives messages, he will not receive them anymore, and if he doesn’t receive messages, he will start to receive. Any help is much appreciated!

  • #2
    Re: Using batch script to write\delete symbols into a text file

    Meanwhile I've found some solution... and I'm trying to improve it. On scripting guys blog I've found explanation how can I use a script to open a file and replace text:

    Const ForReading = 1
    Const ForWriting = 2
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("\\server\share\Text.txt", ForReading)
    strText = objFile.ReadAll
    strNewText = Replace(strText, "Igor ", "#Igor ")
    Set objFile = objFSO.OpenTextFile("\\server\share\Text.txt", ForWriting)
    objFile.WriteLine strNewText
    WScript.Echo "Done for: Igor"
    The first thing I'm searching for, how I can integrate into this script option to prompt users to enter some information instead of ask each user to edit script to change the text string, need to be changed, manually?
    And second, how I can make script possible to check whenever in beginning of the line which contain desired text string we have # sign, remove it and whenever the line not contain this sign, insert it?
    Also I've found another useful article which can help on scripting guys blog:

    intLineFinder = InStr(strNextLine, "Igor")
    This code uses the InStr method to see if the string "Igor" can be found anywhere within the variable strNextLine. But still, how I can tell to script to put or remove # sign in the beginning of the line, which contain desired string?