View unanswered posts | View active topics It is currently 24 Apr 2014 02:23



Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
recursive search for string 
Author Message

Joined: 15 Feb 2012 14:02
Posts: 163
Post recursive search for string
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.


19 Apr 2012 14:21
Profile

Joined: 10 Feb 2012 02:20
Posts: 3675
Post Re: recursive search for string
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:
@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
)
)


19 Apr 2012 14:34
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
Thank you.

PS:
But it is very slow.


20 Apr 2012 02:46
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
Your command takes too long. Maybe the command could to be faster if I would use grep. But here I fail.

Code:
@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


20 Apr 2012 03:12
Profile

Joined: 10 Feb 2012 02:20
Posts: 3675
Post Re: recursive search for string
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. :)


20 Apr 2012 03:18
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
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.


20 Apr 2012 03:26
Profile

Joined: 10 Feb 2012 02:20
Posts: 3675
Post Re: recursive search for string
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?

Quote:
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.


20 Apr 2012 03:39
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
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.


20 Apr 2012 03:43
Profile

Joined: 10 Feb 2012 02:20
Posts: 3675
Post Re: recursive search for string
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.


20 Apr 2012 04:11
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
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.


20 Apr 2012 04:21
Profile

Joined: 23 Dec 2011 13:59
Posts: 1554
Post Re: recursive search for string
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.


20 Apr 2012 05:56
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
why


20 Apr 2012 06:40
Profile

Joined: 23 Dec 2011 13:59
Posts: 1554
Post Re: recursive search for string
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?


20 Apr 2012 06:43
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: recursive search for string
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.


20 Apr 2012 07:25
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: recursive search for string
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


20 Apr 2012 08:14
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.