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!