string manipulation help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#16 Post by ice_531 » 27 Jan 2011 08:24

Yes, sadly. My issue has changed throughout the process due to learning more about how horrible the folder structure is on the networked drive and how most of the folders are named in different formats.

I'll try out the code you posted when I get to work later.

Thanks

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: string manipulation help

#17 Post by ChickenSoup » 27 Jan 2011 08:33

Ahhh. Inconsistency. It is amazing how consistently people are inconsistent. Good luck.

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#18 Post by ice_531 » 27 Jan 2011 18:31

I resolved quite a few issues I was having with this batch file thanks to much help from the posts here. However, still having an issue with this particular line, or maybe it's not possible to work in the way I want it to.

Code: Select all

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir /ad /b /s ^| findstr "%buildPatch%" ^| FINDSTR /s /i /L /M /c:"%choice%" *.txt') do (
set /a n+=1
set v!n!=%%a
echo %%a
)


Specifically just this line

Code: Select all

for /f "tokens=* delims= " %%a in ('dir /ad /b /s ^| findstr "%buildPatch%" ^| FINDSTR /s /i /L /M /c:"%choice%" *.txt') 


Basically %buildPatch% will contain a value like the following: 03L

I need that portion to find all folder that contain "03L" to then conduct the next portion of search code on FINDSTR /s /i /L /M /c:"%choice%" *.txt

An example of a folder that might contain 03L would be the following:
7.02.XX.03LQF1
7.02.XX.03NQF3
7.02.XX.03LQF14

So my code above with "03L" in the variable buildPatch, should ONLY search the folders for QF1, and QF14 in the above 3 folder example.

However, my code is no longer having an error or just causing the batch to close, but it is searching even folders that dont contain the above. so it doesnt seem to be using the code properly, or rather IM not using the functions correctly. Maybe the solution has already been mentioned and I didn't understand? If I have to use a temp file to get this to work, if someone could provide an example with the above. (Id have to place this temp file where the batch file is being run from though, and not the current directory it is searching in...due to the read only on that drive.)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: string manipulation help

#19 Post by avery_larry » 28 Jan 2011 10:08

*untested*

Try:

findstr /i /c:"%buildPatch%"

Also, you probably want "delims=" instead of "tokens=* delmis= ". On the very rare case that a line *starts* with a delimiter, it will be stripped (which can be very useful when needed).

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: string manipulation help

#20 Post by ChickenSoup » 28 Jan 2011 11:01

Code: Select all

for /f "tokens=* delims= " %%a in ('dir /ad /b /s ^| findstr "%buildPatch%" ^| FINDSTR /s /i /L /M /c:"%choice%" *.txt')


Your problem is that you are doing a directory listing and then sending the output to the FINDSTR that is finding the buildpatch variable and then you are sending the output to a findstr that does absolutely nothing with the output and instead does its own search for the %choice% string in all txt files. So the final output has nothing to do with the directory listing or the first findstr.

An easy example of what you are doing:

Code: Select all

echo.This is a search test that does not matter | findstr /c:"matter" | echo.I like turtles

Basically, it doesn't matter what happens on the first 2 commands, it will always give an output of:

Code: Select all

I like turtles

I guess this is why am confused as to what you are doing.

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#21 Post by ice_531 » 28 Jan 2011 13:39

This is probably because I don't understand the for /f functionality.

So basically I need to break it down to a nested for loop or rather just 2 separate for loops? first start out with the dir /ad /b /s | findstr "%buildPatch%. and take the output of that (each folder path) into a temp file. then after that has complete, I do my 2nd for loop with the findstr /s /i /L /M /c: "%choice%" *.txt using each line from the temp file (that will hold each folder to search).

I guess this was my whole confusion, as I thought the for /f could be used like I had it coded, which was what i was trying to ask...if i was using the function correctly or not.

So, I'm going to re-work the search function a bit and see if i can get it broken down to two different for loops.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: string manipulation help

#22 Post by ChickenSoup » 28 Jan 2011 15:17

ice_531 wrote:

Code: Select all

:SEARCH
 
set /p buildPatch=Enter the build and patch level:
set /p choice=Enter the search word or phrase to find the QF:
 

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in ('dir /ad /b /s | findstr "%buildPatch%" | FINDSTR /s /i /L /M /c:"%choice%" *.txt') do (
set /a n+=1
set v!n!=%%a
echo %%a
)


So if the above code worked like i have it written (incorrectly). it would first search for folders containing the build and patch, and then from those folders found, conduct the search for the text file containing the keyword/phrase the user entered. but this search is ONLY conducted on the folders that match the build and patch(partially).. a real folder may read like: 8.03.28.02N (02N would be the build and patch)


did you try my for loop from several posts ago (below again)?

Code: Select all

for /f "tokens=* delims=" %%a in ('dir /ad /b /s ^| findstr /i /c:"%buildPatch%" ^| FINDSTR /i /c:"%choice%"') do (...

This first searches the directory for the %buildPatch% and then directs that output the search for the %choice%. This should be exactly what you need.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: string manipulation help

#23 Post by ChickenSoup » 28 Jan 2011 15:25

I'll give an example set for the dir /ad /b /s | findstr /i /c:"%buildPatch%" | FINDSTR /i /c:"%choice%":

Directories to search (result of 'dir /ad /b /s'):

Code: Select all

C:\sandbox\count
C:\sandbox\New
C:\sandbox\other
C:\sandbox\patches
C:\sandbox\Versions
C:\sandbox\Versions\1.05
C:\sandbox\Versions\2.00
C:\sandbox\Versions\Version 1.03
C:\sandbox\Versions\version 2.00.a15
C:\sandbox\Versions\version 2.00.a16
C:\sandbox\Versions\version 2.00.b03
C:\sandbox\Versions\Version 3.05
C:\sandbox\Versions\Version 3.05.a15


Commands:

Code: Select all

set buildPatch=2.00
set choice=a15
dir /ad /b /s | findstr /i /c:"%buildPatch%" | FINDSTR /i /c:"%choice%"'


Output:

Code: Select all

C:\sandbox\Versions\version 2.00.a15


Is this what you are looking for as an output?

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#24 Post by ice_531 » 28 Jan 2011 16:25

The only issue is, that %choice% variable, is going to be a txt file within any number of subfolders. so have to use the /s option, but whenever I do this.. it doesnt work. It shows all the text files that contain the choice variable. BUT it doesnt ONLY search the folders that include the keyword from the %buildPatch% variable.

I tried your code, and got the same result I was getting before.

See. buildpatch is only helping me narrow down which folders to search within; for the choice variable ..which can be a keyword that is in a text file within the folders. The problem I'm having is with getting it to only search the folders that match buildPatch (partially).

modified your code a bit. dont need the /s switch on dir command as i only need the folders in the current directory (to look through for buildpatch)

Code: Select all

dir /ad /b | findstr /i /c:"%buildPatch%" | FINDSTR /s /i /L /M /c:"%choice%" *.txt


I see why the above wont work like you said before, because it just bypasses the first code and only searches for %choice%?

BUT this is how I need it to work; so i'm going to have to break the code down into separate parts then, am I not?

First code to find the folders that containg %buildPatch% within their folder names.

THEN the code to search for %choice% variable (text files) within the folders folder from the previous code^

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#25 Post by ice_531 » 28 Jan 2011 16:35

So, this first part returns the output that I need.

Code: Select all

dir /ad /b | findstr /i /c:"%buildPatch%"


So now, I just need to use the other piece of code to search the folders returned by the above code, to look for my %choice% variable within any txt files in the subfolders.

Should have gone about it this way a lot sooner. :cry:

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#26 Post by ice_531 » 29 Jan 2011 12:58

Ok, just an update here on what all is working now.

I have everything working fine, it is searching by the first variable, and then only searching txt files based on that. Heres what I have so far.

Code: Select all

set /p buildPatch=Please enter the build and patch: 

dir /ad /b | findstr /i /c:"%buildPatch%" > "%myPath%\temp.txt"

set /p choice=Enter the search word or phrase to find the QF:

set myTemp="%myPath%\temp.txt"

setLocal EnableDelayedExpansion
for /f "tokens=1* delims= usebackq" %%a in (%myTemp%) do (
set /a n+=1
set v!n!=%%a

pushD %CD%\%%a\
FINDSTR /s /i /L /M /c:"%choice%" *.txt
popD

)


So it prints the txt file names that were found. It only prints the ones that were found in the %%a which contained the folder names based on the search of buildPatch.

This is working fine. However, is there any way instead of printing the name of the textfile, i can print the folder name that contained the txt file? didnt see any switches with findstr that would accomplish this. this might not be possible with how I did my search code also. so let me know any suggestions.

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

Re: string manipulation help

#27 Post by aGerman » 29 Jan 2011 18:31

Well, the folder name is in !cd! after pushD. The only thing you have to do is checking the errorlevel that findstr returned.
Untested:

Code: Select all

@echo off &setlocal enabledelayedexpansion

set /p buildPatch=Please enter the build and patch:
set /p choice=Enter the search word or phrase to find the QF:

for /f "delims=" %%a in ('dir /ad /b ^|findstr /i /c:"%buildPatch%"') do (
  pushd "%%~a"
  findstr /s /i /m /c:"%choice%" *.txt >nul &&(
    echo !cd!
  )
  popd
)

pause



Regards
aGerman

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#28 Post by ice_531 » 29 Jan 2011 19:55

I tried that already, just echoing the CD after findstr, however it was giving me weird results. Ill have to wait until I'm back at work on monday, as I didnt save my code this time, and i dont have access to the networked drive, etc from home.

ice_531
Posts: 18
Joined: 21 Jan 2011 09:59

Re: string manipulation help

#29 Post by ice_531 » 31 Jan 2011 14:51

OK. I just had to use a nested for loop, type format. Everything is working like I wanted now. I'm just fixing it up a bit, I'll post the code to the search function tonight, in case it will help anyone else who may be doing something similar.



Thanks again, to everyone who gave me feedback. :mrgreen:

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

Re: string manipulation help

#30 Post by aGerman » 31 Jan 2011 15:49

I'm curious about your code. Sometimes I was wondering if nobody could solve your problem. I hope your solution will elucidate things of my confusion.

Regards
aGerman

Post Reply