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 ...
-
7th June 2006, 08:33 AM #1 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
-
-
IDG Tech News
-
7th June 2006, 08:41 AM #2
- 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
-
-
7th June 2006, 08:46 AM #3 Re: Locate and edit text files
Geoff you have been usurped!
-
-
7th June 2006, 08:48 AM #4
- 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
-
-
7th June 2006, 08:50 AM #5
- 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
-
-
7th June 2006, 09:09 AM #6
- 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
-
-
7th June 2006, 10:27 AM #7 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.
-
-
7th June 2006, 10:30 AM #8
- Rep Power
- 0
Re: Locate and edit text files

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 "
-
-
7th June 2006, 10:32 AM #9 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.
-
-
7th June 2006, 12:44 PM #10 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.
-
-
7th June 2006, 12:56 PM #11 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/
-
-
7th June 2006, 12:58 PM #12
- Rep Power
- 0
Re: Locate and edit text files

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.
-
-
7th June 2006, 12:59 PM #13 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
-
-
7th June 2006, 01:10 PM #14 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.
-
-
7th June 2006, 02:11 PM #15
- 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:
Similar Threads
-
By sidewinder in forum Networks
Replies: 6
Last Post: 6th February 2007, 10:27 PM
-
By danIT in forum General Chat
Replies: 2
Last Post: 20th September 2006, 09:00 AM
-
By SYSMAN_MK in forum Windows
Replies: 5
Last Post: 3rd April 2006, 11:28 AM
-
By Geoff in forum Windows
Replies: 3
Last Post: 17th January 2006, 04: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
-
Forum Rules