.cmd/.bat to add lines to a .txt file ???

Discussion in 'Scripting' started by tnx, Jul 11, 2012.

  1. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    Hi all.
    Was after some more help and advice. My little project just keeps on growing and I always get side tracked when I wish to learn summet new.

    What I would like to know is,,,,,

    Is it possible to add lines to an existing .txt file...

    Exaample if i had this text doc

    (my "makelist.cmd" gave me this .txt.....hark at me...)

    i want to add for example "= 1980,,,,,,,1980,0,0" to each line and end up with the following .txt

    I have been doing it one line at a time and some of my .txt have quite a few lines in them so being able to run a .cmd and have the lines added and a new .txt file made would be great.

    tried to find how to do this on the net but all I see is gobbeldy gook and I dont understand it....

    So any help,tips and pointer will be fantatstic....
     
  2. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    #2 Alphawaves, Jul 11, 2012
    Last edited by a moderator: Apr 20, 2017
    I would use powershell, heres an example that will replace = with = 1980,,,,,,,1980,0,0 in a file named test.txt:

    Code:
    type test.txt | powershell -Command "$input | ForEach-Object { $_ -replace \"=\", \"= 1980,,,,,,,1980,0,0\" } > test.txt"
    ;)
     
  3. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #3 tnx, Jul 11, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Hiya mate....

    Cheers for this info..Will give it a whirl shortly and see what happens...

    :)
     
  4. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    Hi Aphawaves...

    This works fine and wow,it's prety cool. butttttttttttt

    One ikkle problem....The lines on my text file do not end with "=" They are just the file name and ext "viahub3.cat"

    If I manually add a "=" to each line your code works a treat.

    Been looking at it and it uses "-replace". i have been looking at powershell commands and there is an "add-content"

    I have no idea if this will work,I have tried just replacing repalce with add-content but no luck.
    I simply removed the "=" from your code and the gates of hell open up in the test.txt.
    i tried "" and all the lines dissapear,like magic.....

    I will read up more on the power shell commands....never even heard of them till just now...

    cheers..
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    #5 Alphawaves, Jul 11, 2012
    Last edited by a moderator: Apr 20, 2017
    Ok try this to append each end of line:

    Code:
    type test.txt | powershell -Command "$input | ForEach-Object {$_ -replace \"$\", \" = 1980,,,,,,,1980,0,0\"} | out-file test.txt"
    ;)
     
  6. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #6 tnx, Jul 12, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Alphawaves..

    You are a genius.

    I have no idea what any of that means and i just wish i did.

    Alphawaes the scripting wizard :wizard:
     
  7. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    :laie: No im just a guy from up north!!!
    Powershell is there to be used, its cool...:D