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
Re: Locate and edit text files
in linux/unix, this will work
for file in `find|grep user.txt`; do
echo "whatever" >> $file
done
Re: Locate and edit text files
Geoff you have been usurped!
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
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 :)
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
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.
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 "
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 :lol: I will post code later if you havent got it sorted.
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. ;)
Re: Locate and edit text files
Quote:
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/
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.
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
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.
:)
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.