-1

I need to be able to report on the number of files in each redTAG folder within our 'shared drive'

each main directory in our departments shared drive has a redTAG folder. I need a batch file that will go through all folders and sub folders from a start point that contain 'redTAG' and report back a file count for those directories.

so the following structure:

root - 10 files, 
root/redTAG - 2 files, 
root/deliveries/ - 4 files, 
root/deliveries/redTAG - 5 files, 
root/deliveries/help - 4 files, 

would report back:

root/redTAG                2,
root/deliveries/redTAG     5

code provided was my last successful attempt at any analysis of the folders etc.

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
(Echo Folders            #Sub  #Files   ##Sub ##Files
 FOR /D %%G in (*) DO (
   PUSHD "%%G"
   Set /A "Sub#=Files#=0,SUB##=Files##=0" 
   Set "Folders=%%~G                  "
   FOR /F %%H in ('dir /a-d /b 2^>NUL^|find /C /V "" ') DO Set Files#=%%H
   FOR /F %%I in ('dir /ad  /b 2^>NUL^|find /C /V "" ') DO Set Sub#=%%I
   if !Sub#! gtr 0 (
      FOR /F %%H in ('dir /a-d /b /S 2^>NUL^|find /C /V "" ') DO Set Files##=%%H
      FOR /F %%I in ('dir /ad  /b /S 2^>NUL^|find /C /V "" ') DO Set Sub##=%%I
      Set /A "Files##-=Files#,Sub##-=Sub#"
   )
   Set    "Sub#=       !Sub#!"
   Set  "Files#=       !Files#!"
   Set   "Sub##=       !Sub##!"
   Set "Files##=       !Files##!"
   Echo !Folders:~,15! !Sub#:~-7! !Files#:~-7! !Sub##:~-7! !Files##:~-7!
   POPD 
)) > "count.txt"
start count.txt

any of my subsequent attempts have met with failure and hit the back of the bin. I don't need the export to a file but it helps. Some of this code was inherited from a previous colleague who wasn't great at annotation or help

Karrde
  • 3
  • 2
  • I suggest you to start reading [this answer](https://stackoverflow.com/questions/8397674/windows-batch-file-looping-through-directories-to-process-files/8398621#8398621)... – Aacini Aug 08 '19 at 14:24

1 Answers1

0

This task could be done for example with:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
(for /F "delims=" %%I in ('dir redTAG /AD /B /S 2^>nul') do (
    set "Folder=%%I"
    set "FileCount=0"
    for /F "eol=| delims=" %%J in ('dir "%%I" /A-D /B 2^>nul') do set /A FileCount+=1
    call echo "%%Folder:\=/%%",%%FileCount%%
))> "count.csv"
endlocal

I don't know why output file count.csv should contain the folder paths with Linux/Mac directory separator / instead of Windows directory separator \, but the code replaces all backslashes by slashes in folder path before it is output.

The output file is a CSV file. The output format as posted in question would require much more code, but I don't see a real reason for this very special text format. The folder paths are enclosed in double quotes to produce a valid CSV file even if a folder path contains a comma.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?

Well, call is used here just to force Windows command processor to parse the echo command line a second time on each iteration of outer for loop in addition to first time parsing done already on parsing entire block starting with first ( and ending with last ).

See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

This trick with call avoids usage of delayed expansion to process also redTAG folders correct containing one or more ! in folder path.

Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on both FOR command lines to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.

Mofi
  • 38,783
  • 14
  • 62
  • 115