Jump to content

Recommended Posts

Posted

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

Posted
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
Posted

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 :)

Posted

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.

Posted
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 "

Posted
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.
Posted

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. ;)

Posted
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.

Posted

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

Posted

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.

:)

Posted

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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...