Batch Script Issue - How do I convert the dates or suggest any logic to satisfy the IF statement requirements in the given code?

雨燕双飞 提交于 2019-12-28 04:36:05

问题


I have this already initialized batch script codes running in bat file:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
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! lss !DATE2! echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!"
    if !DATE1! gtr !DATE2! echo "!FILE1:\\=\!" is newer than "!FILE2:\\=\!"
    if !DATE1! equ !DATE2! echo "!FILE1:\\=\!" and "!FILE2:\\=\!" are of same age
    endlocal
)

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

endlocal
exit /B

And I am getting this native logic issue, assuming that DATE1 has date of March 3/19/2019 09:23 AM and DATE2 has date of March 3/19/2019 10:03 AM suppose that the expected output be that Date1 has older date with Date2 but in the output it says Date1 is the same date with Date2.

here's the echo on process with what are the values in the dates:

CMD Shell:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\ksaycon>cd Desktop

C:\Users\ksaycon\Desktop>sample.bat
"C:\Users\ksaycon\Desktop\Directory1\FFPDFINV06" and "C:\Users\ksaycon
\Desktop\Directory2\FFPDFINV06" are of same age

C:\Users\ksaycon\Desktop>(
rem // Build absolute `wmic`-compatible file paths:
 set "FILE1=C:\\Users\\ksaycon\\Desktop\\Directory1\\FFPDFSLS02"   & set "FILE2=
C:\\Users\\ksaycon\\Desktop\\Directory2\\FFPDFSLS02"
 setlocal EnableDelayedExpansion
 rem set "FILE1=&=&!" & set "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:\\=\!"
 if !DATE1! LSS !DATE2! echo "!FILE1:\\=\!" is older than "!FILE2:\\=\!"
 if !DATE1! equ !DATE2! echo "!FILE1:\\=\!" and "!FILE2:\\=\!" are of same age
 endlocal
)

C:\Users\ksaycon\Desktop>set "DATE1=LastModified"

C:\Users\ksaycon\Desktop>set "DATE1=20190318040549.218407+480"

" \Users\ksaycon\Desktop>set "DATE1=

C:\Users\ksaycon\Desktop>set "DATE2=LastModified"

C:\Users\ksaycon\Desktop>set "DATE2=20190318095134.760320+480"

" \Users\ksaycon\Desktop>set "DATE2=
"C:\Users\ksaycon\Desktop\Directory1\FFPDFSLS02" and "C:\Users\ksaycon
\Desktop\Directory2\FFPDFSLS02" are of same age

C:\Users\ksaycon\Desktop>rem // Clean up temporary files:

C:\Users\ksaycon\Desktop>del "C:\Users\ksaycon\AppData\Local\Temp\sample_1_13650
.tmp" "C:\Users\ksaycon\AppData\Local\Temp\sample_2_27021.tmp"

C:\Users\ksaycon\Desktop>endlocal

C:\Users\ksaycon\Desktop>exit /B

C:\Users\ksaycon\Desktop>^A

来源:https://stackoverflow.com/questions/55232770/batch-script-issue-how-do-i-convert-the-dates-or-suggest-any-logic-to-satisfy

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