2

I have a for loop that gives me access to filenames in a folder, and I need to check that filename to see if it contains a string.

For example, filename: 'C:\Temp\hello.cs' Pattern to find: 'C:\Temp\h'

The pseudocode for this is

IF filename.Contains(pattern) then
    // DO SOMETHING
END IF

I tried this:

%%i|find %pattern% >nul
if not errorlevel 1 echo "Pattern matched"

But it just opened the file as if I'd double-clicked on the file.

1
  • 1
    You forgot the echo: echo %%i|find... Commented Aug 13, 2012 at 13:40

2 Answers 2

3

Thanks guys but I found this worked:

SETLOCAL EnableDelayedExpansion
Set pattern=name

for .... do (
    Set filenameStr=%%i
    Set patternReplaced=!filenameStr:%pattern%=?!

    If Not !patternReplaced! == !filenameStr! echo "contains pattern"
)
Sign up to request clarification or add additional context in comments.

Comments

0

Rob van der Woude's Scripting Pages is a great resource for solving DOS/Batch file problems like this. From what I'm seeing, something like this might work for you:

ECHO OFF

FOR %%A IN (C:\Temp\h*) DO (
    ECHO ---
    ECHO Pattern matched
    ECHO Name in 8.3 notation : %%~snA
    ECHO Fully Qualified Path : %%~fA
)

If this isn't quite what you're looking for, check the "How To" section to see some other soutions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.