recursive search for string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

recursive search for string

#1 Post by doscode » 19 Apr 2012 14:21

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: recursive search for string

#2 Post by foxidrive » 19 Apr 2012 14:34

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

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#3 Post by doscode » 20 Apr 2012 02:46

Thank you.

PS:
But it is very slow.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#4 Post by doscode » 20 Apr 2012 03:12

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: recursive search for string

#5 Post by foxidrive » 20 Apr 2012 03:18

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

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#6 Post by doscode » 20 Apr 2012 03:26

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: recursive search for string

#7 Post by foxidrive » 20 Apr 2012 03:39

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.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#8 Post by doscode » 20 Apr 2012 03:43

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: recursive search for string

#9 Post by foxidrive » 20 Apr 2012 04:11

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.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#10 Post by doscode » 20 Apr 2012 04:21

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: recursive search for string

#11 Post by Squashman » 20 Apr 2012 05:56

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.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#12 Post by doscode » 20 Apr 2012 06:40

why

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: recursive search for string

#13 Post by Squashman » 20 Apr 2012 06:43

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?

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: recursive search for string

#14 Post by doscode » 20 Apr 2012 07:25

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: recursive search for string

#15 Post by abc0502 » 20 Apr 2012 08:14

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

Post Reply