Reputation: 11
My batch file reads all of the files from a windows directory with the option to include sub directories or not include them, and then saves the list to a txt file. It woks perfectly, but for some reason the application returns "File Not Found" after you answers yes or no to the sub directories question (although the question does function properly).. I'm new to batch programming, and this has me stumped.. Here is the code:
@echo off
:start
set /P DIRECTORY=Type Directory to Search:
if not exist %DIRECTORY% goto firstlogin
:choice
set /P c=Include Sub-Directories?[Y/N]?
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
dir -r -n -c -m /s /b /o "%DIRECTORY%" > C:\Users\Zack\Desktop\list_of_program_files.txt
exit
:somewhere_else
dir -r -n -c -m /b /o "%DIRECTORY%" > C:\Users\Zack\Desktop\list_of_program_files.txt
exit
:firstlogin
echo Directory does not exist!
Pause
goto :start
Upvotes: 1
Views: 2650
Reputation: 39485
I'm new to batch programming
A couple points by way of constructive criticism:
@echo off
out (or REM
it out) and you will get a better understanding of where your code is when you hit errors, as every line will be echoed to the console:END
label in and have your flow directed there (GOTO END
) instead of using the exit
command. This will allow you to use your batch from within another batch file and not run the risk of exiting out of both when finishing this subprocess. Upvotes: 0
Reputation: 2026
I took the liberty to modify your script a little:
@echo off
:start
set /P DIRECTORY=Type Directory to Search:
if not exist %DIRECTORY% goto firstlogin
:choice
set /P c=Include Sub-Directories?[Y/N]?
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
dir "%DIRECTORY%" /A:-r /O:-n /T:c /s /b /o
goto :done
:somewhere_else
dir "%DIRECTORY%" /A:-r /O:-n /T:c /s /b /o
goto :done
:firstlogin
echo Directory does not exist!
Pause
goto :start
:done
Your user might not have the rights to access all the subdirectories, for example
c:\>dir "System Volume Information"
Volume in drive C is sys
Volume Serial Number is 7A8D-49E2
Directory of c:\System Volume Information
File Not Found
Upvotes: 1
Reputation: 354416
Remove the -r -n -c -m
from the call to dir
.
Or pipe stderr into nothingness with 2>nul
to suppress the error message.
Upvotes: 1