multiple IF else conditions issue

橙三吉。 提交于 2020-03-05 04:00:35

问题


I have an issue where I want to segregate / split file based on search item into multiple files.
if I see add I want to put that line to addfile. similar to changeprop, insert and if I cannot find any match then to otherfile. I'm unable to process the if else conditions.

FOR /F "tokens=* delims=" %%a in ('TYPE "%File%"') DO (
    SET "eachline=%%a"
    IF /i "!eachline:~0,3!" == "ADD " (ECHO !eachline!>>"%ADDActionScriptsMergedFile%")
    ELSE ( IF /i "!eachline:~0,9!" == "ADDINSERT" (ECHO !eachline!>>"%ADDINSERTActionScriptsMergedFile%")
        ELSE ( IF /i "!eachline:~0,3!" == "MOV" (ECHO !eachline!>>"%MOVEActionScriptsMergedFile%")
            ELSE ( IF /i "!eachline:~0,6!" == "INSERT" (ECHO !eachline!>>"%INSERTPROPActionScriptsMergedFile%")
                ELSE ( IF /i "!eachline:~0,10!" == "CHANGEPROP" (ECHO !eachline!>>"%CHANGEPROPActionScriptsMergedFile%")
                    ELSE (ECHO !eachline!>>"%OTHERPROPActionScriptsMergedFile%")
                )
            )
        )
    )
)

回答1:


As stated in my comment, your syntax for IF/ELSE is incorrect. The general syntax is that there should always be a space before any opening code block parenthesis, whereas spaces following a closing code block parenthesis are not strictly problematic, even if technically not correct. ELSE is not an individual command, it is dependent upon IF, so it must be read in the same line.

Example based upon your latest provided code:

FOR /F "TOKENS=* DELIMS=" %%a IN ('TYPE "%File%"')DO (SET "eachline=%%a"
    IF /I "!eachline:~0,3!" == "ADD" (>>"%ADDActionScriptsMergedFile%" ECHO !eachline!
    )ELSE (IF /I "!eachline:~0,9!" == "ADDINSERT" (>>"%ADDINSERTActionScriptsMergedFile%" ECHO !eachline!
        )ELSE (IF /I "!eachline:~0,3!" == "MOV" (>>"%MOVEActionScriptsMergedFile%" ECHO !eachline!
            )ELSE (IF /I "!eachline:~0,6!" == "INSERT" (>>"%INSERTPROPActionScriptsMergedFile%" ECHO !eachline!
                )ELSE (IF /I "!eachline:~0,10!" == "CHANGEPROP" (>>"%CHANGEPROPActionScriptsMergedFile%" ECHO !eachline!
                    )ELSE >>"%OTHERPROPActionScriptsMergedFile%" ECHO !eachline!
                )
            )
        )
    )
)

*You'll note that I changed IF /i "!eachline:~0,3!" == "ADD " ( to IF /I "!eachline:~0,3!" == "ADD" (, because I assumed it was a typo, ("!eachline:~0,3!" is always 3 characters, excluding the doublequotes, so it can never equal the four characters in "ADD ").




回答2:


Give this a try. It is based on my previous answer, but changed a bit to avoid writing the same code several times (two times was ok, but now, that it's 5 (or maybe even more), it makes sense to use a for loop):

@echo off
setlocal
set "File=.\WorkFlow_Action_Script_Merged_Folder\All_WorkFlows_Merged_File.txt"
set "Dest=.\WorkFlow_Action_Script_Merged_Folder\@_WorkFlows_Merged_File.txt"
for %%a in ("Add " Addinsert Mov Insert Changeprop) do (
  type "%File%" | findstr /bic:"%%~a" > "%Dest:@=%%~a%"
)
type "%File%" |findstr /bivc:"Addinsert" /bivc:"Mov" /bivc:"Insert" /bivc:"Changeprop" /bivc:"Add " > "%Dest:@=OTHERPROP%"

(untested, as I don't have your files or folderstructure)



来源:https://stackoverflow.com/questions/60229655/multiple-if-else-conditions-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!