Windows 使用命令行列出文件的多种模式
在我的工作目录中,我有以检索的年、月、日和小时命名的文件,如下所示:Windows 使用命令行列出文件的多种模式,windows,cmd,Windows,Cmd,在我的工作目录中,我有以检索的年、月、日和小时命名的文件,如下所示: 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 19980
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
文件,但我不知道如何省略不需要的文件(从第1天开始,我只需要19980101.15.nc、19980101.18.nc和19980101.21.nc文件)。此外,我还没有弄清楚如何将第二天的文件名(02个文件)和第一天的文件名(01个文件)合并到一个列表中
有什么想法吗?这个问题很不清楚,但这里有一个注释过的批处理文件,它会提示用户输入文件日期,如
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/?
获取/?
goto/?
如果/?
暂停/?
rem/?
设置/?
setlocal/?
dir
,然后列出您感兴趣的8个文件,怎么样?或者将这些文件复制到它们自己的目录并使用dir*
?如何定义“所需”?因此,您需要8个“01”文件中的3个和8个“02”文件中的5个。你有每天的清单吗?如果没有-如何决定哪些文件?