如何使用用户输入文件名和文件号创建1到N个文件?

时间:2017-11-12 15:42:21

标签: windows batch-file cmd

我的作业对明天非常重要。我必须创建一个批处理文件。

批次代码必须要求用户输入名称和号码。该数字必须大于0且小于100.

让我们说用户输入hello2。然后批处理文件应在当前目录中创建两个文件hello_1.txthello_2.txt

问题在于我不知道该怎么做而且它非常重要。我上周去过医院。所以我不能上学,错过了那些课程。

cmd窗口应类似于:

hello 2 

Creation of 2 files:

hello_1.txt
hello_2.txt 

这是我已经拥有的:

@echo off

set /p text=Enter a name
set /p number=Enter a number
set /a N=%number%
if %N% LSS 1 goto error
if %N% GTR 99 goto error
for /l %%i in (1,1,%N%) do (
    mkdir %text%_%N%.txt
)
goto end

:error
echo Check your number (between 1 and 100)

:end
echo Script OK

1 个答案:

答案 0 :(得分:2)

此任务的批处理文件的简单版本,对无效输入无法安全保护。

@echo off
set /P "text=Enter a name: "

:PromptNumber
set /P "number=Enter a number: "
set /A N=number 2>nul
if %N% LSS  1 goto PrintError
if %N% GTR 99 goto PrintError

echo/
for /L %%I in (1,1,%N%) do echo Create "%text%_%%I.txt" 2>"%text%_%%I.txt"
echo/
pause
goto :EOF

:PrintError
echo/
echo Check your number which must be between 1 and 100.
echo/
goto PromptNumber

如果在提示用户后未定义环境变量number,因为用户根本没有输入任何内容,则将值0分配给环境变量N。如果输入的字符串不是有效的十进制,八进制或十六进制数,也会发生这种情况。通过将其重定向到设备 NUL 来抑制无效数字字符串上处理 STDERR 的错误消息输出。

带有选项/L FOR 循环用于创建1中的文件,递增步长为1,值为N

ECHO 命令输出此刻创建的文件。 ECHO 命令没有错误输出,因为它不会失败,因此通过重定向命令的 STDERR 来创建0字节文件大小的文件是理想的候选者> ECHO 到要创建的文件。

我还有第二个版本,它可以更安全地防止无效或意外输入。我不建议将它呈现给您的老师,因为很明显批处理文件编码的新手没有写它。

@echo off
setlocal EnableExtensions EnableDelayedExpansion

:PromptName
set "FileName="
echo/
set /P "FileName=Enter a name: "

rem Has the user entered anything at all?
if not defined FileName goto PromptName

rem Remove all double quotes from file name string?
set "FileName=!FileName:"=!"

rem Is the file name string now an empty string?
if not defined FileName goto PromptName

rem The string can be still invalid as file name. So check on
rem first file creation if the file could be created successfully.
(set /P FileNumber=<nul >"!FileName!_1.txt") 2>nul
if not exist "!FileName!_1.txt" (
    echo/
    echo The string !FileName! is most likely not valid for a file name.
    goto PromptName
)

:PromptNumber
set "FileNumber="
echo/
set /P "FileNumber=Enter a number in range 1 to 99: "

rem Has the user entered anything at all?
if not defined FileNumber goto PromptNumber

rem Has the file number string any other character than digits?
for /F "delims=0123456789" %%I in ("!FileNumber!") do (
    echo/
    echo !FileNumber! is not a valid decimal number.
    goto PromptNumber
)

rem It is safe now to reference the file number consisting
rem only of digits 0-9 without usage of delayed expansion.

rem Has the file number more than two digits?
if not "%FileNumber:~2%" == "" (
    echo/
    echo %FileNumber% has more than two digits.
    goto PromptNumber
)

rem Remove first digit of number if the number has two digits and
rem the first digit is 0 to get the number later always interpreted
rem as expected as decimal number and not as octal number.
if not "%FileNumber:~1%" == "" if "%FileNumber:~0,1%" == "0" set "FileNumber=%FileNumber:~1%"

rem The file number is now in range 0 to 99. But 0 is not allowed.
if "%FileNumber%" == "0" (
    echo/
    echo Number 0 is not in valid range.
    goto PromptNumber
)

rem Create the remaining files. The first one was created already before.
echo/
echo Create "!FileName!_1.txt"
for /L %%I in (2,1,%FileNumber%) do echo Create "!FileName!_%%I.txt" 2>"!FileName!_%%I.txt"
echo/
endlocal
pause

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

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

另请阅读有关Using Command Redirection Operators的Microsoft文章,并查看Microsoft的command-line reference以及SS64的A-Z index of the Windows command line。 DosTips论坛主题ECHO. FAILS to give text or blank line - Instead use ECHO/解释了为什么在两个批处理文件echo/中使用而不是经常使用echo.来将空行打印到控制台。