searching within text files and copy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joe
Posts: 35
Joined: 06 Sep 2017 07:56

searching within text files and copy

#1 Post by joe » 07 Sep 2017 07:28

I am not a programmer and very new to batch scripting

I have a directory containing several sub-directories.
each sub-directories contain thousands of .txt files with unique names.

my request is,

I have a list.txt file containing 100 or more key words.

ie.

reboot
shutdown
restart
crash
update
antivirus
windows ...... and so on.

I need a batch script to search for the listed keywords inside the .txt file in all of the directories/sub-directories.

if found then create a directory (keyword name) and copy those .txt files

source directory is d:\logs\\\\\\sub-directories
destination directory may be in e:\sorted\\\\\(keywords as sub-directories).


please help..

clarification:


destination can be e:\sorted\ after this keywords as sub-directories\
Last edited by joe on 07 Sep 2017 08:22, edited 1 time in total.

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

Re: searching within text files and copy

#2 Post by Squashman » 07 Sep 2017 08:16

Not understanding your destination directory example. Looks like you have multiple nested level of directories in your destination. Shouldn't it be just the one keyword as the sub directory? If not then how do we determine what multi-nested sub-directory it is supposed to go to?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: searching within text files and copy

#3 Post by aGerman » 07 Sep 2017 10:01

Give that code a go. Note that findstr option /i means that upper/lower case will be ignored for the key words.

Code: Select all

@echo off &setlocal DisableDelayedExpansion

REM List file in the same folder along with the Batch script
set "list=list.txt"

REM source root folder that shall be recursively searched for text files containing the key words
set "src=d:\logs"

REM destination root folder where the subfolders (named as the key words) shall be created and found files shall be copied
set "dst=e:\sorted"

REM read the list file linewise
for /f "usebackq delims=" %%i in ("%list%") do (
  REM try to find files recursively
  for /f "skip=1 delims=" %%j in ('findstr /d:"%src%" /ismc:"%%i" *.txt') do (
    REM create subfolder if not exists
    2>nul md "%dst%\%%i"
    REM copy the file
    copy "%src%\%%j" "%dst%\%%i\"
  )
)
pause

Steffen

joe
Posts: 35
Joined: 06 Sep 2017 07:56

Re: searching within text files and copy

#4 Post by joe » 08 Sep 2017 07:44

steffen code works perfectly but it takes a long time to search for 50 or so keyword..

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

Re: searching within text files and copy

#5 Post by Squashman » 08 Sep 2017 08:19

joe wrote:steffen code works perfectly but it takes a long time to search for 50 or so keyword..

Yes, Because you want to know what keyword it matched on, it has to iterate all the files for every keyword individually. So if you have 50 keywords, it is searching all your files 50 times.

Post Reply