Recursively checking if *.ext exists in subdirectories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Recursively checking if *.ext exists in subdirectories

#1 Post by ALbino » 23 Oct 2014 19:38

Hey all,

I'm VERY new to this whole thing, so bear with me if this is obvious.

Essentially what I'm trying to do is process files in a directory, but I was running into problems where sometimes the files would be in a subdirectory. Here's my original code that works in one directory:

Code: Select all

if exist *.txt (
   for /F "delims=" %%a in ('dir /b *.txt') do set FoundFile="%%~fa"
   set FileType=txt
   goto foundfile
   ) else (
      echo.
      echo    No TXT Files Found.
      echo.
      goto :nextfiletype
)


That only searches the one directory, but I am able to search all the subdirectories as well by adding /s:

Code: Select all

for /F "delims=" %%a in ('dir /b /s *.txt') do set FoundFile="%%~fa"


However, the problem is I am not able to check to see if a *.txt file exists first before trying to find it because "if exist *.txt" fails since the file is in a subdirectory to begin with.

Since after processing they get moved to a finished directory anyway, my thinking was I could just move the *.txt file to the main directory, and then my "if exist *.txt" would work, so I tried testing with this:


Code: Select all

set MoveFolder="c:\test\"

for /F "delims=" %%a in ('dir /b /s *.txt') do set MoveFile="%%~fa"

if NOT %MoveFile%=="" (
   move %MoveFile% %MoveFolder%
   goto foundfile
   ) else (
   goto nofiles
)

:foundfile
echo Move File: %MoveFile%
echo Move Folder: %MoveFolder%
echo Files Moved.
pause
goto :eof

:nofiles
echo No Files Found.
pause


In this case I'm trying to see if there are no txt files by checking to see if %MoveFile% is null, but that doesn't work either.

Ideally I would like to just be able to do "if exist" recursively through all the subdirectories, but if it's easier just to move them to their root and do "if exist" after then I'm happy to do that as well, but I can't figure that out either :)

If anybody has any suggestions I would appreciate it. Thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Recursively checking if *.ext exists in subdirectories

#2 Post by foxidrive » 23 Oct 2014 23:48

The 2^>nul will remove any error message, if that's your problem.

/a-d
will avoid a possible error when a folder matches the filespec - it just makes it more robust.


Code: Select all

for /F "delims=" %%a in ('dir /b /s /a-d *.txt 2^>nul') do set FoundFile="%%~fa"

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Recursively checking if *.ext exists in subdirectories

#3 Post by ALbino » 24 Oct 2014 00:33

I really only care about 1 file in this script, so in a very sloppy way I have now solved the moving problem by just recursively copying every single file to the root, where I can then process that single file and then delete everything else using:

Code: Select all

for /F "delims=" %%a in ('dir /b /s *.*') do move /Y "%%a" %MoveToFolder%


That's a pretty ugly way to do it though. Ideally I'd prefer to do something more elegant like:

Code: Select all

if exist *.txt /s (
   ...


Wherein the "exist" question is asked recursively. Does that make sense?

ALbino
Posts: 29
Joined: 23 Oct 2014 19:27

Re: Recursively checking if *.ext exists in subdirectories

#4 Post by ALbino » 24 Oct 2014 00:46

foxidrive wrote:The 2^>nul will remove any error message, if that's your problem.

/a-d
will avoid a possible error when a folder matches the filespec - it just makes it more robust.


Code: Select all

for /F "delims=" %%a in ('dir /b /s /a-d *.txt 2^>nul') do set FoundFile="%%~fa"


I'm still learning this stuff, but I've seen a bunch of scripts use /a-d and wasn't sure exactly what it meant. I just looked through the documentation on it, and that does make a lot of sense to use that. I'm going to add it from here on out. Thanks!

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Recursively checking if *.ext exists in subdirectories

#5 Post by Compo » 24 Oct 2014 06:17

Since you appear to intend to process other files further down the line I have typed a .cmd example to hopefully help you.

Code: Select all

@Echo Off
SetLocal

Rem User defined destination directory
(Set _UDdir=C:\Test)

Rem User defined list of file types to process
(Set _UDtypes=cfg ini log txt)

Rem Loop through each file type for processing
For %%a In (%_UDtypes%) Do Call :ProcessIt %%a
Pause
GoTo :EOF

   :ProcessIt

   Rem Variable used for counting
   Set "_num=0"

   Rem Loop through main process
   For /r %%a In (*.%1) Do (
      Set/a "_num+=1" && (
         If Not Exist "%_UDdir%\" MD "%_UDdir%"
         Echo(
         Echo(Processing %%~fa
         Copy "%%~fa" "%_UDdir%"
      )
   )
   Echo(
   Echo(%_num% %1 files were found and processed
   Echo(

Notes
The above will recurse with the Current Directory as its base. The current directory is not necessarily the directory in which your script lies.
Additionally, because it may prove problematic to put moved files back in the event of unwanted results during this test phase, I have taken the liberty of replacing Move with Copy Try it, take a look at the results and output and return with any further questions. If you are happy with the results and wish no further input, replace Copy with Move on line 26.

Post Reply