错误信息“此时出现 N 是意外的”是什么原因?

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

文件:

stack.bat

@echo off
@setlocal enabledelayedexpansion
for %%a in (%*) do (
    call set "%%~1=%%~2"
    shift
)

ECHO para1 %--para1%
ECHO para2 %--para2%

if "%--para2%"=="" (
    echo missing para2
    for /f "eol=: delims=" %F in ('dir /b/o:N %--folder%\*.001') do @set "newest=%F"
    echo latest %newest%
)

此批处理文件的调用方式为:

stack.bat --para1 c:\Sample\temp

执行结果输出错误信息:

此时N出乎意料。

如果使用命令

REM
注释掉 for /f "eol=: ... 行,则不会出现错误。

延迟扩展已启用。

我需要做什么来修复错误?

batch-file cmd
1个回答
0
投票

根本没有描述批处理文件代码应该做什么。看起来它应该通过名称找到最新的文件,该文件的名称很可能包含作为参数传递给批处理文件的目录中的日期字符串,并且应该定义一个环境变量,其名称也作为参数传递给文件的目录路径最新文件的名称。

我建议为此目的使用以下注释的批处理代码:

@echo off
rem Remove this line if the environment variables defined by this batch
rem file should still exist after processing of this batch file finished.
setlocal EnableExtensions DisableDelayedExpansion

rem Delete all environment variables of which name starts with --.
for /F "delims=" %%I in ('set -- 2^>nul') do set "%%I"

:ProcessArguments
rem Get current first argument (option) with surrounding " removed.
set "Option=%~1"
rem Is there no more option?
if not defined Option goto EndBatch
rem Remove all double quotes from the option.
set "Option=%Option:"=%"
rem This condition is just for 100% fail safe code. It should be never true.
if not defined Option goto EndBatch

rem Does the option not start with two hyphens?
if not "%Option:~0,2%" == "--" (
    echo ERROR: Invalid option: "%Option%"
    echo(
    goto EndBatch
)

rem Get current second argument (folder path) with surrounding " removed.
set "Folder=%~2"
rem Is there no folder path?
if not defined Folder goto MissingFolder
rem Remove all double quotes from the folder path.
set "Folder=%Folder:"=%"
rem This condition is just for 100% fail safe code. It should be never true.
if not defined Folder goto MissingFolder

rem Replace all / in folder path by \ as many users type folder paths wrong
rem with slashes as on Linux/Mac instead of backslashes as required on Windows.
set "Folder=%Folder:/=\%"
rem Make sure the last character of folder path is a backslash.
if not "%Folder:~-1%" == "\" set "Folder=%Folder%\"

rem Search in specified folder for *.001 files output reverse by name and
rem define the option as environment variable with first output file name
rem assigned with full qualified absolute path even if environment variable
rem Folder referencing a relative path. Then shift the arguments list by
rem two arguments to the left and process the remaining arguments.
for /F "eol=| delims=" %%I in ('dir "%Folder%*.001" /A-D /B /O-N 2^>nul') do (
    for %%J in ("%Folder%%%I") do set "%Option%=%%~fJ"
    shift
    shift
    goto ProcessArguments
)

rem It is not possible to define an environment variable with no string.
rem So an error message is output if no file could be found like on
rem wrong folder path or no *.001 file found in the specified folder.
echo ERROR: Could not find a *.001 file for option "%Option%" in folder:
echo        "%Folder%"
echo(
goto Endbatch

:MissingFolder
echo ERROR: Missing folder path for option: "%Option%"
echo(

:EndBatch
set "Option="
set "Folder="
echo Options parsed successfully:
echo(
set -- 2>nul

rem Remove this line if the environment variables defined by this batch
rem file should still exist after processing of this batch file finished.
endlocal

可以使用命令行启动此批处理文件:

 stack.bat --para1 c:\Sample\temp "--para2" "C:/Temp/Development & Test!/" --para3 . --para4 "\Program Files\Internet Explorer\" -para5 .."

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

  • call /?
    ...解释如何引用批处理文件参数
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • shift /?

阅读有关 使用命令重定向运算符的 Microsoft 文档,了解

2>nul
的说明。重定向运算符
>
必须在
FOR
命令行上使用脱字符号 ^ 进行转义,以便在 Windows 命令解释器在执行命令 FOR(执行嵌入的
dir
命令行)之前处理此命令行时将其解释为文字字符使用在后台启动的单独命令进程,并使用
%ComSpec% /c
'
中的命令行作为附加参数附加。

我建议还阅读:

© www.soinside.com 2019 - 2024. All rights reserved.