Page 1 of 2

recursive search for string

Posted: 19 Apr 2012 14:21
by doscode
Hello, can you help me with script to find all .txt files which contain more than one occurrence of "</coordinates>" ? To get a list of files.

Re: recursive search for string

Posted: 19 Apr 2012 14:34
by foxidrive
This searches *.txt and in subdirectories and if "</coordinates>" is found on two or more separate lines then it will log the filename in found.txt


Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.txt /b /s') do (
for /f "delims=" %%b in ('find "</coordinates>" ^<"%%a" ^|find /c /v "" ') do (
if %%b GTR 1 >>"found.txt" echo %%a
)
)

Re: recursive search for string

Posted: 20 Apr 2012 02:46
by doscode
Thank you.

PS:
But it is very slow.

Re: recursive search for string

Posted: 20 Apr 2012 03:12
by doscode
Your command takes too long. Maybe the command could to be faster if I would use grep. But here I fail.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.kml /b /s') do (
 for /f "delims=" %%b in ('grep "</coordinates>" <"%%a"') do (
   echo %%b 
  pause
 )
)
pause

Re: recursive search for string

Posted: 20 Apr 2012 03:18
by foxidrive
doscode wrote:Thank you.

PS:
But it is very slow.


You're welcome.

But I dare say it is a shitload faster than you opening each file in a text viewer, searching for text, closing it and repeating with the next 200 files. :)

Re: recursive search for string

Posted: 20 Apr 2012 03:26
by doscode
I run web server on my computer and I have application that can load that files one by one, searches for text, and replaces every file. Directory structure is complicated, and there is cca 10-50 or 80 files in every folder. It takes 15 seconds to complete all edit actions by server. So it is damn fast. But when I run your command nothing happens. I waited few minutes and still not completed. So I think, here is something wrong. I thought it should be simple to find that strings and count them. And sure on server I would do it in 10 seconds. But because I want to use this script to recover files which are damaged (contain twice occurence of the word) I want to copy files from the location which is out of the server document folder. Therefor would like to use CMD if it can be fast. In other way, I can copy the backup to the server document directory and then make command for web app.

Re: recursive search for string

Posted: 20 Apr 2012 03:39
by foxidrive
doscode wrote:I run web server on my computer.
Directory structure is complicated, and there is cca 10-50 or 80 files in every folder.
It takes 15 seconds to complete all edit actions by server. So it is damn fast. But when I run your command nothing happens. I waited few minutes and still not completed. So I think, here is something wrong.


I can't tell what you are doing. Are you running it in the root directory? How many .txt files are involved? What size are the files, on average?

I thought it should be simple to find that strings and count them. And sure on server I would do it in 10 seconds.


Well it is simple - my script is only 6 lines or so.

Re: recursive search for string

Posted: 20 Apr 2012 03:43
by doscode
Statistics:
3822 files, 321 folders.
5,63MB total
14,9MB total on disk

2kB per file

Edit:
Actually it takes cca 4 minutes to find first file. But there is much more files to find.

Re: recursive search for string

Posted: 20 Apr 2012 04:11
by foxidrive
doscode wrote:Statistics:
3822 files, 321 folders.
5,63MB total
14,9MB total on disk

2kB per file

Edit:
Actually it takes cca 4 minutes to find first file. But there is much more files to find.


The first task for CMD is to gather a list of every file in the tree, and isolate the .txt files.

EDIT: Are you sure you aren't running it from the root directory?

If you put the batch file and find.exe on a ramdisk then it may work faster, although disk caches are in place and should help anyway.
Actually, why not copy it all to a ramdisk and run it from there. It will run faster, if that is important to you.

Re: recursive search for string

Posted: 20 Apr 2012 04:21
by doscode
I never worked with ramdisk and idk how to use them.

I implemented a restore function into my web app. It will restore file automatically, if there is an error. Only weakness is the need to copy all folder structure including files into document server location.

Re: recursive search for string

Posted: 20 Apr 2012 05:56
by Squashman
doscode wrote:Statistics:
3822 files, 321 folders.
5,63MB total
14,9MB total on disk

2kB per file

Edit:
Actually it takes cca 4 minutes to find first file. But there is much more files to find.

Those statistics look a little skewed.

Re: recursive search for string

Posted: 20 Apr 2012 06:40
by doscode
why

Re: recursive search for string

Posted: 20 Apr 2012 06:43
by Squashman
doscode wrote:why

Commas are usually used as a thousand separator.
So it that suppose to be 5.63MB or did you drop a zero at the end and it is suppose to be 5,630MB?
14.9MB or 14,900MB?

Re: recursive search for string

Posted: 20 Apr 2012 07:25
by doscode
I just have rewrite what I had seen on dialog window of my local system. The size of whole structure has 14,9MB on disk. 15MB.

Re: recursive search for string

Posted: 20 Apr 2012 08:14
by abc0502
u can use notepad++ it has a search feature that search in many files and replace words u caould try if batch didn't help