我需要帮助编写这个批处理脚本

时间:2013-02-03 05:34:46

标签: if-statement for-loop batch-file switch-statement

我想让这个脚本执行另一个脚本,如果一个文件名以一个东西开头,另一个如果它从另一个东西开始,并且如果它以其他任何东西开始则继续执行其余的代码。我希望它遍历文件夹中的所有文件并检查这一点。我已经研究了一段时间,我正在寻找一套清新的眼睛,因为显然我似乎无法让它正常工作。任何帮助将不胜感激!

 :========================================================================================
:: purpose: 1. copy file(s) from the export dir (SOURCE_DIR) (ARCHIVE_DIR)
::          2. copy file(s) from the export dir (SOURCE_DIR) (TARGET_DIR)
::          3. once file(s) are processed delete them from the export dir (SOURCE_DIR)
::=======================================================================================
::      
@echo off
setlocal
:: Display the command line on the window's Title Bar
title %0 %1
set LOG_FILE="here.txt"
set SOURCE_DIR=here\there
set TARGET_DIR=here\nowHere
set ARCHIVE_DIR=here\oldStuffnThings
set FILE=FileStartingWithBlahBlergOrOther.docx


echo --------------------------------------------------------------------------------------------------------------------------------------- >> %LOG_FILE%
echo %DATE% %TIME% Starting file move >> %LOG_FILE%


if not exist "%SOURCE_DIR%%FILE%" (
  echo %DATE% %TIME% "%SOURCE_DIR%%FILE%" not found >> %LOG_FILE%
  echo %DATE% %TIME% no files to move so stopping >> %LOG_FILE%
  exit
)


FOR /R %completepath% %%G IN (FileStartingWithBLAH*) DO (
  call C:\otherScript1
)

FOR /R %completepath% %%G IN (FileStartingWithBLERG*) DO (
  call C:\otherScript2
)





:: one at a time copy files to archive, copy to dir, then delete from the export dir
FOR /R %SOURCE_DIR% %%G IN (%FILE%) DO (
echo %DATE% %TIME% %%G was found for processing >> %LOG_FILE%
XCOPY %%G "%ARCHIVE_DIR%"/Y 
echo %DATE% %TIME% %%G was archived status %ERRORLEVEL% >> %LOG_FILE% 
XCOPY %%G "%TARGET_DIR%"/Y 
echo %DATE% %TIME% %%G was copied to  dir status %ERRORLEVEL% >> %LOG_FILE%
del %%G
echo %DATE% %TIME% %%G was deleted from the export dir status %ERRORLEVEL% >> %LOG_FILE%
)

rem set err_code=%errorlevel%
rem if %err_code% NEQ 0 (
rem    echo %DATE% %TIME% **ERROR moving files %err_code% >> %LOG_FILE%
rem    exit     
rem )
rem echo %DATE% %TIME% File(s) moved successfully >> %LOG_FILE%

rem echo %DATE% %TIME% Process Complete >> %LOG_FILE%

1 个答案:

答案 0 :(得分:0)

问题是您必须在SOURCE_DIR=here\there后添加反斜杠,否则它会查找here\thereFileStartingWithBlahBlergOrOther.docx