Windows 从命令行接受变量以搜索批处理文件中的日志文件

Windows 从命令行接受变量以搜索批处理文件中的日志文件,windows,batch-file,command-line,Windows,Batch File,Command Line,我的任务如下: 1. Create a batch file that does the following: - Accepts from the command line the following variables: - Year (four digit number) of log files that they want to archive - Month (two-digit number) of log files that they want to a

我的任务如下:

1. Create a batch file that does the following:
    - Accepts from the command line the following variables:
    - Year (four digit number) of log files that they want to archive
    - Month (two-digit number) of log files that they want to archive
    - Does not display any of the commands to the screen
    - Display just the files names of all of the files in the directory and subdirectories that have the archive attribute turned on in a “wide” display on the screen.
    - Display the message “Beginning archive utility at” and then display the current date and time.
    - Create a subdirectory called archive. If this directory already exists, do not report out an error message.
    - If there are files that correspond to the month and year:
         - Create a subdirectory in the archive directory that corresponds to the month and year entered (six digit number using the format yyyymm)
         -  Move all files with a file name that begins with the corresponding month and year entered (six digit number using the format yyyymm)
        -  If there are not any files that correspond to the month and year entered
         - Returns an error message to explain what happened and the type of input that was expected
          - Turn off the archive attribute of only the files that have been moved.
         - Display just the files names of all of the files in the directory and subdirectories that have the archive attribute turned off.

我不完全理解这是什么意思,我在谷歌上搜索了帮助,但他向我指出了资源。我也为此尝试过堆栈溢出文章,但我不明白如何接受“来自命令行的变量”是不是我必须执行set/p%%?但是,我如何通过它来搜索文件夹中包含这些日志文件的文件呢?

下面是一个答案,它涵盖了您发布的项目
1的第一行
8
以及一些艺术许可证:

@Echo关闭
Rem从命令行接受四位数的年份和两位数的月份。
:询问
Rem清除控制台。
ClS
Rem取消定义任何名为YYYY的现有变量。
设置“YYYY=”
Rem要求用户输入1970年至2019年之间的年份。
设置/P“YYYY=请输入四位数年份,例如2016>”
Rem Return再次询问用户是否在1970年至2019年之间未输入四位数年份。
回音“%YYYY%”第19[7-9][0-9][0-9][20[0-1][0-9][0-9]\“$”>NUL|GoTo AskYear
:ask月
Rem清除控制台。
ClS
Rem取消定义任何名为MM的现有变量。
设置“MM=”
Rem要求用户输入两位数的月份。
设置/P“MM=请输入两位数的月份,例如05>”
Rem Return再次询问用户是否未输入两位数的月份。
回音“%MM%”搜索结果/R“^\”0[1-9]\“$^\”1[0-2]\“$”>NUL | |转到AskMonth
Rem以宽格式显示具有存档属性的所有文件。
目录/A-DA/W 2>NUL | FindStr/VBC:“
Rem显示消息,后跟当前日期和时间。
Echo(&Echo)在%TIME%于%DATE%开始存档实用程序(&Echo)&Echo(
Rem创建没有错误消息的子目录。
MD“存档”2>NUL
我希望这足以帮助您了解到这一点的要求。从那以后,您将需要确定是否存在任何文件
%YYYY%%MM%
开始,如果存在,则从
1项的第
10行开始。


请根据需要修改判决或删除
Rem
标记。

在没有看到整个问题/任务的情况下,我们的解决方案可能不相关。您应该从命令提示符运行批处理文件,向其发送两个参数
YYYY
MM
,还是您的批处理文件应该询问最终用户输入这两个参数中的每一个,然后使用输入的内容。(从指向您的页面,我假设是前者。)另外,一些指示您正在搜索的文件名到底是什么样子的,以及在哪里对我们有帮助。暂停批处理文件以向最终用户询问每个参数,然后使用键入的内容搜索包含不同日期的日志文件的文件夹。它还说,不应向scr显示任何命令行即使我认为这意味着@echo off。我可以将整个内容添加到帖子中。谢谢,教授提供了一个“日志”的zip文件,其名称为日期,即20180122.txt,因此我将调整此文件以归档这些日志,它们将重定向回文件夹。谢谢!
@echo off
If %1Err==Err Goto InputError
If %2Err==Err Goto InputError

cls
dir /w /aa
pause
echo Beginning archive utility at
date /t
time /t
REM Create a subdirectory called archive. If this directory already exists, do not report out an error message. 
If not exist archive md archive
cd archive

REM Create a subdirectory in the archive directory that corresponds to the month and year entered (six digit number using the format yyyymm) 
If not exist %1%2 md %1%2
cd ..

REM Move all files with a file name that begins with the corresponding month and year entered (six digit number using the format yyyymm)
If not exist %1%2* Goto NoFiles
move %1%2* archive\%1%2\

REM Turn off the archive attribute of only the files that have been moved.
attrib -A archive\%1%2\*

DIR /S /A-A

Goto TheEnd

:NoFiles
echo There are not files that begin with %1%2
Goto TheEnd


:InputError
echo when running this command please include a four digit year and a two digit month (Ex: COMMAND 2010 12).
Goto TheEnd

:TheEnd
Echo On