windows - What comand do I enter into my batch file to do this?

07
2014-07
  • HaKDMoDz

    I want my batch file to find a string in a file and return it to the next line in my batch file.

    How can I do this? I've googled over and over but can't get the specific answer I need with the command to enter into my batch file. So for example

    I have a document on desktop and it's called "PhoneNumbers.txt" And I want my batch file to find the string I'm searching for from the file and return it to my batch file for next step of batch file.

    Desktop file is called "phonenumbers.txt" and inside is a list of people I know and their phone number. I want my batch file to search for a string say "Amanda P" and return only her number to my batch file so it enters the returned number into the line like

    ECHO Amanda's phone number is __________ 
    

    Ok so I can't explain it any easier or better so I hope someone understands and can help me with the exacr words to type into my batch file?

  • Answers
  • Richard

    This code snippet should get you started:

    @echo off
    set /p string="Enter search string: " %=%
    echo Looking for %string%...
    findstr "%string%" phonenumbers.txt
    

    In case it is not obvious %string% contains the text entered by the user.

    How you extract the phone number from the output of findstr depends very much on how the line is formatted in the first place. If it is delimited in a sane way, then one possible solution would be to consider using the for command.


  • Related Question

    windows 7 - Multicolored Batch File
  • JustinD

    I have this batch file i wrote, and i wrote it to make specific words to be different colors, as a test.

    But so far, i can't get any farther than this, I'm stuck, and I was wanting to use it to make other words in different colors, to make a color coding scheme. But can't figure out how. I've honestly spent weeks trying to figure this one out but am having no luck.

    Here is my code:

    MD Color
    CD Color
    cls
    echo Cool> "Text to Color"
    echo Hello> "Test"
    echo Test> "Helloo"
    cls
    FINDSTR /A:0c /C:"Cool" /S "Text to Color"
    FINDSTR /A:0b /C:"Hello" /S "Test"
    FINDSTR /A:0a /C:"Test" /S "Helloo"
    CD..
    RD /S /Q Color
    @pause
    

    The name of the file is "Text to Color.bat" in a folder named "Color"

    Result

    C:\Users\CST27\Dropbox\Color\Color>FINDSTR /A:0c /C:"Cool" /S "Text to Color"
    Text to Color:Cool
    
    C:\Users\CST27\Dropbox\Color\Color>FINDSTR /A:0b /C:"Hello" /S "Test"
    Test:Hello
    
    C:\Users\CST27\Dropbox\Color\Color>FINDSTR /A:0a /C:"Test" /S "Helloo"
    Helloo:Test
    
    C:\Users\CST27\Dropbox\Color\Color>CD..
    
    C:\Users\CST27\Dropbox\Color>RD /S /Q Color
    Press any key to continue . . .
    

    With "text to color" in red, "test" in blue, and "Helloo" in green.

    And I was wanting to maybe try and make the words after those in a different color as well.


  • Related Answers
  • Trevor Sullivan

    Use Windows PowerShell instead of cmd.exe

    Write-Host -ForegroundColor Red -Object "Hello";
    Write-Host -BackgroundColor Blue -Object "World!";