+ Post New Thread
Results 1 to 15 of 15
Scripts Thread, Locate and edit text files in Coding and Web Development; Ok a reques to the scripting gurus... I want to search a folder tree for all instances of a given ...
  1. #1

    Join Date
    Jun 2005
    Posts
    223
    Thank Post
    6
    Thanked 8 Times in 8 Posts
    Rep Power
    29

    Locate and edit text files

    Ok a reques to the scripting gurus...
    I want to search a folder tree for all instances of a given text file, say 'user.txt' and append a couple of lines to it.
    I'm requesting a script though perhaps there's a application out there with this kind of function?
    TIA

  2. #2

    Join Date
    May 2006
    Posts
    66
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Re: Locate and edit text files

    in linux/unix, this will work

    for file in `find|grep user.txt`; do
    echo "whatever" >> $file
    done

  3. #3

    Join Date
    Jun 2005
    Posts
    223
    Thank Post
    6
    Thanked 8 Times in 8 Posts
    Rep Power
    29

    Re: Locate and edit text files

    Geoff you have been usurped!

  4. #4

    Join Date
    May 2006
    Posts
    66
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Re: Locate and edit text files

    btw: you may wish to change it to user.txt$ rather than user.txt, that way it wont care if for some reason you have a dir named user.txt, or, as . matches any charachter in regexs, if you had a dir named userFtxt

  5. #5

    Join Date
    May 2006
    Posts
    66
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Re: Locate and edit text files

    also, a more elegant soloution might be to use `find -name user.txt` instead of `find |grep user.txt`

    Just correcting my own suggestion again

  6. #6

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: Locate and edit text files

    In Windows, how about

    Code:
    for /f %f in (' dir /s /b user.txt') do @echo whatever >> %f
    or

    Code:
    for /f %f in (' dir /s /b user.txt') do type addendum.txt >> %f

  7. #7

    Join Date
    Jun 2005
    Posts
    223
    Thank Post
    6
    Thanked 8 Times in 8 Posts
    Rep Power
    29

    Re: Locate and edit text files

    Geezer,
    Cheers that almost works. The slight problem is that some of the files returned in %f have a space in their path which stops it doing the append.

  8. #8

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: Locate and edit text files

    Quote Originally Posted by _Bob_
    Geezer,
    Cheers that almost works. The slight problem is that some of the files returned in %f have a space in their path which stops it doing the append.
    just put quotes around it like so

    Code:
    for /f %f in (' dir /s /b user.txt') do type addendum.txt >> "%f "

  9. #9
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,942
    Thank Post
    115
    Thanked 272 Times in 250 Posts
    Rep Power
    104

    Re: Locate and edit text files

    I think you need to put the file name in some kind of quotes or double quotes to get the dos cmd line to work. If not then the equivalent vbscript would be several lines but would cope with the space I will post code later if you havent got it sorted.

  10. #10

    Join Date
    Jun 2005
    Posts
    223
    Thank Post
    6
    Thanked 8 Times in 8 Posts
    Rep Power
    29

    Re: Locate and edit text files

    Doh! Doesn't work even with quotes.
    Even
    for /f %f in (' dir /s /b prefs.js') do echo "%f "
    gives a mangled list, so the spaces are messing it up as soon as it is stored in the %f variable. I gave up and piped the output of the dir /s /b user.txt command to text file and did it in two stages.
    Linux users i bow to your technically superior command shell.

  11. #11


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    338

    Re: Locate and edit text files

    Linux users i bow to your technically superior command shell
    lol - you could install cygwin and get a *nix command shell on your windows box

    http://www.cygwin.com/

  12. #12

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: Locate and edit text files

    Quote Originally Posted by _Bob_
    Doh! Doesn't work even with quotes.
    Even
    for /f %f in (' dir /s /b prefs.js') do echo "%f "
    gives a mangled list, so the spaces are messing it up as soon as it is stored in the %f variable. I gave up and piped the output of the dir /s /b user.txt command to text file and did it in two stages.
    Linux users i bow to your technically superior command shell.
    Sorry forgot, spaces and tabs are used as delimeter characters by default.
    Have to try something like this

    Code:
    for /f "delims=?" %f in (' dir /s /b prefs.js') do @echo "%f"
    On the basis that ? will never be part of a valid filename.

  13. #13

    bossman's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    3,855
    Thank Post
    1,162
    Thanked 1,028 Times in 729 Posts
    Rep Power
    323

    Re: Locate and edit text files

    Why ammend why not create user.txt with every line in place how u want it and then just run a batch script or vb script to copy it into the dir tree, or am i being too plain and stupid and you require to do something else.

    sorry if this does not help

  14. #14

    Join Date
    Jun 2005
    Posts
    223
    Thank Post
    6
    Thanked 8 Times in 8 Posts
    Rep Power
    29

    Re: Locate and edit text files

    Ah will have to remember the delims bit. I didnt realise you could change delimiters. What i have basically done is added a line to every users' prefs.js file in their firefox profile. I didn't want to overwrite them and kill any existing preferences they may have set. It's all sorted now but i have a feeling i may be doing this again in the future so i'll kepp hold of the code.

  15. #15

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

    Re: Locate and edit text files

    The delims bit is useful for processing CSV files so that you can assign different fields to different variables:

    Code:
    for /f "tokens=1,5 delims,"  %n in (somefile.csv) @echo name: %n age: %m
    You can find out more by typing for /?

    There is a slight gotcha in that it's %n on the command line but %%n in a batch file.

SHARE:
+ Post New Thread

Similar Threads

  1. How can I locate a rogue access point?
    By sidewinder in forum Wireless Networks
    Replies: 6
    Last Post: 6th February 2007, 09:27 PM
  2. PDF file edit
    By danIT in forum General Chat
    Replies: 2
    Last Post: 20th September 2006, 09:00 AM
  3. Edit registry remotely?
    By SYSMAN_MK in forum Windows
    Replies: 5
    Last Post: 3rd April 2006, 11:28 AM
  4. Edit MSI?
    By Geoff in forum Windows
    Replies: 3
    Last Post: 17th January 2006, 03:11 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •