_Bob_ Posted June 7, 2006 Posted June 7, 2006 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
ico2 Posted June 7, 2006 Posted June 7, 2006 in linux/unix, this will work for file in `find|grep user.txt`; do echo "whatever" >> $file done
ico2 Posted June 7, 2006 Posted June 7, 2006 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
ico2 Posted June 7, 2006 Posted June 7, 2006 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
NetworkGeezer Posted June 7, 2006 Posted June 7, 2006 In Windows, how about for /f %f in (' dir /s /b user.txt') do @echo whatever >> %f or for /f %f in (' dir /s /b user.txt') do type addendum.txt >> %f
_Bob_ Posted June 7, 2006 Author Posted June 7, 2006 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.
NetworkGeezer Posted June 7, 2006 Posted June 7, 2006 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 for /f %f in (' dir /s /b user.txt') do type addendum.txt >> "%f "
ChrisH Posted June 7, 2006 Posted June 7, 2006 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.
_Bob_ Posted June 7, 2006 Author Posted June 7, 2006 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.
CyberNerd Posted June 7, 2006 Posted June 7, 2006 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/
NetworkGeezer Posted June 7, 2006 Posted June 7, 2006 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 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.
bossman Posted June 7, 2006 Posted June 7, 2006 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
_Bob_ Posted June 7, 2006 Author Posted June 7, 2006 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.
NetworkGeezer Posted June 7, 2006 Posted June 7, 2006 The delims bit is useful for processing CSV files so that you can assign different fields to different variables: 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now