0

I am having an issue with the following Windows batch script. I have multiple XML files in the %indir% that I want to import one at a time and email the report. Basically the section starting with "if /I %v_continue% == y" is not executing as expected. I did some debugging step by step with echo on, and it goes through the script till the %BLAT% command without executing any of the steps, and then it starts execution with the first "copy %script_path%.....". It executes the import.exe correctly, but then nothing gets assigned to %subj% and subsequently the %BLAT% command fails. Any advice?

Thanks!

@echo off

set environment=%1
set domain=%2

if [%environment%] == [] goto :endofscript

rem - Get the script path
set script_path=%~dp0
rem - Get the script name without the extension
set script_name=%~n0
rem - Get the script name with the extension
rem set script_name=%~nx0
rem - get the script extension
set script_ext=%~x0

rem - Set environment variables
call %script_path%\setenv.cmd

set cnt=0
set filemask=*.xml

for /f %%a in ('dir /b /a-d %indir%\%filemask%') do call :procfile %%a
goto :EOF

:procfile
set impfile=%1
set v_continue=n
set emailyn=y



set trset=%impfile:~0,3%
set /A cnt + = 1
if 1%cnt% lss 100 set cnt=0%cnt%

if %trset% == RCT (
   set "subtxt=Receipt Confirmation"
   set v_continue=y
)

if %trset% == SHP (

   set "subtxt=Shipment Confirmation"
   set v_continue=y

   setlocal EnableDelayedExpansion

   set MOFound=
   for /f "tokens=3 delims= " %%f in ('find /i /c "<RefID>MO-ORD</RefID>" %indir%\%impfile%') do (set MOFound=%%f)

   if !MOFound! GTR 0 (
      copy %indir%\%impfile% %inarchdir%
      move %indir%\%impfile% %S_INDIR%\FX%dttmstamp%%cnt%.xml 2>NUL
      goto :EOF
   )

   endlocal

)

if /I %v_continue% == y (

   copy %script_path%\%script_name%.dat %infile%
   cscript %REPLACEVBS% %infile% "DOMAIN"   "%domain%"   1>NUL 2>&1

   cd /d %rptdir%

   %DLC%\bin\import.exe -b -T d:\tmp -p %pfile%

   rem - Check for errors

   find /i /c "ERROR:" %rptfile% > NUL

   if %ERRORLEVEL% NEQ 0 (
      set "subj=SUCCESS: %subtxt% Import Report (%environment%/%domain%)"
      set emailyn=y
   ) else (
      set "subj=ERROR: %subtxt% Import Report (%environment%/%domain%)"
      set emailyn=y
   )

   move %rptfile% %logdir%\%script_name%_%datestamp%_%cnt%.prn 2>NUL
   move %outfile% %logdir%\%script_name%_%datestamp%_%cnt%.out 2>NUL

   if /I %emailyn% == y (
         echo Report location: %logdir%\%script_name%_%datestamp%_%cnt%.prn > %msgfile%
         %BLAT% %msgfile% -server abc-com.mail.protection.outlook.com -f donotreply@abc.com -s "%subj%" -t %INBEMAIL% -attachi %logdir%\%script_name%_%datestamp%_%cnt%.prn
   )

)

del /f /q %infile%
del /f /q %pfile%
del /f /q %msgfile%

:delfiles

rem - Delete log files that are older than 10 days.
PushD "%logdir%" && (
   forfiles /M %script_name%_*.prn /D -10 /C "CMD /C del /f /q @PATH" 2>NUL
) & PopD

:endofscript

exit /B
user3152289
  • 107
  • 8
  • You've set variables inside of parentheses, but you forgot to [enable delayed expansion](http://stackoverflow.com/questions/30282784/combining-all-mp4s-in-directory-with-ffmpeg/30284028#30284028). – SomethingDark Jun 02 '15 at 02:37
  • I tried different things with "setlocal EnableDelayedExpansion", but for some reason I can't get it to work – user3152289 Jun 02 '15 at 03:16
  • Stick it at the very top of your script, right under `@echo off`. Then use `!subj!` instead of `%subj%`. – SomethingDark Jun 02 '15 at 03:18
  • Thanks - that worked!!!! – user3152289 Jun 02 '15 at 03:46
  • Be careful since you conditionally run `endlocal`. If will remove all your env settings that were set between `setlocal` and `endlocal`. – Okkenator Jun 02 '15 at 09:50

0 Answers0