windows在批处理文件中迭代通过目录中的文件返回错误

时间:2016-03-24 09:22:34

标签: windows batch-file cmd

我正在创建一个.bat文件,它遍历特定​​目录中的文件。这是代码的一部分:

@echo off

 setlocal ENABLEDELAYEDEXPANSION

 set DSPath=C:\Axway_DS
 set inFolder=!DSPath!\Datastore\getStarted\withDatastore\data
 set fileToTreat=!inFolder!\
 set outFolder=C:\Users\acucu\Desktop\folders\exit\arn\out
 set DSRuntime=!DSPath!\Datastore\DatastoreRuntime
 set archive=C:\Users\acucu\Desktop\folders\exit\arn\archive

 set fileIn=deleteInput.txt
 set logSuppresion=supp.log
 set logMaintenance=maintenance.log
 set fichierTrace=!outFolder!\TRSUPIN.txt
 set case=SUPPRESSION

 for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
 set mytime=%time%
 set mytime2=%time::=%
 set mytime2=%mytime2:.=%

cd !folderIn!
for %%i in (*) do ( 
echo Am intrat
set fileIn=%%i
set COUNTER=0
for /f "tokens=*" %%a in (!fileIn!) do (

set /A COUNTER+=1

  set countRows=0   
  set idInjection=%%a

      ......................

)
)

endlocal

问题出现在这里:

 C:\Users\acucu\Desktop\folders\exit\arn>cd !folderIn!
 The syntax of the command is incorrect.

执行停止。 我究竟做错了什么? 现在我在上次检查的旧版本上遇到了同样的错误:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set DSPath=C:\Axway_DS
set inFolder=!DSPath!\Datastore\getStarted\withDatastore\data
set fileToTreat=!inFolder!\
set outFolder=C:\Users\acucu\Desktop\folders\exit\arn\out
set DSRuntime=!DSPath!\Datastore\DatastoreRuntime
set archive=C:\Users\acucu\Desktop\folders\exit\arn\archive

set fileIn=deleteInput.txt
set logSuppresion=supp.log
set logMaintenance=maintenance.log
set fichierTrace=!outFolder!\TRSUPIN.txt
set case=SUPPRESSION

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
set mytime=%time%
set mytime2=%time::=%
set mytime2=%mytime2:.=%
set COUNTER=0

  for /f "tokens=*" %%a in (!fileIn!) do (

set /A COUNTER+=1

set countRows=0 
set idInjection=%%a

::Suppresion de l'injection
call !DSRuntime!\dstools deleteCollection -i %%a > !logSuppresion!_!COUNTER! 2>&1

IF !errorlevel!==0 (
    set result=OK 
    set comm= 
    ) ELSE (
     set result=KO
        for /f "delims=" %%x in (!logSuppresion!_!COUNTER!) do (
        set "comm=%%x"  )
        )


 call !DSRuntime!\dstools.bat maintenance -tasks CLEAN_COLLECTION > !logMaintenance!_!COUNTER!
    set /a count=0
    for /f "tokens=3 delims=:" %%i in ('find "Deleted row(s) count:" !logMaintenance!_!COUNTER!') do set /a count=%%i

    set countRows=!count!   


     ::Creation des dossiers s'ils n'existent pas
     if not exist !outFolder! mkdir !outFolder!
     if not exist %archive% mkdir %archive!%            

    ::Alimentation des variables avec des blancs  
    set countRows=!countRows!                  
    set fileIn=!fileIn!                                                                     
    set idInjection=!idInjection!                                                  
    set comm=!comm!                                                                                                    

    ::Creation du fichier de trace d'accompagnement
    if exist !fichierTrace! set            fichierTrace=!outFolder!\TRSUPIN_!mydate!_!mytime2!.txt 


    echo. 2>!fichierTrace!
    echo !case:~0,11!!countRows:~0,18!!fileIn:~0,50!!mydate!!mytime2:~0,-2!!idInjection:~0,50!!result!!comm:~0,100! > !fichierTrace!


    ::Copier les fichiers dans l'archive
    ::xcopy /q !fichierTrace! !archive!\%mydate%_%mytime2%\TRSUPIN.txt >nul
    ::xcopy /q !fileToInject! !archive!\%mydate%_%mytime2%\TRSUPIN.txt >nul

    ::Supprimer le fichier de log injection
    ::del !logMaintenance!_!COUNTER!
    ::del !logSuppresion!_!COUNTER!

   )
  endlocal

1 个答案:

答案 0 :(得分:0)

如果问题是delayedExpansion,请在命令之前添加;

setlocal enableDelayedExpansion

如果是folderIn中的空格,请在cd命令中的变量周围添加引号;

cd "!folderIn!"

如果路径不完整,请务必完成;

cd "X:\Example\SubFolder\!folderIn!"

根据需要合并这些内容,或者让我们了解更多信息。

修改

错误匹配的变量名称? 试试

cd "!inFolder!"