user2918831
user2918831

Reputation: 485

How to compare file format in windows batch script

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

    ) 

)

Upvotes: 0

Views: 2077

Answers (4)

Endoro
Endoro

Reputation: 37579

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
)

Upvotes: 0

npocmaka
npocmaka

Reputation: 57272

    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

Upvotes: 1

MC ND
MC ND

Reputation: 70943

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.

Upvotes: 1

dbenham
dbenham

Reputation: 130879

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.

Upvotes: 1

Related Questions