0

I have list of file names which contains .xml, .css, .js, .jsp, .java etc. I want a code that will allow you execute your function only when that file have .js or .jsp or .java extension.I don't know how to check muliple condition in if statement. that's why adding as OR..kindly help me..Thank you

   FOR /F "delims=#" %%A in (demo.txt) do (

    IF [/I] %%~xA==".js" OR %%~xA==".css" OR %%~xA==".java"

    (

    echo correct format

    )

    ELSE

    (

    ECHO incorrect format

    ) 

)
npocmaka
  • 55,367
  • 18
  • 148
  • 187
user2918831
  • 455
  • 1
  • 6
  • 9

4 Answers4

1

Create a variable that contains a list of all valid extensions. Be sure to include an extra dot at the end, such that all extensions are enclosed by dots. Then for each value, attempt search and replace on the list, replacing the found extension (plus dot) with nothing. If the result is different than the original list, then the extension is valid.

@echo off
setlocal enableDelayedExpansion
set "formats=.css.js.jsp.java."
for /f "delims=#" %%A in (demo.txt) do (
  if "!formats:%%~xA.=!" neq "!formats!" (
    echo %%~xA is correct format
  ) else (
    echo %%~xA is incorrect format
  )
)

See IF… OR IF… in a windows batch file for additional methods to implement OR logic.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • simpler than mine attempt.+1 – npocmaka Oct 25 '13 at 14:19
  • Thank you so much...your code running perfectly..Thanks alot :) – user2918831 Oct 25 '13 at 14:35
  • @user2918831 - Don't forget to accept the answer you find most helpful if it completely answers your question. Simply click on the check mark in the upper left of the anser. That action lets others know the question has been answered, awards you 2 reputation points, awards the answer poster 15 rep points. Only one answer can be accepted per question. Once you reach 15 rep points you will be able to up vote any answer that you find useful, including answers to other people's questions. – dbenham Oct 25 '13 at 15:12
1
    setlocal enableDelayedExpansion
    FOR /F "delims=#" %%A in (demo.txt) do (
      set "format=incorect"
      for %%E in (.js;.java;.jsp;.css) do (
         if "%%~xA" == "%%~E" (         
           set "format=correct"
         )
      )
      if "!format!" equ "correct" (
        echo correct format
      ) else (
        echo incorrect format
      )

    )
   endlocal
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

If file list in demo.txt as indicated by OP

for /F "tokens=*" %%f in ('type demo.txt ^| findstr /i /e ".js .java .jsp"') do (
    rem here what needs to be done
    echo %%f
)

If files in directory, replace type demo.txt with dir /b .... adjusting the directory to process.

MC ND
  • 69,615
  • 8
  • 84
  • 126
0

try this:

@ECHO OFF &SETLOCAL
for /f "delims=#" %%a in (demo.txt) do (
    set "flag="
    if /i "%%~xa"==".js" set "flag=true"
    if /i "%%~xa"==".css" set "flag=true"
    if /i "%%~xa"==".java" set "flag=true"
    if defined flag (echo %%~xa is valid) else echo %%~xa is not valid
)
Endoro
  • 37,015
  • 8
  • 50
  • 63