Help need on file paths and files Keyword

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Help need on file paths and files Keyword

#1 Post by ladduq » 25 Jul 2012 04:34

Hi

I have a list of file names in a file named filenames.txt with some line number like below

filenames.txt
one.txt:12
two.txt:35
three.txt:89
four.txt:56
etc up to some thousands of files.


what needed to do is eliminate the Line numbers starting with colon in the filenames.txt

so output will be
deletedlinenumbers.txt
one.txt
two.txt
three.txt
four.txt

Now needed to find the file paths of these files in a directory and if there were multiple files of same name in a directory then the file path should not be taken for that file and it should be logged in a separate file named log.txt with reason multiple instances for specify that filename

and add the line numbers at the end of file path with colon in place in between which were deleted in above step, to add the line numbers at the end of file path the line numbers can be put in a separate file named deletedlinenumbers.txt to add them later at the end of file path

so output will be
filepath.txt
C:/folder/folder1/one.txt:12
C:/folder/folder1/two/two.txt:35
C:/folder/folder1/three/three.txt:89
C:/folder/folder1/four/four.txt:56


Now based on the file names without file numbers want to export a single line matching the keyword 'Hovering' in that directory files

so output will be like below if these were line starting with 'Hovering' keyword in all four files
Hoverting.txt file
Hoveringline: Hovering of one
Hoveringline: Hovering of two
Hoveringline: Hovering of three
Hoveringline: Hovering of four

Now finally needed to do is keep file path and its keyword line of same file line by line.so output will be like below
finaloutput.txt
C:/folder/folder1/one.txt:12
Hoveringline: Hovering of one

C:/folder/folder1/two/two.txt:35
Hoveringline: Hovering of two

C:/folder/folder1/three/three.txt:89
Hoveringline: Hovering of three

C:/folder/folder1/four/four.txt:56
Hoveringline: Hovering of four

It should be able to work for any number of files.Any other solution to this will be appreciated.

Thanks & Regards
Ladduq

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

Re: Help need on file paths and files Keyword

#2 Post by foxidrive » 25 Jul 2012 05:12

I was with you until here. Please explain further.

ladduq wrote:Now based on the file names without file numbers want to export a single line matching the keyword 'Hovering' in that directory files

so output will be like below if these were line starting with 'Hovering' keyword in all four files
Hoverting.txt file
Hoveringline: Hovering of one
Hoveringline: Hovering of two
Hoveringline: Hovering of three
Hoveringline: Hovering of four

Now finally needed to do is keep file path and its keyword line of same file line by line.so output will be like below
finaloutput.txt
C:/folder/folder1/one.txt:12
Hoveringline: Hovering of one

C:/folder/folder1/two/two.txt:35
Hoveringline: Hovering of two

C:/folder/folder1/three/three.txt:89
Hoveringline: Hovering of three

C:/folder/folder1/four/four.txt:56
Hoveringline: Hovering of four

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

Re: Help need on file paths and files Keyword

#3 Post by Squashman » 25 Jul 2012 07:24

Weren't you shown how to get the file paths in your previous thread and another thread showed you how to un-delimit data with colons in them?
You were also shown in those threads how to find a specific word in a file as well. Seems like you have this batch file written already.

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

Re: Help need on file paths and files Keyword

#4 Post by abc0502 » 25 Jul 2012 21:55

I'm Lost Too, :!:

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Help need on file paths and files Keyword

#5 Post by ladduq » 26 Jul 2012 04:22

foxidrive wrote:I was with you until here. Please explain further.

ladduq wrote:Now based on the file names without file numbers want to export a single line matching the keyword 'Hovering' in that directory files

so output will be like below if these were line starting with 'Hovering' keyword in all four files
Hoverting.txt file
Hoveringline: Hovering of one
Hoveringline: Hovering of two
Hoveringline: Hovering of three
Hoveringline: Hovering of four

Now finally needed to do is keep file path and its keyword line of same file line by line.so output will be like below
finaloutput.txt
C:/folder/folder1/one.txt:12
Hoveringline: Hovering of one

C:/folder/folder1/two/two.txt:35
Hoveringline: Hovering of two

C:/folder/folder1/three/three.txt:89
Hoveringline: Hovering of three

C:/folder/folder1/four/four.txt:56
Hoveringline: Hovering of four


@foxidrive
It means like based on filenames should get all the lines in those files that match keyword 'Hovering'.However there will be only one line that matches the keyword Hovering in all files.All these files will be in a root directory which will have many sub directories.

if we input 100 filenames then we get output 100 lines starting with hovering keyword.which should be placed in a different file.

Finally as file paths will be in one file and hovering keyword lines will be in another file.they needed to be placed like below.It means like placing line by line filepath and hovering line.


C:/folder/folder1/one.txt:12
Hoveringline: Hovering of one

C:/folder/folder1/two/two.txt:35
Hoveringline: Hovering of two

C:/folder/folder1/three/three.txt:89
Hoveringline: Hovering of three

C:/folder/folder1/four/four.txt:56
Hoveringline: Hovering of four

Regards
Ladduq
Last edited by ladduq on 26 Jul 2012 04:37, edited 1 time in total.

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Help need on file paths and files Keyword

#6 Post by ladduq » 26 Jul 2012 04:27

@all Thanks for your reply.

I dont have all of it i have a part of it like based on filenames to get their paths and matching keyword line and it doesnot work for filenames with multiple instances and there are no logs to keep track of multiple instances and as i'm leaning and new to this one i face some problems in acheiving it.


Regards
Ladduq

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

Re: Help need on file paths and files Keyword

#7 Post by foxidrive » 26 Jul 2012 05:22

As far as I can make out you have a file:

one.txt:12
two.txt:35
three.txt:89
four.txt:56

and you want to get the paths assembled so it is like the following.

C:/folder/folder1/one.txt:12
C:/folder/folder1/two/two.txt:35
C:/folder/folder1/three/three.txt:89
C:/folder/folder1/four/four.txt:56


If say one.txt appears more than once then you want that information logged in another file and not in the file with paths.

This code does that bit. Now read below: I need more info.

Code: Select all

@echo off
set fileout=fileout.txt
set filein=filein.txt
set filelog=file.log

del "%fileout%" 2>nul
del "%filelog%" 2>nul

for /f "tokens=1,2 delims=:" %%a in (%filein%) do (
del file1.tmp 2>nul
for /f "delims=" %%c in ('dir /b /s /a-d "%%a"') do >> file1.tmp echo %%c:%%b
find /c /v "" < file1.tmp |findstr /r "^1$" >nul
if errorlevel 1 (
>>"%filelog%" echo multiple %%a:%%b found
) else (
for /f "delims=" %%c in (file1.tmp) do >>"%fileout%" echo %%~dpc%%a:%%b
)
del file1.tmp 2>nul
)




Now, please continue the explanation where hovering comes from and you state it will only appear once, but you also said it will be more than once.

Is hovering a keyword that you want to find INSIDE the files, assuming they are text? Will there be more than one keyword? Is the keyword case sensitive?
I assume you want the lines that are found containing the keyword, to be placed in a file?

Give a brief but real example of the output of such a search - "hoveringline: hovering of one" don't make much sense.

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Help need on file paths and files Keyword

#8 Post by ladduq » 26 Jul 2012 22:20

@foxidrive

file instances will be more than once and not hovering keyword lines which are present inside the files as text.

yes hovering is a keyword that should be found inside the files.The keyword is not case sensitive.

foxidrive wrote:I assume you want the lines that are found containing the keyword, to be placed in a file?


yes lines that are found containing the keyword, to be placed in a file in order that means if filenames are given as input in a order then matching keyword lines of those line should be place in order

Example:
Input:
one.txt
two.txt
three.txt
four.txt

output:
Hoveringline: Hovering of one
Hoveringline: Hovering of two
Hoveringline: Hovering of three
Hoveringline: Hovering of four

Hovering keyword line will be present in each one of the files and that hovering keyword line in each file will only be once and not more than once.

say we have four files

one.txt
two.txt
three.txt
four.txt

Each one of these files have a line that start with hovering keyword.

So if we input these four lines then we should get 4 lines output of that matching hovering keyword line w.r.t file names this means the search of hovering keyword should be done on files in the given input filenames .similarly if we input 1000 filenames the output should be 1000 lines that match the hovering keyword in files.

After getting this matching keyword lines in one file and file paths in another file.

ladduq wrote:Now finally needed to do is keep file path and its keyword line of same file line by line.so output will be like below
finaloutput.txt
C:/folder/folder1/one.txt:12
Hoveringline: Hovering of one

C:/folder/folder1/two/two.txt:35
Hoveringline: Hovering of two

C:/folder/folder1/three/three.txt:89
Hoveringline: Hovering of three

C:/folder/folder1/four/four.txt:56
Hoveringline: Hovering of four


Regards
Ladduq

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

Re: Help need on file paths and files Keyword

#9 Post by foxidrive » 27 Jul 2012 00:21

The keyword is set in the batch file. Try this:

Code: Select all

@echo off
set keyword=hovering
set fileout=fileout.txt
set filein=filein.txt
set filelog=file.log

del "%fileout%" 2>nul
del "%filelog%" 2>nul

for /f "tokens=1,2 delims=:" %%a in (%filein%) do (
del file1.tmp 2>nul
for /f "delims=" %%c in ('dir /b /s /a-d "%%a"') do >> file1.tmp echo %%c:%%b
find /c /v "" < file1.tmp |findstr /r "^1$" >nul
if errorlevel 1 (
>>"%filelog%" echo multiple %%a:%%b found
) else (
for /f "delims=" %%c in (file1.tmp) do >>"%fileout%" echo %%~dpc%%a:%%b
)
del file1.tmp 2>nul
)

move /y "%fileout%" file2.tmp >nul

for /f "tokens=1,2,3 delims=:" %%a in (file2.tmp) do (
for /f "delims=" %%d in ('findstr /i "%keyword%" "%%a:%%b"') do (
>>"%fileout%" echo %%a:%%b:%%c
>>"%fileout%" echo %%d: %keyword% of %%~nb
>>"%fileout%" echo.
)
)

del file?.tmp 2>nul

pause

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Help need on file paths and files Keyword

#10 Post by ladduq » 27 Jul 2012 03:47

@foxidrive Thank you

Post Reply