Reputation: 11
How can I use batch to find all subfolders in a directory that contains 1 and only 1 file with the file name which contains the word log and an excel file extension. Either xls or xlsx.
I tried to modify this batch script which finds empty folders and I think it's a good starting point but my batch skills are non-existent. I figure I need to add if statement to check if more than one file exist and if it does then ignore that direct. If only 1 file exist then I need to check to see if it contains Log in the name and has the extension xls or xlsx. I thing something like Log.xls* might work for the string comparison.
@echo off
setlocal
set "myPath=%CD%"
for /r "%myPath%" /d %%F in (.) do dir /b "%%F" | findstr "^" >nul || echo %%~fF
For perspective here is why I want to do this. I have a co-worker whose job is to create all documentation. I noticed that he like to pull wool over our eyes by creating empty directories and directories with only a log file inside. There are hundreds of folder which should have assembly drawings and BOMs inside. It waste my time hunting for things that don't exist and so I want to compile a list so we can clean it up and deal with the problem. I know if there are more than one file in the directory there is a good chance the second file is the data I would actual want and the first file is the revision log.
EDIT to Show Example Folder Structures
I'm going to use BOM (bill of materials) as the example
BOM
└ BOM-1000
└ [EMPTY]
└ BOM-1001
└ Log.xls or Log.xlsx
└ BOM-1002
└ Log.xls
└ GoodData.doc or some other extension
└ BOM-1003
└ SubfolderWithGoodData
│ └ GoodData.doc or some other extension
└ Log.xls
The first two example directories are what I want flagged. The last 2 contain a log and valid data and therefore shouldn't be listed in the output. There could possibly be one more scenario where there is a subfolder that should contain good data but instead it's just empty.
BOM
└ BOM-1004
└ SubfolderWhichShouldContainGoodData
│ └ [EMPTY]
└ Log.xls
Upvotes: 1
Views: 394
Reputation: 38604
As you did not clarify exactly what you required, I've had to take my lead from the other answer. This should therefore find every directory rooted from the location in %sd%
, (I've used %CD%
to match your example, you can change that on line 2
), containing just one file, (but any number of directories). If true, it will output a message for each of those which contains either a .xls
, or a .xlsx
file, telling you the full path of the directory and the name of the file.
@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "sd=%CD%"
Set "fs=%__AppDir__%findstr.exe"
Set "ec=^" & For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "cr=%%G" & (Set lf=^
% 0x0A %
)
Set "}=" & For /F "Tokens=1,* Delims=:" %%G In ('%__AppDir__%cmd.exe /D/V/C
"Dir "%sd%" /A:-D/D/S | "%fs%" /RC:"!ec! " | "%fs%" /RC:"!cr!!lf! *1 ""'
) Do If Not "%%H" == "" (Set "}=%%G" & SetLocal EnableDelayedExpansion
For /F "Delims=" %%I In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
"!}:~-1!:%%H":"*.xls" "!}:~-1!:%%H":"*.xlsx" 2^> NUL'
) Do Echo !}:~-1!:%%H contains just one Excel file [%%~nxI]
EndLocal)
Pause
The last line is optional, if you intend to run the file from cmd.exe
, as opposed to from the GUI.
Upvotes: 1
Reputation: 80023
@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\*"' ) DO (
SET "nofiles=Y"
SET "onefileonly=Y"
FOR /f "delims=:" %%c IN ('dir /b /a-d "%%b\*" 2^>nul ^|findstr /n /R "." ' ) DO SET "nofiles="&IF %%c gtr 1 SET "onefileonly="
IF DEFINED nofiles (ECHO No files IN %%b) ELSE (
IF DEFINED onefileonly IF EXIST "%%b\*.xlsx" (ECHO Only one *.xlsx file IN Directory "%%b") ELSE IF EXIST "%%b\*.xls" (
ECHO Only one *.xls file IN Directory "%%b") ELSE (ECHO Only one file IN Directory "%%b")
)
)
GOTO :EOF
Assign each directoryname in turn to %%b
. Determine whether there are 0, 1, or more files in the directory by conducting a directory list and enumerating the resultant list. If there's exactly 1 file, determine its extension. Note sequence is important here, as a .xlsx
file will likely have a shortname .xls
.
Upvotes: 1