2

File: 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%
)

This batch file is called with:

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

The execution results in output of the error message:

N was unexpected at this time.

There is no error if the line for /f "eol=: ... is commented out with command REM.

Delayed expansion is already enabled.

What do I need to do to fix the error?

Mofi
  • 46,139
  • 17
  • 80
  • 143
zoomraider
  • 117
  • 1
  • 9
  • Use `%%F` for the `metavariable` **F**. `metavariables` need to be single-`%` from the prompt, double within a batch file. Other than the `@echo off`, your `@command`s are superfluous. The `@echo off` sets `echo` off without reporting the command. The function of `@` is to not-report the command when executing. `@echo off` already does that, so you can remove all of the other `@`s. – Magoo Jan 30 '21 at 09:20
  • @Magoo this works for me. It `%%F` that was tripping thing up. – zoomraider Jan 31 '21 at 16:11

1 Answers1

0

It is not described what the batch file code should do at all. It looks like it should find the newest file by its name containing most likely a date string in name in a directory passed as argument to the batch file and should define an environment variable with name passed also as argument left to the directory path with the file name of newest file.

I suggest following commented batch code for this purpose:

@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

This batch file can be started for example with the command line:

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

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /? ... explains how to reference batch file arguments
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • shift /?

Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.

I suggest also reading:

Mofi
  • 46,139
  • 17
  • 80
  • 143