I am working on a batch script that cycles recursively through a directory on a network. With each file it reads, it grabs the path and name, writes it to a text file, and then transfers it to an excel sheet. I have a few folders that I need to skip over in the iteration. Those are within the "else if"s. However, I have several additional folders that are not being iterated through.
I should mention that it is being executed via task scheduler every 1.5 hours, and during the first few executions the script worked perfectly, but then later it is skipping folders.
Excuse the mess of code, I learned Batch whilst creating this script. In order for some things to work properly, weird things had to be done. My code is below, with a dummy folder. This is my first post on here, but I've looked all over for an explanation but had no hope. I hope I didn't break any rules.
::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"
Thanks!