Windows批处理脚本问题

Windows批处理脚本问题,windows,batch-file,cmd,windows-scripting,Windows,Batch File,Cmd,Windows Scripting,我对以下Windows批处理脚本有问题。我在%indir%中有多个XML文件,我希望一次导入一个并通过电子邮件发送报告。基本上,以“if/I%v_continue%==y”开头的部分没有按预期执行。我在echo打开的情况下一步一步地进行了一些调试,它在不执行任何步骤的情况下通过脚本一直执行到%BLAT%命令,然后它开始执行第一个“copy%script_path%”。它正确地执行import.exe,但是没有为%subc%分配任何内容,因此%BLAT%命令失败。有什么建议吗 谢谢 @echo o

我对以下Windows批处理脚本有问题。我在%indir%中有多个XML文件,我希望一次导入一个并通过电子邮件发送报告。基本上,以“if/I%v_continue%==y”开头的部分没有按预期执行。我在echo打开的情况下一步一步地进行了一些调试,它在不执行任何步骤的情况下通过脚本一直执行到%BLAT%命令,然后它开始执行第一个“copy%script_path%”。它正确地执行import.exe,但是没有为%subc%分配任何内容,因此%BLAT%命令失败。有什么建议吗

谢谢

@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
@echo关闭
设置环境=%1
设置域=%2
如果[%environment%]==[]转到:endofscript
rem-获取脚本路径
设置脚本路径=%~dp0
rem-获取不带扩展名的脚本名称
设置脚本名称=%~n0
rem-获取带有扩展名的脚本名称
rem集合脚本_name=%~nx0
rem-获取脚本扩展名
设置脚本_ext=%~x0
rem-设置环境变量
调用%script\u path%\setenv.cmd
设置cnt=0
设置filemask=*.xml
对于('dir/b/a-d%indir%\%filemask%')中的/f%%a,请调用:procfile%%a
后藤:EOF
:procfile
设置文件=%1
设置v_continue=n
设置为y=y
set trset=%impfile:~0,3%
设置/A cnt+=1
如果1%cnt%lss 100设置cnt=0%cnt%
如果%trset%==RCT(
设置“subtext=收货确认”
设置v_continue=y
)
如果%trset%==SHP(
设置“subtext=装运确认”
设置v_continue=y
setlocal EnableDelayedExpansion
找到=
对于/f“tokens=3 delims=“%%f in('find/i/c“MO-ORD”%indir%\%impfile%”)do(set MOFound=%%f)
如果!MOFound!全球技术法规0(
复制%indir%\%impfile%%inarchir%
移动%indir%\%impfile%%S\u indir%\FX%dttmstamp%%cnt%.xml 2>NUL
后藤:EOF
)
端部
)
如果/I%v\u继续%==y(
复制%script\u path%\%script\u name%.dat%infle%
cscript%REPLACEVBS%%infle%%“域”“%DOMAIN%”1>NUL 2>&1
cd/d%rptdir%
%DLC%\bin\import.exe-b-td:\tmp-p%p文件%
rem-检查错误
查找/i/c“错误:“%rptfile%”NUL
如果%ERRORLEVEL%NEQ 0(
设置“sub=SUCCESS:%subtext%导入报告(%environment%/%domain%)”
设置为y=y
)否则(
设置“sub=错误:%subtext%导入报告(%environment%/%domain%)”
设置为y=y
)
移动%rptfile%%logdir%\%script\u name%\u%datestamp%\u%cnt%.prn 2>NUL
移动%outfile%%logdir%\%script\u name%\u%datestamp%\u%cnt%%out 2>NUL
如果/I%emailyn%==y(
回显报告位置:%logdir%\%script\u name%\%datestamp%\%cnt%.prn>%msgfile%
%BLAT%%msgfile%%服务器abc-com.mail.protection.outlook.com-fdonotreply@abc.com-s“%subc%”-t%inbeail%-attachi%logdir%\%script\u name%\%datestamp%\%cnt%.prn
)
)
del/f/q%填充%
del/f/q%p文件%
del/f/q%msgfile%
:delfiles
rem-删除超过10天的日志文件。
PushD“%logdir%”和(
forfiles/M%script\u name%\u*.prn/D-10/C“CMD/C del/f/q@PATH”2>NUL
)&PopD
:endofscript
退出/B

您已经在括号内设置了变量,但您忘记了。我尝试了使用“setlocal EnableDelayedExpansion”的不同方法,但由于某些原因,我无法让它在脚本顶部的
@echo off下勾选它。然后使用
!Subc而不是
%sub%
。谢谢-这很有效!!!!小心,因为您有条件地运行
endlocal
。If将删除在
setlocal
endlocal
之间设置的所有环境设置。