Results 1 to 4 of 4

Thread: Batch Commands To Read and Delete Lines from txt-File

  1. #1
    officer07 Guest

    Batch Commands To Read and Delete Lines from txt-File

    Hi there,

    first of all I have a file named "directory.txt" which contains all
    filenames of a certain directory (some thousand files or so) and looks like
    this:

    importantfile1.doc
    nicework.ppt
    salary09.xls
    salary08.xls
    ....

    All these files need to be encrypted with a special program (GnuPG, but that
    is rather unimportant).
    Therefore I need to create a batch file that reads the first line of
    "directory.txt", puts the information into a variable and then deletes that
    line from the file and then passes the information in the variable on to the
    encryption program (as a parameter, see below).
    It is important that the line is deleted from the "directory.txt" before the
    encryption program is being executed, so that another thread of that batch
    file that would be started some time later does not start to encrypt the same
    file again.

    For now, I have already created the following "one-liner":

    FOR /F "delims=" %%i IN ('TYPE directory.txt') DO echo
    secretpassphrase|gpg.exe --passphrase-fd 0 -v -c "%%i"

    What I have not been able to accomplish yet, is to delete the line in
    question from the directory.txt BEFORE gpg.exe is being executed.
    In some imaginative pseudocode I imagine this would look somewhat like this:

    ========================
    x = CountLines(directory.txt);
    FOR i = 0 to x DO
    BEGIN
    $filename = READLINE(i) from directory.txt;
    DELETELINE(i) from directory.txt;
    echo secretpassphrase|gpg.exe --passphrase-fd 0 -v -c "$filename"
    END;
    ========================

    I hope you can understand my problem and help me somehow to find the
    commands to accomplish this.
    The goal is to be able to start this "program" like five or ten times in
    parallel, so that the task of encrypting all files in the directory is
    accomplished faster that if only one file could be encrypted at a time.

    I would also happily try to do this using VBS or something like that. The
    only point is that the line that executes the encryption program, namely:
    echo secretpassphrase|gpg.exe --passphrase-fd 0 -v -c "$filename"
    should stay as it is (except for the variable containing the filename at the
    end, of course). This is, because the passphrase can only be entered
    interactively (which is done here by a pipe that passes it on to fd0, kind
    of).

    Any help will be appreciated very much!
    Thank you all in advance,

    yours,
    officer07

  2. #2
    Mervyn Zhang [MSFT] Guest

    RE: Batch Commands To Read and Delete Lines from txt-File

    Hi,

    Thank you for posting here.

    According to your description, I understand that:

    You would like to read and delete the first line of a TXT file.

    If I have misunderstood the problem, please don't hesitate to let me know.

    The following bat file will delete the first line of directory.txt. You can
    "call" it in your original bat file to delete first of directory.txt. For
    more information about Call, please refer to the following article:
    Call
    http://technet.microsoft.com/en-us/l.../bb490873.aspx


    @echo off
    for /f "skip=1 tokens=*" %%A in (directory.txt) do echo %%A >> d2.txt
    del d.txt
    rename d2.txt directory.txt

    This bat file create a new file without the first line of original file and
    then delete original file and rename new file. It performance may be not
    good. You may get error message if there is only one line left in
    directory.txt file.

    At the same time, I suggest that you initial a new post in the MSDN form to
    get further support there. They are the best resource for development
    related problems.

    For your convenience, I have list the link as followed.

    MSDN Forum
    http://forums.microsoft.com/MSDN/default.aspx?SiteID=1

    Sincerely,
    Mervyn Zhang
    Microsoft Online Community Support

    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.


  3. #3
    Pegasus \(MVP\) Guest

    Re: Batch Commands To Read and Delete Lines from txt-File


    "officer07" <officer07@newsgroup.nospam> wrote in message
    news:F435EE47-872B-4755-AA2E-F9BBCE97220C@microsoft.com...
    > Hi there,
    >
    > first of all I have a file named "directory.txt" which contains all
    > filenames of a certain directory (some thousand files or so) and looks
    > like
    > this:
    >
    > importantfile1.doc
    > nicework.ppt
    > salary09.xls
    > salary08.xls
    > ...
    >
    > All these files need to be encrypted with a special program (GnuPG, but
    > that
    > is rather unimportant).
    > Therefore I need to create a batch file that reads the first line of
    > "directory.txt", puts the information into a variable and then deletes
    > that
    > line from the file and then passes the information in the variable on to
    > the
    > encryption program (as a parameter, see below).
    > It is important that the line is deleted from the "directory.txt" before
    > the
    > encryption program is being executed, so that another thread of that batch
    > file that would be started some time later does not start to encrypt the
    > same
    > file again.
    >
    > For now, I have already created the following "one-liner":
    >
    > FOR /F "delims=" %%i IN ('TYPE directory.txt') DO echo
    > secretpassphrase|gpg.exe --passphrase-fd 0 -v -c "%%i"
    >
    > What I have not been able to accomplish yet, is to delete the line in
    > question from the directory.txt BEFORE gpg.exe is being executed.
    > In some imaginative pseudocode I imagine this would look somewhat like
    > this:
    >
    > ========================
    > x = CountLines(directory.txt);
    > FOR i = 0 to x DO
    > BEGIN
    > $filename = READLINE(i) from directory.txt;
    > DELETELINE(i) from directory.txt;
    > echo secretpassphrase|gpg.exe --passphrase-fd 0 -v -c "$filename"
    > END;
    > ========================
    >
    > I hope you can understand my problem and help me somehow to find the
    > commands to accomplish this.
    > The goal is to be able to start this "program" like five or ten times in
    > parallel, so that the task of encrypting all files in the directory is
    > accomplished faster that if only one file could be encrypted at a time.
    >
    > I would also happily try to do this using VBS or something like that. The
    > only point is that the line that executes the encryption program, namely:
    > echo secretpassphrase|gpg.exe --passphrase-fd 0 -v -c "$filename"
    > should stay as it is (except for the variable containing the filename at
    > the
    > end, of course). This is, because the passphrase can only be entered
    > interactively (which is done here by a pipe that passes it on to fd0, kind
    > of).
    >
    > Any help will be appreciated very much!
    > Thank you all in advance,
    >
    > yours,
    > officer07


    You could solve this problem either with a batch file or with VB Script. The
    batch file would a little shorter but it has one big drawback: It would not
    be particularly robust. If the file "directory.txt" contains any "poison"
    characters such as ", ', %, ^,(,), & etc. then it's likely to fail. A VB
    Script solution would not be subject to this restriction.

    Which would you prefer, batch or VB?



  4. #4
    Mervyn Zhang [MSFT] Guest

    RE: Batch Commands To Read and Delete Lines from txt-File

    Hi,

    Would you mind letting me know the result of the suggestions? If you need
    further assistance, feel free to let me know. I will be more than happy to
    be of assistance.

    Sincerely,
    Mervyn Zhang
    Microsoft Online Community Support

    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.


Similar Threads

  1. Multiple commands for a batch file in a for loop?
    By SANDESH49 in forum Software Development
    Replies: 4
    Last Post: 08-05-2012, 10:40 AM
  2. Multiple commands in batch file loop
    By Toshaan in forum Vista Help
    Replies: 10
    Last Post: 08-05-2012, 10:39 AM
  3. How to execute commands of batch file at timed intervals
    By Camryn in forum Software Development
    Replies: 5
    Last Post: 27-03-2010, 08:47 PM
  4. Batch File Commands
    By Pratap Gad in forum Operating Systems
    Replies: 3
    Last Post: 30-07-2009, 08:07 PM
  5. Batch Script to parse lines in text file
    By jntoner1@gmail.com in forum Windows Server Help
    Replies: 8
    Last Post: 28-01-2009, 04:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,287,651.66325 seconds with 17 queries