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

string manipulation help

#1 Post by ice_531 » 21 Jan 2011 10:10

Hey all.

I am currently trying to get a batch file to work correctly, and having mild success. I'm still waiting for approval at work to have a VS ide installed on my work computer, but until then I need to go ahead and work on this batch file which may suit my needs, and even just incorporate it into my actual program when the time comes.

So heres what I'm trying to do. I want to make a batch file that will search through the current folder, and all subfolders for a specific word or phrase in all txt files that are found. The user chooses the search string, and then I need a way to be able to return the path to that txt file that contained the search string. This is what I have so far.

Code: Select all

@echo off
:SEARCH
set /p choice=Enter the search word or phrase to find the QF:

FINDSTR /s /i /L /c:"%choice%" *.txt > temp.txt

set /p exit= Do you want to try to search again: 1 for yes, 2 for no.
IF %exit%==1 GOTO SEARCH
IF %exit%==2 exit


The only problem with this is that it returns the path AND the search phrase that was found in the text file. if multiple txt files were found, it puts them each on separate lines, which is great. However I only really need the path. (so that later on i can give the user a choice to launch one of the text files that were found.)

Here is an example of the temp.txt output. This is returned by searching for the word "loyalty"

QF Folder Structure\Version 8.02\Service Packs\QF17.txt:Loyalty Interface Issues
QF Folder Structure\Version 8.04\Service Packs\QF3.txt:Loyalty Adjustment Issue

So, all I really want to do, is get rid of the " : " and anything to the right of it. Just so I have the paths to the text files, 1 path per line. I've had issues trying to use for /f and I guess I don't understand the tokens and delims parameters.

Any help would be appreciated!
Thanks.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: string manipulation help

#2 Post by !k » 21 Jan 2011 11:33

Use findstr /M

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

Re: string manipulation help

#3 Post by ice_531 » 21 Jan 2011 11:47

Doh!

I thought I had tried the m switch already, and had problems. Thanks, it worked perfect :)

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

Re: string manipulation help

#4 Post by ice_531 » 21 Jan 2011 11:51

Since that works now, what would be the best function to use to open up the path thats in the temp.txt

and if there were 4 paths in the temp file. 1 path per line.

i would assume i'd need a for loop to go line by line and shell notepad linefromtempfile(i)

i know how i would do this in php, or vb even. not so sure in dos though

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

Re: string manipulation help

#5 Post by ChickenSoup » 21 Jan 2011 12:53

You wouldn't even need to send it to the temp.txt file. Just use the following:

Code: Select all

for /f %%a "tokens=*" in ('FINDSTR /s /i /L /c:"%choice%" *.txt') do notepad.exe %%a

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

Re: string manipulation help

#6 Post by ice_531 » 21 Jan 2011 13:04

Well, the reason behind the temp.txt is that im going to end up giving the user a choice whether they want to open 1 of the text files that was found, or 1 at a time. If 3 are found, I don't want all 3 to open, unless they do it 1 at a time. (this will be incorporated later using a listbox in a vb or c++ most likely)

Thanks for the example of "for /f" though! :)

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

Re: string manipulation help

#7 Post by ice_531 » 21 Jan 2011 18:37

Another question here regarding the same code. Here's what I have so far. This isn't going to be my final result, but it works so far the way i want it to(on my pc at least)

Code: Select all

@echo off
:SEARCH
set /p choice=Enter the search word or phrase to find the QF:


FINDSTR /s /i /L /M /c:"%choice%" *.txt > temp.txt

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (temp.txt) do (
set /a n+=1
set v!n!=%%a
echo %%a
notepad.exe %%a
)

set /p exit= Do you want to try to search again: 1 for yes, 2 for no.
IF %exit%==1 GOTO SEARCH
IF %exit%==2 exit


Heres my question though. my for loop does the job of getting each line in the temp file into a separate variable: v1, v2, v3, etc. Later on I will need to give the user a choice to launch each of these, or all of them into notepad. Something I've run into though regarding my findstr line is this.

Code: Select all

FINDSTR /s /i /L /M /c:"%choice%" *.txt > temp.txt


How can I have it start its search in a different directory than the batch file is located. I tried inserting a path right after /c: but it creates a blank temp file. doesnt seem to do the search at the path I give it. Also, my batch file that will be running this will just be in a folder on the desktop, and so the temp file is fine being created in the same folder as the batch file. I just need to be able to conduct the whole search of a separate drive and folder path. (Which no users will have write access to, only read access.) Anyone able to shed some light on either of my issues, with the path to begin the search in, or the way to give the user the choice to launch each of the text files in the variables.

Let me know if I need to explain any better. The fun part is yet to come, when I have to inject all the user input needed for the batch file, from an actual application made in vb or c++.

Thanks again. :D

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

Re: string manipulation help

#8 Post by ice_531 » 22 Jan 2011 19:23

OK. nevermind the last post. It isn't going to be possible to have it search the different directory. as it isn't local on the harddrive, and i didnt have access to it by just doing CD \thepath

Sooo. Ill just have to get approval to have my batch file (once complete) placed in that root directory of that path,, so people can just run it from there and wont have the issue. Also did away with the temp file, so that no files have to be written (avoids the read-only access issue).

My current dilemma is the following. I need a way to search folder names for partial string.

Example:

Folders in root directory.

Version 4.00
Version 3.02
Version 6.00
Version 8.03
Version 8.04

Ok, so I need a way to search for the folder that contains partial string that the user can enter. So if a user types in " 3.02 " It will find the folder Version 3.02, and then the rest of my code can be executed from here, by simply issuing the CD Version 3.02 to change to that directory.

My only thought so far, is to just hardcode IF statements in for all 20 possible folders (would have to update the program anytime a new version comes out...which wouldn't be that bad i guess) But figured I'd ask to see if perhaps there was a for loop someone knew that would allow to just search.

Thanks

subhash chandra
Posts: 2
Joined: 23 Jan 2011 16:56

Re: string manipulation help

#9 Post by subhash chandra » 23 Jan 2011 18:05

If you want to change to a network directory you can use pushD command.

Further, For your last question i guess below should work.

1 ) Take a dir output of your root directory in temp file. DIR /b >%temp%\_dir.txt
2) take the user input using set /p in a varible
3) do a search for user inputed string into tempfile and redirect the output to another temp textfile1.
4) assign the value of tempfile1 to a varieble. use set /p var=<filename
5) cd to this newvariable.
6) it will change the directory to the correct version folder.
It will skip the IF condition,and even the folders are added later the code does not need to be modifed.

Let me know your thoughts..

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

Re: string manipulation help

#10 Post by ChickenSoup » 24 Jan 2011 09:00

ice_531 wrote:My only thought so far, is to just hardcode IF statements in for all 20 possible folders (would have to update the program anytime a new version comes out...which wouldn't be that bad i guess) But figured I'd ask to see if perhaps there was a for loop someone knew that would allow to just search.


This loop would probably do it for you:

Code: Select all

@echo off
:start
set /p searchversion=Enter the version to use (X to exit):
if /i "%searchversion%" == "X" echo.Exited by user && goto end
echo %searchversion% | findstr /r "[0-9]\.[0-9][0-9]">nul || echo.Invalid Version (ex: #.##) && goto start
for /f "tokens=*" %%a in ('dir /b /ad ^| findstr /R "\<%searchversion%\>"') do set workingversion=%%a
if "%workingversion%" == "" (
   echo.Version not found.
   goto start
)
echo Selected version: %workingversion%
CD "%workingversion%"
echo.Run rest of code...
:end
pause

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

Re: string manipulation help

#11 Post by ice_531 » 26 Jan 2011 19:42

I appreciate the replies all.

I have another question, i cant seem to figure this out after reading over the dir , findstr and for /f functions. So i cant use a temp file due to read only access restrictions all the machines have that this batch file will be run from. just an fyi, for any responses.

Im going to show you two pieces of code, the first is what i have that works, and the 2nd is how id like to change it to get it to narrow the search down more, based on a build and patch level added to the search.

Code: Select all

:SEARCH

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

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



So here's the piece of code Id like to get it to do.. i know the syntax and form must not be right since it isnt working. So by the time the user gets to the portion of SEARCH code, the batch file has found the main version folder the user wanted, and changed its path to this folder. Then i need to narrow down the next search to any folders that contain a partial string, which will be in format "buildpatch" e.g. 03F , or 01P (or it may not have a patch level, it may just be 02, 03, etc)

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)

If anyone can shed some light on my code above, or perhaps a better way. I want to be able to give the user the choice to launch the folder containing the txt file match at the end of all of this. obviously there might be multiple matches, hence the need for a choice to launch, instead of automatically launching 10 explorer windows if that was the result of the search.

Thanks again!

subhash chandra
Posts: 2
Joined: 23 Jan 2011 16:56

Re: string manipulation help

#12 Post by subhash chandra » 26 Jan 2011 21:03

what is the exact errror you are getting with your code?
use ^|^ instead of | (| does not work directly inside loop).
Will there possibility of more than 1 folder ?IF so then a temp file needs to be created which will hold the name of folders containg version match,Can you think of any folder where the user will have write permission (May be his home directory or something)?

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

Re: string manipulation help

#13 Post by ice_531 » 26 Jan 2011 21:36

the drive to be searched is read only. their own harddrive where the folder the batch file is kept has write access though. I guess a temp file could be made there.

Also, yes there are many folders. one of the versions alone has 60+ folders in it with different patches, etc.

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

Re: string manipulation help

#14 Post by ice_531 » 26 Jan 2011 21:38

So you are saying the following should work how I was trying to explain it.

Code: Select all

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



and then something like %%a > temp.txt
at the end? and then read each line back out and give each line as a choice to the user, and let them pick.. then just explorer %theirchoice% ?

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

Re: string manipulation help

#15 Post by ChickenSoup » 27 Jan 2011 07:32

Not Quite. The carot escapes the pipe in the for loop. also, when you pipe the output from one command to FINDSTR you are only searching the output not files. Try the following (I think). I'm not clear on what you are doing exactly. This seems to have morphed from your original question

Code: Select all

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


Post Reply