IF和ELSE IF代码帮助-批处理脚本 [英] IF and ELSE IF code help - Batch Scripting

查看:114
本文介绍了IF和ELSE IF代码帮助-批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅需要以下示例批处理脚本代码中的if条件方面的帮助,如何实现if和else if条件?如果在运行inw Windows命令时无法在我的.bat文件中识别:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // constant variables
set "_ROOT=%~dp0." & rem // (common root directory; `%~dp0.` is script's parent, `.` is current)
set "_DIR1=%_ROOT%\Directory1" & rem // (1st directory containing files)
set "_DIR2=%_ROOT%\Directory2" & rem // (2nd directory containing files)
set _MASKS="*INV*" "*SLS*"     & rem // (list of quoted file masks)
set "_TMP1=%TEMP%\%~n0_1_%RANDOM%.tmp" & rem // (1st temporary file)
set "_TMP2=%TEMP%\%~n0_2_%RANDOM%.tmp" & rem // (2nd temporary file)

rem // Resolve 1st directory to be an absolute path:
for %%E in ("%_DIR1%") do set "RDIR1=%%~fE"
rem // Resolve 2nd directory to be an absolute path:
for %%E in ("%_DIR2%") do set "RDIR2=%%~fE"

rem // Write all matching files in 1st directory to 1st temporary file:
pushd "%RDIR1%" && (
    for %%F in (%_MASKS%) do (
        echo(%%~nxF
    )
    popd
) > "%_TMP1%"
rem // Write all matching files in 2nd directory to 2nd temporary file:
pushd "%RDIR2%" && (
    for %%F in (%_MASKS%) do (
        echo(%%~nxF
    )
    popd
) > "%_TMP2%"

rem // Loop over all common files from both temporary files:
for /F %%L in ('findstr /L /I /X /G:"%_TMP1%" "%_TMP2%"') do (
    rem // Build absolute `wmic`-compatible file paths:
    set "FILE1=%RDIR1:\=\\%\\%%L" & set "FILE2=%RDIR2:\=\\%\\%%L"
    setlocal EnableDelayedExpansion
    rem set "FILE1=%!FILE1:&=&!" & set "FILE2=%!FILE2:&=&!"
    rem // Get standardised file date/time (last modification) of 1st file by `wmic`:
    for /F %%K in ('wmic DataFile where "Name='!FILE1!'" get LastModified') do set "DATE1=%%K"
    rem // Get standardised file date/time (last modification) of 2nd file by `wmic`:
    for /F %%K in ('wmic DataFile where "Name='!FILE2!'" get LastModified') do set "DATE2=%%K"
    rem // Compare file dates/times (last mod.) of both files and return differing ones:

    if !DATE1! gtr !DATE2! (echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!")
   else if !DATE1! leq !DATE2! (echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!")
   else if !DATE1! equ !DATE2! (echo "!FILE1:\\=\!" is same with  "!FILE2:\\=\!")


    endlocal
)

rem // Clean up temporary files:
del "%_TMP1%" "%_TMP2%"

endlocal
exit /B

这将是非常有帮助的.谢谢

解决方案

使用lss代替leq.因此,我们将具有小于" 大于" 等于" ,这似乎是您想要的.

在这种情况下使用else的正确方法是:

if !DATE1! gtr !DATE2! (
     echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!"
   ) else if !DATE1! lss !DATE2! (
          echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!"
    ) else if !DATE1! equ !DATE2! (
           echo "!FILE1:\\=\!" is same with  "!FILE2:\\=\!"
)

但是,在这种情况下我们不需要else,因为每一行都会被评估,直到返回true为止,因此您可以这样做:

if !DATE1! gtr !DATE2! echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!"
if !DATE1! lss !DATE2! echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!"
if !DATE1! equ !DATE2! echo "!FILE1:\\=\!" is same with  "!FILE2:\\=\!"

我建议您阅读帮助,打开cmd.exe并运行:

  • if /?

i just need a help on the if condition in the below sample batch script code, how do I implement an if and else if condition? if else doesnt recognize in my .bat file when running inw windows command:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // constant variables
set "_ROOT=%~dp0." & rem // (common root directory; `%~dp0.` is script's parent, `.` is current)
set "_DIR1=%_ROOT%\Directory1" & rem // (1st directory containing files)
set "_DIR2=%_ROOT%\Directory2" & rem // (2nd directory containing files)
set _MASKS="*INV*" "*SLS*"     & rem // (list of quoted file masks)
set "_TMP1=%TEMP%\%~n0_1_%RANDOM%.tmp" & rem // (1st temporary file)
set "_TMP2=%TEMP%\%~n0_2_%RANDOM%.tmp" & rem // (2nd temporary file)

rem // Resolve 1st directory to be an absolute path:
for %%E in ("%_DIR1%") do set "RDIR1=%%~fE"
rem // Resolve 2nd directory to be an absolute path:
for %%E in ("%_DIR2%") do set "RDIR2=%%~fE"

rem // Write all matching files in 1st directory to 1st temporary file:
pushd "%RDIR1%" && (
    for %%F in (%_MASKS%) do (
        echo(%%~nxF
    )
    popd
) > "%_TMP1%"
rem // Write all matching files in 2nd directory to 2nd temporary file:
pushd "%RDIR2%" && (
    for %%F in (%_MASKS%) do (
        echo(%%~nxF
    )
    popd
) > "%_TMP2%"

rem // Loop over all common files from both temporary files:
for /F %%L in ('findstr /L /I /X /G:"%_TMP1%" "%_TMP2%"') do (
    rem // Build absolute `wmic`-compatible file paths:
    set "FILE1=%RDIR1:\=\\%\\%%L" & set "FILE2=%RDIR2:\=\\%\\%%L"
    setlocal EnableDelayedExpansion
    rem set "FILE1=%!FILE1:&=&!" & set "FILE2=%!FILE2:&=&!"
    rem // Get standardised file date/time (last modification) of 1st file by `wmic`:
    for /F %%K in ('wmic DataFile where "Name='!FILE1!'" get LastModified') do set "DATE1=%%K"
    rem // Get standardised file date/time (last modification) of 2nd file by `wmic`:
    for /F %%K in ('wmic DataFile where "Name='!FILE2!'" get LastModified') do set "DATE2=%%K"
    rem // Compare file dates/times (last mod.) of both files and return differing ones:

    if !DATE1! gtr !DATE2! (echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!")
   else if !DATE1! leq !DATE2! (echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!")
   else if !DATE1! equ !DATE2! (echo "!FILE1:\\=\!" is same with  "!FILE2:\\=\!")


    endlocal
)

rem // Clean up temporary files:
del "%_TMP1%" "%_TMP2%"

endlocal
exit /B

This would be a very much help. Thanks

解决方案

Use lss instead of leq. So we will have "less than", "greater than" and "equal to", which seems to be what you want.

The correct method of using else in this instance would be:

if !DATE1! gtr !DATE2! (
     echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!"
   ) else if !DATE1! lss !DATE2! (
          echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!"
    ) else if !DATE1! equ !DATE2! (
           echo "!FILE1:\\=\!" is same with  "!FILE2:\\=\!"
)

However, We do not need else in this instance as each line will be evaluated until one returns true, so you could just do:

if !DATE1! gtr !DATE2! echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!"
if !DATE1! lss !DATE2! echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!"
if !DATE1! equ !DATE2! echo "!FILE1:\\=\!" is same with  "!FILE2:\\=\!"

I suggest you read the help, open cmd.exe and run:

  • if /?

这篇关于IF和ELSE IF代码帮助-批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆