为什么我的批处理脚本跳过for循环中的某些文件夹?

时间:2016-01-20 22:19:07

标签: batch-file

我正在处理批处理脚本,该脚本以递归方式循环通过网络上的目录。对于它读取的每个文件,它会抓取路径和名称,将其写入文本文件,然后将其传输到Excel工作表。我有一些文件夹,我需要在迭代中跳过。这些都属于“其他如果”。但是,我有几个其他文件夹没有被迭代。

我应该提到它是每1.5小时通过任务调度程序执行的,并且在最初的几次执行中,脚本运行得很好,但后来它正在跳过文件夹。

请原谅代码的混乱,我在创建这个脚本时学会了Batch。为了使某些事情能够正常运作,必须要做一些奇怪的事情。我的代码在下面,带有一个虚拟文件夹。这是我在这里发表的第一篇文章,但我已经全神贯注地寻求解释但没有希望。我希望我没有违反任何规则。

::COMPANY NAME HERE
::MY NAME HERE - AUTOMATION INTERN
::
::An excel spreadsheet is generated containing a list of all subfiles for each project
::The spreadsheet is placed inside each project folder
::This script should run at scheduled times in order to remain updated
::

@echo off
::Make working directory begin where this file is located by pushing the path onto a stack
pushd %~dp0

::Prevent echo from happening before declaration
setlocal EnableDelayedExpansion

::Recursively access each folder inside the Projects folder
for /d /r %%G in ("\BRDATA1\Projects\*") DO (
    ::Change directory to current folder using short notation (to access deep folders)
    cd %%~sG
    ::Set cf to the name of the current folder
    set cf=%%~nG
    if "!cf!" == "Archive WIP" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol
    ) 2>nul else if "!cf!" == "Archives" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol
    ) 2>nul else if "!cf!" == "Help Document" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol
    ) 2>nul else if "!cf!" == "Recovered" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol
    ) 2>nul else if "!cf!" == "Templates" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol  
    ) 2>nul else if "!cf!" == "xxxx-Customer Name" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol
    ) 2>nul else if "!cf!" == "xxxx-Project Name" (
        ::DO NOT REMOVE
        ::FOR SOME REASON COMMENTS ARE NEEDED IN ORDER FOR THIS TO WORK... lol
    ) 2>nul else (
        echo Indexing Folder !cf!
        call :search
    )
)

::Pop the stack
echo Indexing Complete
popd

::Terminate execution
exit

:search
        ::Write each filepath inside the current folder into a temporary text file
        ::The filepath property is used and therefore efficiency is independent from file size
        dir /a-d /b /s /o:gn > list.txt
        ::Write headers on the excel output file
        echo FILENAME,FILE LOCATION > index.csv  
        ::For each filepath inside the temporary text file, Remove all text before the final \
        for /f "tokens=* delims=\" %%a in (list.txt) DO (
            ::Value = full path name (long)
            set value=%%a 
            echo !value! >nul
            ::New Value = Trimmed path name and only initial project folder name uses short path name
            set newValue=!value:*\Projects\=! 
            set newValue=!newValue:*\=!
            echo !newValue! >nul
            ::Write the filename including the extension, and then its trimmed path name 
            echo %%~na%%~xa , !newValue! >> index.csv
        )
        ::Delete the temporary text file and continue to the next project folder
        del "list.txt"

谢谢!

2 个答案:

答案 0 :(得分:0)

使用所有嵌套的If / else条件诊断逻辑流程更加困难。我建议从这个改变开始,看它是否解决了这个问题。

编辑:根据goto的每条评论更正。跟踪var中的条件。

pair

答案 1 :(得分:0)

您的代码非常复杂,很难在其上搜索错误,但已证明会导致问题的一点是在代码块中插入double-colon "comments";您应始终使用rem命令插入注释。下面有一个等效代码,它执行的代码与代码相同,但更简单:

@echo off
::Make working directory begin where this file is located by pushing the path onto a stack
pushd "%~dp0"

::Prevent echo from happening before declaration
setlocal EnableDelayedExpansion

::Define the list of folders to omit, enclosed by slashes
set "omit=/Archive WIP/Archives/Help Document/Recovered/Templates/xxxx-Customer Name/xxxx-Project Name/"

::Recursively access each folder inside the Projects folder
for /d /r "\BRDATA1\Projects" %%G in (*) DO (
    rem Change directory to current folder using short notation (to access deep folders^)
    cd "%%~sG"
    rem If current folder is not in the list of folders to omit
    if "!omit:/%%~nG/=!" equ "%omit%" (
        echo Indexing Folder %%~nG
        call :search
    )
)

::Pop the stack
echo Indexing Complete
popd

::Terminate execution
exit


:search

::Write each filepath inside the current folder into a temporary text file
::The filepath property is used and therefore efficiency is independent from file size
dir /a-d /b /s /o:gn > list.txt

(
rem Write headers on the excel output file
echo FILENAME,FILE LOCATION

rem For each filepath inside the temporary text file, Remove all text before the final \
for /f "tokens=* delims=\" %%a in (list.txt) DO (
   rem Value = full path name (long)
   set "value=%%a"
   echo !value! >nul

   rem New Value = Trimmed path name and only initial project folder name uses short path name
   set "newValue=!value:*\Projects\=!"
   set "newValue=!newValue:*\=!"
   echo !newValue! >nul

   rem Write the filename including the extension, and then its trimmed path name 
   echo %%~NXa , !newValue!
)
) > index.csv

::Delete the temporary text file and continue to the next project folder
del "list.txt"

exit /B
相关问题