1

I have a Windows batch file that process a bunch of files. As part of this I use the following line:

forfiles /p "%~dpn1%LogDir%" /m "%SupportLog%*" /c "cmd /c logreader.py @file > \"%~dpn1%ParsedLogDir%\@file_Logreader.txt\"

This works OK, but essentially loops through all my files (%SupportLog%*) and passes each one by one into the "logreader.py" script.

What I really want to do is create a list or parameter of all these files and pass all of them at once into the Python script, such the the command that should be run would resemble:

logreader.py "logfile.log" "logfile.log.1" "logfile.log.3" .....

I tried to use the SET command within the forfile command such that:

forfiles /p "%~dpn1%LogDir%" /m "%SupportLog%*" /c "cmd /c set PARAMS=%PARAMS%@file "

However, when run this and leave the ECHO ON, I see:

forfiles /p "C:\Path\log" /m "logfile.log*" /c "cmd /c set PARAMS=@file "

Which is incorrect. And when I "echo %PARAMS%", I get no result.

Is there a way of achieving this?

Many thank

1

1 Answer 1

1

Changing environment variables in a child cmd instance (that's what the cmd /c invokes) does not affect environment in the parent cmd process. Next code snippet could help:

SETLOCAL EnableExtensions EnableDelayedExpansion
set "LogDir=%~dpn1log"         my guess only
set "SupportLog=logfile.log"   my guess only
set "PARAMS="                  initial value
rem 1st try for /R "%LogDir%" %%a in ("%SupportLog%*") do set PARAMS=!PARAMS! "%%~a"
rem 2nd try for %%a in ("%LogDir%\%SupportLog%*") do set "PARAMS=!PARAMS! "%%~a""
rem 3rd try
for %%a in ("%LogDir%\%SupportLog%*") do set "PARAMS=!PARAMS! "%%~nxa""
echo logreader.py %PARAMS%

Note that imperative logreader.py is merely echoed for debugging purposes.

However, I don't apprehend target of > redirection, sorry...

Resources (required reading):

Edit:

3
  • Hi Jose, this is almost perfect, although the the for /r seems to recurse the folder tree. Now, this is OK, the first time I run this batch file as I can set the For loop early in the batch that evaluates the folder before I create a subfolder to store the parsed file, but if I then run the batch again, if picks up file in the sub folder. (sorry for the multiple comments, I couldn't quite get the hang of these ;()
    – Swinster
    Sep 26, 2015 at 17:27
  • Doh. Stupid boy! Just remove the /r and set the SET to the correct directory. Many thanks, all done, from what I can see. Maybe I should start looking at PowerShell to build such things in the future.
    – Swinster
    Sep 26, 2015 at 17:59
  • 1
    For reference, the full line that worked was For %%A in ("%~dpn1%LogDir%\%SupportLog%*") do ( set PARAMS=!PARAMS! "%%A" ). This is used in conjunction with passing a file into the batch file in the first place (via a SentTo short cut)
    – Swinster
    Sep 26, 2015 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.