# Solved: Batch file to check for a series of folders, and write results



## JTS123 (Dec 4, 2012)

I work on multiple networks with multiple projects, each project having multiple study types. I have a list of all projects, but I do not have a list of all study types. Each project has a unique key (number), and each project has a unique name (letter). I'm looking to make a batch file which will search the network and generate a list (*.CSV works well) of all projects and a "Yes" or "No" next to study type.

A typical file path would be: J:\12345\abc\, where 12345 is the unique project number, and abc would be the study type. I have a list of all projects I would want to use (in CSV format at the moment, but can be converted to another version if needed), I also have a list of all study types (abc, def, xyz, etc.).

The output I would like would tell me the project name and the study types it has. This can be something as simple as "Search <all projects> for ABC" and repeating for DEF, XYZ, etc., and outputting "Yes" or "No" (or blank) next to the project number for that specific study type. Or it could be something as complex as "Search <all projects> for ABC, DEF, XYZ, etc." and an output of "Project 12345 Yes Yes No Yes Yes No No" for all the different study types.

Known elements:
- All project numbers (not all projects on the network are important to me, but they don't need to be excluded from this search if it's a pain to exempt them / read from a specific list).
- All study type names
- Network paths (sometimes in J:\ format, sometimes in \\XYZ-abc-123\ format).

Thoughts on how I might accomplish this? I've found a few batch files to recognize if a file or folder exist, but none on how to first do multiple searches, and second write the results in a single file.

Thanks in advance.

JTS


----------



## foxidrive (Oct 20, 2012)

The kind of details needed as I see it are:

How many drives/shares are being searched for instance?
The study types are a set of folders which are at the next level below the project folder - is that right?

so you have something like this folder structure representing a project number and letter, and three study types - is that right?

j:\B54321\abc
j:\B54321\def
j:\B54321\xyz

or this:

\\server\share\C84321\abc
\\server\share\C84321\jhi
\\server\share\C84321\vwx


----------



## JTS123 (Dec 4, 2012)

Drives being searched: 1
Shared servers being searched: 9
I'd rather not have an all-encompassing batch file for searching all 10 spots. I'd like to be able to run these individually as needed.

Folder structure:
Projects: J:\1234567\ -- it's always like this. Typical length is 7 numbers. Very rarely do these names have letters in them.
Study types have two versions: J:\12345\STUDY-TYPE --or-- J:\12345\randomletters\STUDY-TYPE. More often than not, it's the latter, and typically those random letters are "Fmg".

A typical folder setup often looks like this:
J:\1234567\Fmg\Study1
J:\1234567\Fmg\Study2
J:\1234567\Fmg\Study3

\\server\share\name\1234567\Fmg\Study1
\\server\share\name\1234567\Fmg\Study2
\\server\share\name\1234567\Fmg\Study3

I have about 30 different study types. Each study type gets its own folder, and the folder is always named the study type. Not all study types are included in each project folder.


----------



## foxidrive (Oct 20, 2012)

Specifying the task properly is the hardest part - anyone that attempts to provide a custom batch file needs a good picture of the actual details, to determine which method will be the simplest and most appropriate and to avoid any pitfalls.

Does j:\1234567\ contain any more folders other than study type folders, or a single random folder that contains study type folders?

Do the study type folders contain even more folders?


----------



## JTS123 (Dec 4, 2012)

Agreed. All of my attempts at this batch file have ended... poorly.

Yes. J:\1234567\ does contain other folders. These would be project details - correspondence folders, contact folders, etc. There should be no duplicates of the study type folders. Only one instance of STUDY1, STUDY2, etc. I can't say for sure as I have thousands of projects. The study types are a combination of letters and numbers, typically 6 characters long.

The old folder structure was J:\1234567\<all studies & all other folders>, which became very messy. The newer file structure now puts all studies in the "Fmg" folder -- J:\1234567\Fmg\<all studies>.

Yes. The study folders do contain subfolders. 50% of these are standard folder names, 50% are unique folder names.

A batch file to list out all folders within J:\1234567\ would still be useful for me. Output would be CSV, Job_Number,Folder1,Folder2,Folder3 (including non-study folders). I can filter out everything from there.


----------



## foxidrive (Oct 20, 2012)

This should provide you with a csv format file with the path and folders - it creates the file in the current folder with a filename format of of "project_1234567.csv"
The details are on a single line without headers.

Run the batch file without any parameters for a brief help.


```
@echo off
set "project=%~1"
if not defined project (
   echo Please supply details of a share or drive letter and project
   echo.
   echo Use this format: "%~nx0" "j:\1234567" or  "%~nx0" "\\server\share\name\1234567"
   echo.
   pause
   goto :EOF
)

for /f "delims=" %%a in ('dir "%project%" /b /s /ad 2^>nul ') do (
   echo Found %%a
   set "projfolder=%%a"
   set "proj=%%~nxa"
)
if not defined proj echo Project "%project%" can't be located & pause & goto :EOF

set "csv=project_%proj%.csv"
>"%csv%" set /p ="%projfolder%," <nul

setlocal enabledelayedexpansion
echo Checking for folders
for /f "delims=" %%a in ('dir "%projfolder%" /b /s /ad ') do (
set "folder=%%a"
set "folder=!folder:%projfolder%\=!"
>>"%csv%" set /p ="!folder!," <nul
)
echo Done.
pause
```


----------



## JTS123 (Dec 4, 2012)

I'm not sure where to place the J:\ or server name in that.


----------



## foxidrive (Oct 20, 2012)

batchfilename "j:\1234567"

or 

batchfilename "\\server\share\name\1234567"


----------



## foxidrive (Oct 20, 2012)

You can use it to search a few items like this:


```
@echo off
call batchfilename "j:\1234567"
call batchfilename "\\server\share\85319"
call batchfilename "j:\435112"
```
Or to search j: for several project numbers this will work


```
@echo off
for %%a in (
12345
9876554
368251
8746372
) do call batchfilename "j:\%%a"
```
Remove the pause on the last line of the batch file to use it without a keypress


----------



## JTS123 (Dec 4, 2012)

I can't get this to run. I've set up a test case for it, located at C:\Users\jts\Desktop\Test. This folder contains subfolders (123, 456, etc.), as well as the batch file itself (test.bat). When I run the batch file, the screen remains blank - no messages, no errors, and no .csv in the main folder.

Here is the file I'm running:


```
@echo off
test.bat C:\Users\jts\Desktop\Test
set "project=%~1"
if not defined project (
   echo Please supply details of a share or drive letter and project
   echo.
   echo Use this format: "%~nx0" "j:\1234567" or  "%~nx0" "[URL="file://\\server\share\name\1234567"]\\server\share\name\1234567[/URL]"
   echo.
   pause
   goto :EOF
)
for /f "delims=" %%a in ('dir "%project%" /b /s /ad 2^>nul ') do (
   echo Found %%a
   set "projfolder=%%a"
   set "proj=%%~nxa"
)
if not defined proj echo Project "%project%" can't be located & pause & goto :EOF
set "csv=project_%proj%.csv"
>"%csv%" set /p ="%projfolder%," <nul
setlocal enabledelayedexpansion
echo Checking for folders
for /f "delims=" %%a in ('dir "%projfolder%" /b /s /ad ') do (
set "folder=%%a"
set "folder=!folder:%projfolder%\=!"
>>"%csv%" set /p ="!folder!," <nul
)
echo Done.
pause
```


----------



## foxidrive (Oct 20, 2012)

Two issues exist here, as I see it.

If this is called test.bat and the following is the first two lines then it's going to keep looping and executing itself.

@echo off 
test.bat "C:\Users\jts\Desktop\Test"

Remove that second line.

I presume you are trying to locate the test folder, so what you would do in another batch file, or from the command line, is to execute something like this: (if in a batch file then prefix it with the call keyword)

test.bat "C:\Test"

or

test.bat "C:\Users\Test"

Look in the folder with the batch file for the .csv file


----------



## JTS123 (Dec 4, 2012)

Great, that works. I made a batch file which listed the main directory and pointed it towards the original batch file you made.

And now that I know how this runs, two problems have come up. The first is that regardless of the number of folders I'm pointing it at, there is only one folder listed in the .csv -- the last folder it found. Could this be changed so all file paths are listed?

The second problem is that if I run this for multiple folders, it creates multiple .csv files. Could this all be summed in to one .csv?


----------



## foxidrive (Oct 20, 2012)

Bear with me, am checking on some issues you raised...


----------



## JTS123 (Dec 4, 2012)

One problem solved - the file now writes only one .csv. Instead of adding the line you said, I replaced it - there seems to be no difference between having it or not.

But I'm still only getting the full path of the last folder found. The batch file does give a readout of all folders and their paths when it runs though.

Maybe we're using two different batch files? This is what I'm using right now:



> @echo off
> set "project=%~1"
> if not defined project (
> echo Please supply details of a share or drive letter and project
> ...


----------



## foxidrive (Oct 20, 2012)

Try this edited version.

It locates the first match on the path given, rather than than the last match.

The CSV file is appended with subsequent searches.

You can specify an exact match for a folder too.


```
@echo off
set "project=%~1"
if not defined project (
   echo Please supply details of a share or drive letter and project
   echo.
   echo Use this format: "%~nx0" "j:\1234567" or  "%~nx0" "\\server\share\name\1234567"
   echo.
   pause
   goto :EOF
)
if exist "%project%\" (
   echo Found %~1 & set "projfolder=%~1" & set "proj=%~nx1" & goto :exitfor
   goto :exitfor
)
for /f "delims=" %%a in ('dir "%project%" /b /s /ad 2^>nul ^|sort /r ') do echo Found %%a & set "projfolder=%%a" & set "proj=%%~nxa" & goto :exitfor
:exitfor
if not defined proj echo Project "%project%" can't be located & pause & goto :EOF
set "csv=projects.csv"
>>"%csv%" set /p ="%projfolder%," <nul
setlocal enabledelayedexpansion
echo Checking for folders
for /f "delims=" %%a in ('dir "%projfolder%" /b /s /ad ') do (
set "folder=%%a"
set "folder=!folder:%projfolder%\=!"
echo subfolder "!folder!"
>>"%csv%" set /p ="!folder!," <nul
)
echo.>>"%csv%"
echo Done.
endlocal
```


----------



## JTS123 (Dec 4, 2012)

Beautiful! That works perfectly. Thank you very much for your time and effort, I really appreciate it.

What do you mean by an exact match for a folder? How does that work, and what does it do?


----------



## foxidrive (Oct 20, 2012)

If you know the folder location you can specify it directly, rather than have the batch file search for it. 
So you could use the line below to process that particular 1234567 folder. Earlier revisions wouldn't process that command line correctly.

call test.bat "c:\folder a\location b\1234567"

I'm glad it's useful for you.


----------



## JTS123 (Dec 4, 2012)

Another small problem has come up with running this. If I point the batch file to a location which doesn't exist, it just stalls.

My list of projects is about 2300 lines. I can link these together in either a massive batch file, or a series of batch files. But putting in the proper path for each one is difficult, as some of them are from a main working drive (J:\), while some others are from another (T:\), or even a server / archive server. I have 9 areas to scour for folders. Could it be possible to add a clause to go to the next if a path is invalid?


----------



## foxidrive (Oct 20, 2012)

Line 11 is added and it should exit if the path isn't found.


```
@echo off
set "project=%~1"
if not defined project (
   echo Please supply details of a share or drive letter and project
   echo.
   echo Use this format: "%~nx0" "j:\1234567" or  "%~nx0" "\\server\share\name\1234567"
   echo.
   pause
   goto :EOF
)
if not exist "%~dp1" goto :EOF
if exist "%project%\" (
   echo Found %~1 & set "projfolder=%~1" & set "proj=%~nx1" & goto :exitfor
   goto :exitfor
)
for /f "delims=" %%a in ('dir "%project%" /b /s /ad 2^>nul ^|sort /r ') do echo Found %%a & set "projfolder=%%a" & set "proj=%%~nxa" & goto :exitfor
:exitfor
if not defined proj echo Project "%project%" can't be located & pause & goto :EOF
set "csv=projects.csv"
>>"%csv%" set /p ="%projfolder%," <nul
setlocal enabledelayedexpansion
echo Checking for folders
for /f "delims=" %%a in ('dir "%projfolder%" /b /s /ad ') do (
set "folder=%%a"
set "folder=!folder:%projfolder%\=!"
echo subfolder "!folder!"
>>"%csv%" set /p ="!folder!," <nul
)
echo.>>"%csv%"
echo Done.
endlocal
```


----------



## JTS123 (Dec 4, 2012)

Hm, it still seems to be stalling. I don't think it knows that the path doesn't exist - there's no message for it, the screen stays black.


----------



## foxidrive (Oct 20, 2012)

Here is the result I get. a.bat is the batch file name so you see the command as a
and in drive d:\abc there is no folder named 123
Then below that I do not have an F: drive and the batch file returned to the cmd prompt.

d:\abc>a "D:\abc\123"
Project "D:\abc\123" can't be located
Press any key to continue . . .

d:\abc>a "f:\abc\123"

d:\abc>

Paste part of your calling batch file in a reply - and the name of the calling batch file and the name of the batch file created from the last post.


----------



## JTS123 (Dec 4, 2012)

Name of batch file: FindFolders.bat
Name of calling file: Run.bat

Run.bat:


> call FindFolders.bat "J:\1301035"
> call FindFolders.bat "J:\1300855"
> call FindFolders.bat "J:\1301406"


Folder 1301406 doesn't exist.

I've tried using Run.bat, and I've also tried going through cmd to run the main batch file. Both just stay blank at this directory.


----------



## JTS123 (Dec 4, 2012)

It works now.

I ran this on a folder on my desktop and had the same prompt as you mentioned earlier. The issue I was having was that this is significantly delayed when running this on a network drive. I didn't think this was the case, as the .CSV writes fairly rapidly, depending on the number of folders it finds.

Thanks again!


----------



## foxidrive (Oct 20, 2012)

I'm glad you sorted it out.

When a folder doesn't exist then it has to search the entire drive before it knows there is no match.

Cheers
foxi


----------

