在Windows批处理作业上工作并获取变量错误

时间:2018-11-27 13:55:29

标签: windows batch-file

这是我的代码:

@echo on
setlocal 
Rem *
for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%i%%j%%k
echo %d% " Starting" > D:\Stratus\WaterSewer\Bills2IC.log
Dir D:\Stratus\WaterSewer\*.zip >> Bills2IC.log
for %%f in (D:\Stratus\WaterSewer\*.zip) do set N=%%~nxf
if exists D:\Stratus\WaterSewer\%N% (    
  echo %N% >> D:\Stratus\WaterSewer\Bills2IC.log
  rename %N% coc_ub_%d%.zip
)
fi
echo %d% " Completed" >> D:\Stratus\WaterSewer\Bills2IC.log
endlocal

当我运行这段代码时,我得到了:

send_bill2ic.bat
Rem * JCN 10/25/2018 - Batch file for SFTP to Invoice Cloud
Rem *
setlocal
Rem *
for /F "tokens=2,3,4 delims=/ " %i in ('date/t') do set d=%i%j%k
set d=11272018
echo 11272018 " Starting"  1>D:\Stratus\WaterSewer\Bills2IC.log
Dir D:\Stratus\WaterSewer\*.zip  1>>Bills2IC.log
for %f in (D:\Stratus\WaterSewer\*.zip) do set N=%~nxf
set N=COC_UBIC_1126.zip
D:\Stratus\WaterSewer\COC_UBIC_1126.zip was unexpected at this time.
if exists D:\Stratus\WaterSewer\COC_UBIC_1126.zip (

在此先感谢您的帮助 约翰

1 个答案:

答案 0 :(得分:0)

您的语法有几个错误。请检查以下代码:

@echo on
setlocal 
Rem *
for /F "tokens=2-4 delims=/ " %%i in ('date/t') do set "d=%%i%%j%%k"
echo %d% " Starting" > D:\Stratus\WaterSewer\Bills2IC.log
dir D:\Stratus\WaterSewer\*.zip >> Bills2IC.log
set "N="
for %%I in (D:\Stratus\WaterSewer\*.zip) do set "N=%%~nxI"
  echo %N% >> D:\Stratus\WaterSewer\Bills2IC.log
  rename %N% coc_ub_%d%.zip
echo %d% " Completed" >> D:\Stratus\WaterSewer\Bills2IC.log
endlocal

希望这会有所帮助!

如果您遇到任何问题,请通知我,因为由于测试本地位置导致我做错了事。