views:

140

answers:

3

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
+1  A: 

Remove the -r -n -c -m from the call to dir.

Or pipe stderr into nothingness with 2>nul to suppress the error message.

Joey
+1  A: 

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
foxx1337
Thanks, your edit didn't completely work how I wanted, but because of some of the edits you made, I found my problem :)
Tougamer
A: 

I'm new to batch programming

A couple points by way of constructive criticism:

  1. you can take the @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
  2. it is better practice to put a :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.
akf
You don't need an "end" label. You can just use `goto :eof`. `exit /b` is also a viable option for breaking out of batch files.
Joey
@Johannes, yes and thanks for the comment. On the `/b`, there is still room for error if it is forgotten. Good point on `:eof`.
akf