使用命令行列出文件的多种模式

问题描述 投票:-1回答:1

在我的工作目录中,我有以检索的年,月,日和小时命名的文件,如下所示:

19980101.00.nc 19980101.03.nc 19980101.06.nc 19980101.09.nc
19980101.12.nc 19980101.15.nc 19980101.18.nc 19980101.21.nc
19980102.00.nc 19980102.03.nc 19980102.06.nc 19980102.09.nc
19980102.12.nc 19980102.15.nc 19980102.18.nc 19980102.21.nc
19980103.00.nc 19980103.03.nc 19980103.06.nc 19980103.09.nc
19980103.12.nc 19980103.15.nc 19980103.18.nc 19980103.21.nc

使用Windows命令行,我希望能够只使用这些文件:

19980101.15.nc  |
19980101.18.nc  |
19980101.21.nc _|_ # these are day 01 files
19980102.00.nc  |
19980102.03.nc  |
19980102.06.nc  |
19980102.09.nc  |
19980102.12.nc _|_ # these are day 02 files

到目前为止,我已经使用命令:dir 19980101.*.nc以列出19980101文件,但我不知道如何省略非所需文件(我只需要19980101.15.nc,19980101.18.nc和19980101.21.nc文件形式第01天)。此外,我还没想出如何在单个列表文件中组合第二天(02文件)的名称和第一天的文件(01文件)。

有什么想法吗?

windows cmd
1个回答
0
投票

这个问题非常不清楚,但是这里仍然是一个评论批处理文件,提示用户输入文件日期,如19980102,确定输入日期的前一天,并运行DIR,打印8个文件。

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Prompt user for date string until a number string with exactly 8 digits is entered.

:EnterFileDate
set "FileDate="
set /P "FileDate=Enter date in format YYYYMMDD: "

rem Has the user nothing entered at all?
if not defined FileDate goto EnterFileDate

rem Has the user entered a string not consisting of only digits?
for /F "delims=0123456789" %%I in ("!FileDate!") do goto EnterFileDate

rem Has the user entered a too long string?
if not "%FileDate:~8%" == "" goto EnterFileDate

rem Has the user entered a too short string?
if "%FileDate:~7%" == "" goto EnterFileDate

rem Has the user entered a string not starting with 19 or 20 for century?
if not "%FileDate:~0,2%" == "19" if not "%FileDate:~0,2%" == "20" goto EnterFileDate

rem Don't care of possible other invalid date strings, but
rem check if the entered date string matches any existing file.
if not exist "%FileDate%.??.nc" (
    echo/
    echo There is no file "%FileDate%.??.nc".
    echo/
    goto EnterFileDate
)

rem Discard local environment and pass entered file date
rem to previous environment restored by command ENDLOCAL.
endlocal & set "FileDate=%FileDate%"

set "PrevYear=%FileDate:~0,4%"
set "PrevMonth=%FileDate:~4,2%"

rem Get day in month subtracted by 1 with avoiding to get 01 to 09
rem interpreted as octal number by concatenating this two digits
rem of day in month first with 1 for integer number 101 to 132.
set /A PrevDay=1%FileDate:~6,2% - 101

rem Is day in month greater or equal 1, month must not be changed.
if %PrevDay% GEQ 1 goto BuildPrevDate

set /A PrevMonth=1%PrevMonth% - 101

rem Is month less than 1, year must be changed too.
if %PrevMonth% LSS 1 (
    set /A PrevYear-=1
    set "PrevMonth=12"
    set "PrevDay=31"
    goto BuildPrevDate
)

rem Determine last day in new month and year.
if %PrevMonth% ==  1 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% ==  3 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% ==  4 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% ==  5 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% ==  6 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% ==  7 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% ==  8 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% ==  9 set "PrevDay=30" & goto BuildPrevDate
if %PrevMonth% == 10 set "PrevDay=31" & goto BuildPrevDate
if %PrevMonth% == 11 set "PrevDay=30" & goto BuildPrevDate

rem Leap year finding for the years 1901 to 2099.
set /A LeapYear=PrevYear %% 4
if %LeapYear% == 0 (
    set "PrevDay=29"
) else (
    set "PrevDay=28"
)
set "LeapYear="

:BuildPrevDate
if %PrevDay% LSS 10 set "PrevDay=0%PrevDay%"
if %PrevMonth% LSS 10 set "PrevMonth=0%PrevMonth%"
set "PrevDate=%PrevYear%%PrevMonth%%PrevDay%"
set "PrevDay="
set "PrevMonth="
set "PrevYear="

echo/
dir %PrevDate%.15.nc %PrevDate%.18.nc %PrevDate%.21.nc %FileDate%.00.nc %FileDate%.03.nc %FileDate%.06.nc %FileDate%.09.nc %FileDate%.12.nc
set "FileDate="
set "PrevDate="
echo/
pause

要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
© www.soinside.com 2019 - 2024. All rights reserved.