views:

551

answers:

4

Hello, i have a directory which contains the following:

-directory
 -another directory
  -meta.xml
 -yet another directory
  -meta.xml
...

So it needs to find in every directory 'meta.xml', all by itself (so that i dont have to type in every directory at the code) And i want that with EVERY meta.xml file a command is done.

Is there ANY possible way to do this, and could this be quiet done because the command i was talking about gives a text line from that meta.xml file, and i would like to not see whats its doing, that you get something like this:

text from meta file 1
text from meta file 2
text from meta file 3

and not

meta.xml found
text from meta file 1
meta.xml found
text from meta file 2
meta.xml found
text from meta file 3

If thats not clear, just think that i would just to like it run quietly, ok. (/Q maybe?)

this is the command i was talking about:

findstr "<name>" meta.xml >temp1.lis
FOR /F "tokens=2 delims=>" %%i in (temp1.lis) do @echo %%i > temp2.lis
FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do @echo %%i > temp3.lis

it needs to do this for every single meta.xml file and giving the output so you get a total list when you do

type temp3.lis

Thanks!

A: 
findstr /r /s ".*" meta.xml

will give you every line from every meta.xml file in the current directory and all its subdirectories, in the form:

dir1\dir2\dir2\meta.xml: line is here.

If you need anything more complex, I'd suggest installing Cygwin or GNU tools and using the bash shell or UNIX-like utilities from that, something like:

find . -name meta.xml -exec cat {} ';'
paxdiablo
i like the first one, but the problem is that i dont want every line. Is there a way that i can put my own command (see my edited post) in findstr?
YourComputerHelpZ
If you don't want every line then just supply a more specific search term. How about this > findstr /r /s "tokens-. delims=" meta.xml
Glen
@YourComputerHelpZ. command.com/cmd.exe has come a long way since the bad old MSDOS days but it's still a poor cousin to bash (or ksh). The suggestion to use Cygwin would be my preference - it really gives you a *lot* more expressive power.
paxdiablo
Glen: I think you're confusing `for` and `findstr` there. What you wrote doesn't make much sense.
Joey
@YourComputerHelpZ or if you doesn't want to use cygwin, try PowerShell. There is no reason to use cmd/bat-scripts, if you don't develop for Operating Systems older than 10 years.
flokra
@flokra the problem is that not everyone has those extra stuff. with batch i know for sure everyone has it. It should reach a lot of users.
YourComputerHelpZ
A: 

To find all meta.xml files:

dir meta.xml /s

Then you can pipe the output to some other command like

dir meta.xml /s | someCommand
Glen
the second one seems great, how can i replace 'SomeCommand' with more lines of code?
YourComputerHelpZ
does not work, keeps giving me 'FINDSTR: meta.xml not found'. i meant that it should look trough all dirs for it...
YourComputerHelpZ
*subdirs i mean
YourComputerHelpZ
sorry, your command is fine, its my command, it uses findstr
YourComputerHelpZ
what's the command that you want to execute for every meta.xml file? If you don't know the exact command, then what do you want it to do?
Glen
i have to be able to find a way out,...
YourComputerHelpZ
its in the post, at the end
YourComputerHelpZ
hmmm. the code seems to work fine when i try it elsewhere. the problem is that i cant search into sub dirs?
YourComputerHelpZ
basicly what i want is to let it search trough subdirs, something that doesnt work now.
YourComputerHelpZ
A: 

I am not quite sure if I understood what you want. You want to find all files named meta.xml, but then do you want to search for something inside that file or do you want to see the complete content from that file? And you want to filter out any output regarding the name of the files found, you are only interested in the content of the files, right?

I am not sure if this is possible to do with cmd, but if you install cygwin you can easily do it the following way:

prompt>find top_level_directory -name meta.xml -print0 | xargs -0 grep -h "your search pattern"

for searching or

prompt>find top_level_directory -name meta.xml -print0 | xargs -0 cat

for outputting the complete file.

hlovdal
i dont wnat to use extra command-line tools, cause its for public.
YourComputerHelpZ
ok,, um... could there be a combination with my command? Maybe cygwin could also do that what i want to achieve?
YourComputerHelpZ
+1  A: 

Ok, as far as I can tell, this should work:

findmeta.cmd

@echo off
for /f "delims=" %%f in ('dir /b /s meta.xml') do (
    FOR /F "tokens=2 delims=>" %%i in ('findstr "<name>" %%f') do @echo %%i > temp2.lis
    FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do @echo %%i >> temp3.lis
)

I modified your script part so answers are added to temp3.lis, instead of replacing it, and removed the use of the first temp file.

If, in the end, what you're looking for is what ever is between the tags, like
<name>blahblah</name> (the blahblah part)

this should do even faster, no temp files needed and one less for loop (broke into multiple lines for ease of reading, it all hold on a single line if needed.

for /f "delims=" %%f in ('dir /b /s meta.xml') do (
    FOR /F "tokens=2 delims=><" %%i in ('findstr "<name>" %%f') do (
        @echo %%i >> temp3.lis
    )
)

Single line version:

for /f "delims=" %%f in ('dir /b /s meta.xml') do FOR /F "tokens=2 delims=><" %%i in ('findstr "<name>" %%f') do @echo %%i >> temp3.lis

Not sure which you prefer, IF this is what you're looking for.

EDIT:
Ok I think I got it, but I'm not sure it's gonna fix everything. Basically you want to change the tokens=2 to tokens=3 like this:

for /f "delims=" %%f in ('dir /b /s meta.xml') do FOR /F "tokens=3 delims=><" %%i in ('findstr "<name>" %%f') do @echo %%i >> temp3.lis

Now see if this works.

Jay
thanks, im going to try it out when im back home. im not at home right now.
YourComputerHelpZ
not working, for 2 meta.xml files it says a lot of ´name´s
YourComputerHelpZ
Hmm which one of the 3 snipets is not working? Did you try them all? also, can you post a part of the xml you want to process, so we get a clearer view of the task at hand?
Jay
i tried the 2nd one, the one with 5 lines and one temp file.
YourComputerHelpZ
the edit you gave works!!! Thanks for all your help and time you spend in this!
YourComputerHelpZ
im so glad i could do this with only batch...
YourComputerHelpZ
I'm glad I could provide an answer with standard cmd.exe commands.I will be reviewing your questions, I see people are quick to suggest PS/cygwin/python/VBS answers while there are possible -dos-solutions.
Jay