2

My issue is simple, I want to run log parser in batch, I however haven no experience dealing with multiple files or folders.

I have been putting together this code and so far got this:

echo %1
SETLOCAL ENABLEDELAYEDEXPANSION
set INPUTDIR=%~dp0\
set OUTPUTDIR=%INPUTDIR%ParseOutput\
For %%A in (%*) do (
    Set Folder=%%~dpA
    Set Name=%%~nxA
    GO TO PROCESS
)

ECHO %Name%
:PROCESS
set INPUTFILE=%Name%
set OUTPUTFILE=%Name%parsed.txt
set OUTPUTTARGET=%OUTPUTDIR%%OUTPUTFILE%
set INPUTTARGET=%INPUTDIR%%INPUTFILE%

logparser "SELECT DISTINCT date, time, X-Forwarded-For,cs-username, sc-status INTO %OUTPUTTARGET% FROM %INPUTTARGET%" -i:W3C -o:W3C

PAUSE

exit /B

What result I want is that if I drag and drop several files, they will output the parsed files , one for each input file . However if I put only one fil everything is fine , but if I put several files it just does the first. I knwo my error is on the :process , but how do I go back from the :process back to the loop and continue where I left?

Victor J Mytre
  • 313
  • 1
  • 7

1 Answers1

0

The folder path of the batch file referenced with %~dp0 always ends with \. So there should be never a backslash after %~dp0 independent where %~dp0 is used in a batch file.

It is not recommended to enable delayed environment variable expansion if not really needed because of doing so results in double parsing every command line in batch file and ! is interpreted everywhere as begin/end of a delayed expanded environment variable reference including exclamation marks in file and directory names.

The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains the reasons for using syntax set "variable=value" on assigning a string value to an environment variable. File and folder names can contain characters with special meaning in batch files like space or one of these characters &()[]{}^=;!'+,`~ which requires enclosing file/folder strings in " to get these characters interpreted literally by Windows command processor. Run in a command prompt window cmd /? and read output help, especially the last section on last output help page.

GO TO is not a valid command. This can be seen on debugging the batch file by executing it from within a command prompt window to see the error message output by cmd.exe processing the batch script. It is possible to run in a command prompt window help to get displayed a list of Windows Commands with a brief description for each command. All internal commands of cmd.exe and most external commands (*.com or *.exe in %SystemRoot%\System32) can be executed in a command prompt window with /? as argument to get displayed the help for this command. More details about the Windows commands can be read at SS64.com - A-Z index of the Windows CMD command line.

The usage of correct written command GOTO would not help to process all files passed as arguments to the batch file referenced with %* because of this command results in leaving FOR loop and continue batch file processing on the line below the line with :PROCESS. The help for command CALL explains how to use this command to run a subroutine. See also Where does GOTO :EOF return to?

In general it is advisable on using a FOR loop to avoid assigning strings get by referencing the loop variable without or with a modifier to environment variables if that is not necessary because it is possible to use these strings directly in command line(s) to execute by FOR. That makes it possible to avoid delayed environment variable expansion or usage of a subroutine which can make script execution much faster.

I don't know anything about logparser. Is it an executable with file extension .exe or .com or a batch file with file extension .bat or .cmd? Where is this file stored? I assume for the code below that this file is an executable located in directory of the batch file and therefore reference it with full path, file name and file extension using "%~dp0logparser.exe". But if logparser is a batch file stored in same directory as the batch file already running, it would be necessary to use call "%~dp0logparser.bat".

I don't know if the single argument string passed to logparser is really correct. I have some doubts on correct passing the argument(s) to this executable or script, especially if file or directory names of input or output file contain a space character. However, the code below use what is written as working in question.

The batch code below takes everything written above into account.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "InputDirectory=%~dp0"
set "OutputDirectory=%InputDirectory%ParseOutput\"
md "%OutputDirectory%" 2>nul
for %%I in (%*) do "%~dp0logparser.exe" "SELECT DISTINCT date, time, X-Forwarded-For,cs-username, sc-status INTO %OutputDirectory%%%~nI_parsed.txt FROM %InputDirectory%%%~nxI -i:W3C -o:W3C
endlocal
pause

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

  • echo /?
  • endlocal /?
  • for /?
  • md /?
  • pause /?
  • set /?
  • setlocal /?

See also Microsoft article about Using command redirection operators.

Mofi
  • 38,783
  • 14
  • 62
  • 115