【问题标题】:Accept variables from the command line to search through log files in a batch file从命令行接受变量以在批处理文件中搜索日志文件
【发布时间】:2020-02-19 22:03:12
【问题描述】:

我的作业如下:

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.

我不完全理解这是什么意思,我已经在谷歌上寻求帮助,但他将我指向资源 https://ss64.com/nt/syntax-args.html。我也为此尝试过堆栈溢出文章,但我不明白如何接受“来自命令行的变量”我必须做一个 set /p %% 吗?但是我如何通过它来搜索文件夹中包含这些日志文件的文件?

【问题讨论】:

  • 没有看到整个问题/任务,我们的解决方案可能不相关。你应该从命令提示符运行一个批处理文件,向它发送两个参数YYYYMM 还是你的批处理文件应该询问最终用户输入这两个参数中的每一个,然后使用输入了什么。 (从您指向的页面来看,我假设是前者。)另外,某种指示您正在搜索的文件名的确切外观以及对我们有帮助的地方。跨度>
  • 批处理文件会询问最终用户的每个参数,然后使用输入的内容来搜索一个文件夹,其中包含不同日期的日志文件。它还说不应在屏幕上显示任何命令行,我认为这意味着@echo off。我可以将整个内容添加到帖子中

标签: windows batch-file command-line


【解决方案1】:
@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

【讨论】:

    【解决方案2】:

    这是一个涵盖您发布项目1. 的第一行8 的答案具有一些艺术许可

    @Echo Off
    
    Rem Accept from the command line a four digit year and a two digit month.
    
    :AskYear
    Rem Clear the console.
    ClS
    Rem Undefine any existing variable named YYYY.
    Set "YYYY="
    Rem Ask the user to input a year between 1970 and 2019.
    Set /P "YYYY=Please enter a four digit year e.g. 2016 >"
    Rem Return to ask again if the user did not enter a four digit year between 1970 and 2019.
    Echo "%YYYY%"|FindStr /R "^\"19[7-9][0-9]\"$ ^\"20[0-1][0-9]\"$">NUL||GoTo AskYear
    
    :AskMonth
    Rem Clear the console.
    ClS
    Rem Undefine any existing variable named MM.
    Set "MM="
    Rem Ask the user to input a two digit month. 
    Set /P "MM=Please enter a two digit month e.g. 05 >"
    Rem Return to ask again if the user did not enter a two digit month.
    Echo "%MM%"|FindStr /R "^\"0[1-9]\"$ ^\"1[0-2]\"$">NUL||GoTo AskMonth
    
    Rem Display all files with archive attibute in wide format. 
    Dir /A-DA/W 2>NUL|FindStr /VBC:" "
    
    Rem Display message followed by current date and time.
    Echo(&Echo Beginning archive utility at %TIME% on %DATE%&Echo(
    
    Rem Create subdirectory without error messages.
    MD "archive" 2>NUL
    

    我希望这足以帮助您了解当时的要求。从那里您将需要确定if 任何以%YYYY%%MM% 开头的文件exist,如果是这样,请从项目1.10 行开始

    如有必要,请随时修改量刑或删除Remarks。

    【讨论】:

    • 这项工作谢谢你,教授提供了一个“日志”的 zip 文件,其名称为日期,即 20180122.txt,所以我将对其进行调整以存档这些日志,它们将重定向回文件夹。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多