Search "My TXT-FILES Folder" from Drive (A:) to Drive (Z:), then parse the direcory listing into the MENU systems

Hi all Experts,

I love to have a script that will:

1. Search "My TXT-FILES Folder" from Drive (A:) to Drive (Z:).

2. Assume it was found in Drive D or E, then stop searching.

3. Begin to parse the directory listing into the MENU systems.

4. Then, I can choose a "Folder" that I want to see what in there.

5. Another MENU appears, from there, I can choose a file to view.

6. Even I closed the file I just view, The menu need to be there,
   so I can go back and forth to open other folders or files,
   if needed.

"My TXT-FILES Folder"

  Folder 1
     My Note 01.txt
     Your note 01.txt
     
  Folder 2
     Output.txt
     Input.txt

  Folder 3
     Temp.txt
     List.txt

--------------------------------
            MAIN MENU
--------------------------------
1. Folder 1
2. Folder 2
3. Folder 3
4. Exit

Enter your selection __
--------------------------------

If choose option 1, then another MENU appears.

--------------------------------
          Folder 1
--------------------------------
1. My Note 01.txt
2. Your note 01.txt
3. Back to Main Menu

Enter your selection __
--------------------------------

If choose option 2, then call "Notepad.exe" to open "Your note 01.txt"

and so on ...
cunocAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

t0t0Commented:
hmmm.. interesting question...
0
t0t0Commented:
When searching drives for the "My TXT-FILES" folder, can it be assumed it will ALWAYS be found in the root folder of the drive?

Should the search start from drive C: or drive D:? Or, should the search start from drive A: (which is normally a floppy disk)?
0
cunocAuthor Commented:
Hi t0t0,

I know Drive A: and Drvive B are no longer existed, but we can assign an External or USB drive into that available drive letters if we use all of others, so I love to have that script to search from Drive (A:) to Drive (Z:).

The "My TXT-FILES Folder" is in (My USB Thumb Drive), of course it won't be in Drive C:\ for my protection.

Thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

t0t0Commented:
Please try this 'rough' code for evaluation.....

I accept some tweaking will be needed to work with drives A: and B:. I've omitted drive C: from the listed drive letters.

Please suggest areas which need modifying.

NOTE: Hopefully, if you copy and paste the code into your text editor, the line:

   set /p .=%%a:...<nul

will function as it should - ie, when searching drives, it should cycle through each letter while remaining on the same line. Please let me know if this fails to work properly as I can omit it from the code.





:: =================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 11/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   echo.
   set /p .=Searching drive <nul

   for %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a:...<nul
      
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\ 2^>nul') do (
         if /i "%%~nb"=="My TXT-FILES" (
            set source=%%~db
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b %source%') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo Cannot find "My TXT-FILES" folder.
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
      
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0
   for /f "tokens=*" %%a in ('dir /a-d /b "%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
            
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
      
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
::

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cunocAuthor Commented:
Hi t0t0,

I don't want it to automatically to search from Drive A: - Drive Z, without asking me .. and the script doesn't work.
////////////////////////////
Searching drive
a
The system cannot find the path specified.
b
The system cannot find the path specified.
d
The device is not ready.
e
The device is not ready.
f
g
The device is not ready.
h
The device is not ready.
i
The device is not ready.
j
k
File Not Found
l
m
n
o
/////////////////////////////////
:: =================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 11/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   echo.
   set /p .=Searching drive

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a
     
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\') do (
         if /i "%%~nb"=="My TXT-FILES" (
            set source=%%~db
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b %source%') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo Cannot find "My TXT-FILES" folder.
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0
   for /f "tokens=*" %%a in ('dir /a-d /b "%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
0
cunocAuthor Commented:
Hi t0t0,

I want it to automatically to search from Drive A: - Drive Z, without asking me .. and the script doesn't work.
0
cunocAuthor Commented:
Hi t0t0,
I modified (set /p .=Searching drive <nul) to (set p .=Searching drive) and (set /p .=%%a) to (set p .=%%a), the script does run a search but could not find any ... even I have assigned the correct  "TXT, Removable0001" for searching ...

:: =================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 11/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   echo.
   set p .=Searching drive

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set p .=%%a
     
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\') do (
         if /i "%%~nb"=="TXT, Removable0001" (
            set source=%%~db
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b %source%') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo Cannot find "My TXT-FILES" folder.
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0
   for /f "tokens=*" %%a in ('dir /a-d /b "%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
0
cunocAuthor Commented:
Hi t0t0,
I test again and the script is not be able to find the "TXT, Removable0001" that I am looking for ..
=================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 11/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   echo.
   set p .=Searching drive

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set p .=%%a
     
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\') do (
         if /i "%%~nb"=="TXT, Removable0001" (
            set source=%%~db
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b %source%') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo Cannot find "TXT, Removable0001" folder.
pause ..


The system cannot find the path specified.
The system cannot find the path specified.
The device is not ready.
The device is not ready.
The device is not ready.
The device is not ready.
The device is not ready.
File Not Found
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
Cannot find "TXT, Removable0001" folder.
Press any key to continue . . .
0
RobSampsonCommented:
Hi, quick question....

Would it suitable (as a huge shortcut), to have a script search for your specific folder on all drives, and when found, just open up Windows Explorer *at* that folder?

Then, you can just use Windows Explorer to navigate from there through the subfolders?

Rob.
0
cunocAuthor Commented:
Hi Rob Sampson,

I like the menu that t0t0 has design, the only problem is that that script fail to search the folder.

To answer to your question, if your design similar to t0t0's script, i like it. As long as it works base on script itself.

Thanks,
0
t0t0Commented:
cunoc

Hiya... I've just returned home to find all your comments which I have yet to read through.

One thing I must emphasise at this moment though is that my code is designed to search from drive C: upwards. Drive A: and B: would be treated differently due to the way the software hooks into the hardware.

I need a little time to look into this further.....



0
RobSampsonCommented:
OK, this is VBS code, but give it a shot and see what you think.

Copy the code into Notepad, and save it as something like "FindFolder.vbs", then double click it to run it.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
strFolderName = "My TXT-FILES"
strTempFile = "c:\temp\result.txt"
strFoundFolderPath = ""
If objFSO.FileExists(strTempFile) = True Then objFSO.DeleteFile strTempFile, True
For intChr = 1 To 26
	strDrive = Chr(intChr + 64) & ":"
	If objFSO.DriveExists(strDrive) = True Then
		objShell.Run "cmd /c dir /ad /s /b c:\""*" & strFolderName & "*"" > " & strTempFile, 0, True
		If objFSO.FileExists(strTempFile) = True Then
			Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
			If Not objFile.AtEndOfStream Then strFoundFolderPath = objFile.ReadLine
		End If
	End If
	If strFoundFolderPath <> "" Then Exit For
Next
If strFoundFolderPath = "" Then
	MsgBox "Folder was not found."
Else
	objShell.Run "explorer.exe /n,/e,""" & strFoundFolderPath & """", 1, False
End If

Open in new window

0
cunocAuthor Commented:
Hi RobSampson:

Your script only search in drive C:\ and the result was "Folder was not found."

Thanks

//////////////

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
strFolderName = "TXT, Removable0001"
strTempFile = "c:\temp\result.txt"
strFoundFolderPath = "" 
If objFSO.FileExists(strTempFile) = True Then objFSO.DeleteFile strTempFile, True
For intChr = 1 To 26
        strDrive = Chr(intChr + 64) & ":"
        If objFSO.DriveExists(strDrive) = True Then
                objShell.Run "cmd /c dir /ad /s /b c:\""*" & strFolderName & "*"" > " & strTempFile, 0, True
                If objFSO.FileExists(strTempFile) = True Then
                        Set objFile = objFSO.OpenTextFile(strTempFile, intForReading, False)
                        If Not objFile.AtEndOfStream Then strFoundFolderPath = objFile.ReadLine
                End If
        End If
        If strFoundFolderPath <> "" Then Exit For
Next
If strFoundFolderPath = "" Then
        MsgBox "Folder was not found."
Else
        objShell.Run "explorer.exe /n,/e,""" & strFoundFolderPath & """", 1, False
End If
0
RobSampsonCommented:
Oh whoops! Replace this line:
               objShell.Run "cmd /c dir /ad /s /b c:\""*" & strFolderName & "*"" > " & strTempFile, 0, True

with this
               objShell.Run "cmd /c dir /ad /s /b " & strDrive & "\""*" & strFolderName & "*"" > " & strTempFile, 0, True



Regards,

Rob.
0
cunocAuthor Commented:
Hi Hi RobSampson:

The script was able to find the "My TXT-FILES" then open Explorer, I was hoping that it would parse the directory listing into the MENU systems and so on.

Thanks

3. Begin to parse the directory listing into the MENU systems.

4. Then, I can choose a "Folder" that I want to see what in there.

5. Another MENU appears, from there, I can choose a file to view.

6. Even I closed the file I just view, The menu need to be there,
   so I can go back and forth to open other folders or files,
   if needed.

"My TXT-FILES Folder"

  Folder 1
     My Note 01.txt
     Your note 01.txt
     
  Folder 2
     Output.txt
     Input.txt

  Folder 3
     Temp.txt
     List.txt

--------------------------------
            MAIN MENU
--------------------------------
1. Folder 1
2. Folder 2
3. Folder 3
4. Exit

Enter your selection __
--------------------------------

If choose option 1, then another MENU appears.

--------------------------------
          Folder 1
--------------------------------
1. My Note 01.txt
2. Your note 01.txt
3. Back to Main Menu

Enter your selection __
--------------------------------

If choose option 2, then call "Notepad.exe" to open "Your note 01.txt"

and so on ...
0
t0t0Commented:
Concerning your comment: 26284921 above.


Where you state:

    >>"I don't want it to automatically to search from Drive A: - Drive Z, without asking me .."

What do you mean by this? Do you mean you want the program to ASK you for a drive letter to search for example?

Furthermore, you state:

    >>"and the script doesn't work"

The program works perfectly well in it's original form. The reason why it did not work for you is because you had made changes to it (I suspect) without full understanding of what the code does.

Please do not criticise my code if it fails to work as a result of you meddling with it.

Please review these changes you made and their effect on the original code.


1) The program starts with a:

    set /p .=Searching drive <nul

The purpose of this line is NOT to ask for user input despite using the SET /P command. The function of this line is to display (and ONLY display) part of a message - "Searching drive ", the other part ("a:..." through "z:...") is added later from inside a loop which cycles through each letter "a" to "z".

The "<nul" at the end of the line is ABSOLUTELY vital as it suppresses the cursor and therefore enabling DOS to display the next part of the message on the SAME line.

The ".=" part of the command is my way of signifying that NO user input is expected and therefore, input is assigned to a "." (period) instead of a variable.


2) The part of the code that compliments this is:

    set /p .=%%a:...<nul

Again, you changed this line without regard to it's intended operation.

This line of code uses a number of advanced features previously unseen in EE. Again, the SET /P fetches it's input from DOS rather than the user. This is signified byt the "<nul" which suppresses the cursor.

The main function of this command is to display (and ONLY to display) the contents of the variable '%%a' followed by a series of "." (periods). '%%a' is FOR-loop's varaible which cycles through "a" to "z" so first time round, it displays:

    a:...

which is appended onto the end of the previously displayed text ("Searching drive ") resulting in:

    Searching drive a:...

Furthermore, this is followed by 5 ASCII Back-Space characters (ASCII 008) which moves the cursor back 5 spaces placing it over the letter "a" ready to display the next letter in it's place. The "<nul" ensures SET /P does not move the cursor onto the next line and therefore waits for input from DOS's STDOUT.

The overall effect of these two commands is to display a message on the screen which cycles through the letters "a" to "z" without moving onto the next line so all you see is:

    Searching drive a:...

throught to:

    Searching drive z:...

This all happens on the same line.

NOTE: The above is NOT vital for the operation of the rest of the code and was ONLY included to indicate the program was searching through each drive letter.


3) The FOR-loop:

    for %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do (

cycles through the letters "d" to "z". You have added the "a"  and the "b" letters. This MAY work without windows throwing up a windows "No Disk" exception error message. If that's the case then all well and done.

I deliberately held off adding the "a" and "b" drives until I had time to test the code more fully.


4) During the drive detection part of the program, no other output should be generated therefore, iut was ABSOLUTELY necessary to add " 2^>nul" as in the case of:

    for /f "tokens=*" %%b in ('dir /ad /b %%a:\ 2^>nul') do (

that's because the code IS DELIBERATELY EXPECTING to generate errors. The "2>nul" ensures that errors are redirected to DOS's NUL device (which disappears into thin air).

By not including this vital element, you WILL see errors on appearing on the screen - this is normal and well anticipated. The "^" (ceret) delimits the ">" (greater-than sign) otherwise DOS throws a throws a wobbly when used in the context it is used in.

You MUST restore this line to it's original code!


5) Finally, the last command during the drive-detection phase:

    echo Cannot find "My TXT-FILES" folder.

begins with a series of Back-Space characters (ASCII 008) which replaces the line:

    Searching drive z:...

if the folder "My TXT-FILES" cannot be found on any drive - at which point the program exits.

It is NOT absolutely necessary to include the Back-Space characters however, you should preced the command with at least one "echo." command to drop the cursor down to the next line.
0
t0t0Commented:
Concerning your comment: 26284925


    >>"I want it to automatically to search from Drive A: - Drive Z, without asking me"

It does. See my previous comment.


    >>"and the script doesn't work"

Also explained above.


0
t0t0Commented:
Concerning your comment: 26285119

    >>"I modified (set /p .=Searching drive <nul) to (set p .=Searching drive) and (set /p .=%%a) to (set p .=%%a), the script does run a search"


For the sake of simplicity, and I can see this is a struggle for you, please REMOVE those two lines from the code as they are superflous to it's operation AND unappreciated.


    >>"but could not find any ... even I have assigned the correct  "TXT, Removable0001" for searching ..."

Hmmm... I'm a little confused here. Are you actually trying to search for a volume label and NOT a folder-name?


The line:

    if /i "%%~nb"=="TXT, Removable0001" (

is comparing the contents of the FOR-variable %%b (the 'name' part) which is the name of each folder which is found, to the literal text ""TXT, Removable0001". This means, you are looking for a single folder named EXACTLY as:

    TXT, Removable0001

inclding the comman and the space! Is this what you actually intend?

Please be MORE SPECIFIC regarding the ACTUAL folder-name you are searching for!

0
cunocAuthor Commented:
Hi t0t0,
Sorry, I mess up your script when I test it, but now I run the original you sent over me and here is the output.

Here what I have changed ..
 1.  if /i "%%~nb"=="TXT, Removable0001" (

2. echo Cannot find "My TXT-FILES" folder.
   PAUSE ..
   exit /b

3. Cannot find "My TXT-FILES" folder.
    Press any key to continue . . .

/////////////////////////////////

@echo off
   setlocal enabledelayedexpansion

   echo.
   set /p .=Searching drive <nul

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a:...<nul
     
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\ 2^>nul') do (
         if /i "%%~nb"=="TXT, Removable0001" (
            set source=%%~db
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b %source%') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo Cannot find "My TXT-FILES" folder.
PAUSE ..
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0
   for /f "tokens=*" %%a in ('dir /a-d /b "%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
::
0
cunocAuthor Commented:
Hi t0t0,
I did try a few diffrent folders that I have in the drives and it not be able to find one.

 1. if /i "%%~nb"=="TXT0001" (
 2. if /i "%%~nb"=="TXT0002" (
 3. if /i "%%~nb"=="TXT, Removable0001" (
0
t0t0Commented:
Concerning your comment: 26285205

    >>"I test again and the script is not be able to find the "TXT, Removable0001" that I am looking for .."


See previous comment.

Furthermore, you should NOT be getting any of these messages (as shown in your comment):

    The system cannot find the path specified.
    The system cannot find the path specified.
    The device is not ready.
    The device is not ready.
    The device is not ready.
    The device is not ready.
    The device is not ready.
    File Not Found
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    The system cannot find the path specified.
    Cannot find "TXT, Removable0001" folder.


Therefore, it is vital to restore:

    for /f "tokens=*" %%b in ('dir /ad /b %%a:\') do (

As explained above, these messages are EXPECTED and redirecting STDOUT to NUL is the NORMAL way to deal with this!
0
cunocAuthor Commented:
Hi t0t0,
I took out this line:  for /f "tokens=*" %%b in ('dir /ad /b %%a:\ 2^>nul') do (
Replaced with this: for /f "tokens=*" %%b in ('dir /ad /b %%a:\') do (
/////////
Searching drive The system cannot find the path specified.
The system cannot find the path specified.
The device is not ready.
The device is not ready.
The device is not ready.
The device is not ready.
The device is not ready.
File Not Found
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
Cannot find "My TXT-FILES" folder.
Press any key to continue . . .
0
t0t0Commented:
Concerning your comments: 26288431, 26291059 and 26291150

If you intend to go down the VB-route then I will withdraw further assistance.


I have tested my code with a foldername "TXT, Removable0001" and it works perfectly well.


What is the ACTUAL name of the folder you are searching for? Is it "TXT, Removable0001" (not including the double-quotes of course)?


0
cunocAuthor Commented:
Hi t0t0,
I tried both..
1.  if /i "%%~nb"=="TXT, Removable0001" (
2.  if /i "%%~nb"==TXT, Removable0001 (

To answer to your question, My statement in the Ticket 26288431 was that I love your script, it great, the only problem was that it could not find the folder i am looking for.

And the reason I modified some part of your scrip was that I want to figure out where it stop and how far it goes to .... I did not mean your script doesn't work. Please accept my explanation...

Thanks,
0
cunocAuthor Commented:
Hi t0t0,
It works and only works when I placed that script inside the directory of TXT, Removable0001

--------------------------------
            MAIN MENU
--------------------------------
1. Shell Scripts 0001
2. TXT, Account0001
3. TXT, Adapter0001
4. TXT, Cisco 0001
5. TXT, Computer 0001
6. TXT, DVD0001
7. TXT, Linux n Unix 0001
8. TXT, Mix 0001
9. TXT, Movie AVI0001
10. TXT, Quiz0001
11. TXT_Text_File_0001
12. ViceVersa PRO, Backup0001
13. Exit

Enter your selection:
0
t0t0Commented:
Concerning your comments: 26291705

You posted as I was in the porcess of replying to your previous comments.


    >>"I did try a few diffrent folders that I have in the drives and it not be able to find one."

    >>"1. if /i "%%~nb"=="TXT0001" ("
    etc...


Okay, I'm looking at it now.....

0
t0t0Commented:
I can see where the problem is..... I'm just making some adjustments to the code....
0
t0t0Commented:
Please try this revised code. Note: the folder name My TXT-FILE appears in more than one place should you need to change this globally!



:: =================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 11/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   echo.
   set /p .=Searching drive <nul

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a:...<nul
     
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\ 2^>nul') do (
         if /i "%%~nb"=="My TXT-FILES" (
            set source=%%a:
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b /on "!source!\My TXT-FILES\"') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo.
   echo.
   echo Cannot find "My TXT-FILES" folder.
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0

   for /f "tokens=*" %%a in ('dir /a-d /b "%source%\My TXT-FILES\%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%source%\My TXT-FILES\%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
::
0
cunocAuthor Commented:
Hi Paul Tomasi,

1. I changed (exit /b) in your script to PAUSE .. for testing purpose ..

2. It searchs all the way down to Z, then Message shown that "Can not find ..."

/////////////////

Searching drive z:...

Cannot find "TXT, Removable0001" folder.
Press any key to continue . . .
=================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 11/01/1010
:: =================================================================================

@echo off
   setlocal enabledelayedexpansion

   echo.
   set /p .=Searching drive <nul

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a:...<nul
     
      for /f "tokens=*" %%b in ('dir /ad /b %%a:\ 2^>nul') do (
         if /i "%%~nb"=="TXT, Removable0001" (
            set source=%%a:
            set folder=0

            for /f "tokens=*" %%a in ('dir /ad /b /on "!source!\TXT, Removable0001\"') do (
               set /a folder+=1
               set folder[!folder!]=%%a
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo.
   echo.
   echo Cannot find "TXT, Removable0001" folder.
PAUSE ..

::exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0

   for /f "tokens=*" %%a in ('dir /a-d /b "%source%\TXT, Removable0001\%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%source%\TXT, Removable0001\%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
0
cunocAuthor Commented:
Hi Paul Tomasi,

The folder "TXT, Removable0001" is located at N:\USB, BACKUP, 0001\DOC0001\TXT, Removable0001.

I did not use that folder is because I don't want to mess up with that and that was why I used this folder "My TXT-FILES" as for testing.

And I have tried to search a few folders that are residing in that drive, and the script  failed to locate those ..

Thanks
0
t0t0Commented:
Hmm... okay. what's your operating system that you are running this on?

Is "USB, BACKUP, 0001" a single foldername which is located on drive N:

If so, I can see what the problem is..... I did ask very early on whether the folder (My TXT-FILES at the time) was located off the root of the drive or nested off another folder. You never provided confirmation to this so I assumed, along with your examples, it was located off the root. It appears you want the code to search deep into all and any folders for "TXT, Removable0001". That being the case, the code needs to be modified as it only works at root level.

Please bear with me while I make the necessary adjustments.... unfortunately, it's time for me to collect my daughter from school and do all the domestic stuff I have to do before I can get back to this code. Rest assured I will return to this a little later.



0
cunocAuthor Commented:
Hi Paul Tomasi,

It works only when I placed the folder in

M:\TXT, Removable0001    (<-- It found it ..)

N:\TXT, Removable0001    (<-- It found it ..)

It would fail if I put it in anywhere like this:

M:\Foler\TXT, Removable0001     (<-- It can not be found)

N:\Directory\TXT, Removable0001 (<-- It can not be found)

Z:\TXT, Removable0001           (<-- It found it ..)
0
cunocAuthor Commented:
Hi t0t0,

I am totally sorry for not providing all the information that you needed.

My "TXT, Removable0001" is nested off another folder and another folder and I want the code to search deep into all and any folders for "TXT, Removable0001" that it located.

I have three PCs. (Windows XP, Vista and 7) and never want to put my Data in Drive C:,

External Drives only and those are movable from one PC to another.

I keep testing until I wondered myself that why the searching process runs very fast from Drive A through Drive Z. And I decide to place the "TXT, Removable0001" right at the root of the Drive N, it it found, (Description of Ticket: 26293558).
0
t0t0Commented:
Please review the following program:

NOTE: The text folder is currently set to 'TXT, Removable0001' as defined by the variable %text%.




:: =================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 12/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   set text=TXT, Removable0001

   echo.
   set /p .=Searching drive <nul

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a:...<nul
     
      for /f "tokens=*" %%b in ('dir /ad /b /s %%a:\ 2^>nul') do (
         if /i "%%~nb"=="%text%" (
            set source=%%b
            set folder=0

            for /f "tokens=*" %%c in ('dir /ad /b /on "!source!\"') do (
               set /a folder+=1
               set folder[!folder!]=%%c
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu
         )
      )
   )

   echo.
   echo.
   echo Cannot find "%text%" folder.
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   echo --------------------------------
   echo             MAIN MENU
   echo --------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo Please select from 1..!folder!
         echo.
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            cls
            echo Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0
   for /f "tokens=*" %%a in ('dir /a-d /b "%source%\%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      echo --------------------------------
      echo           %~1
      echo --------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo Please select from 1..!file!
            echo.
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%source%\%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
::
0
t0t0Commented:
The only improvements I can think of at the moment is:

1) When a sub-menu is displayed, the foldername which appears between the dotted lines is not strictly centred. This feature can be added. EASY

2) If any of the foldernames in any of the sub-menus is longer than the length of the dotted lines then then dotted lines can be made to extend to at least that same length globally so as to add 'balance' to the menu. EASYISH

3) the menu could be positioned on the screen. Horizontal positioning would depend on the length of the longest sub-menu entry. Vertical positioning would depend on the number of menu entries. COMPLEX

Here's an example of horizontal and vertical positioning. Bear in mind this is NOT dynamic though however, that could be added as the next phase of development.



:: =================================================================================
:: TXTMenu
::
:: Written by Paul Tomasi - 12/01/1010
:: =================================================================================
   @echo off
   setlocal enabledelayedexpansion

   rem The following line contains 24 spaces
   set hpos=                        
   set vpos=5
   set text=TXT, Removable0001

   echo.
   set /p .=Searching drive <nul

   for %%a in (a b d e f g h i j k l m n o p q r s t u v w x y z) do (
      set /p .=%%a:...<nul
     
      for /f "tokens=*" %%b in ('dir /ad /b /s %%a:\ 2^>nul') do (
         if /i "%%~nb"=="%text%" (
            set source=%%b
            set folder=0

            for /f "tokens=*" %%c in ('dir /ad /b /on "!source!\"') do (
               set /a folder+=1
               set folder[!folder!]=%%c
            )

            set /a folder+=1
            set folder[!folder!]=Exit
            goto :menu %hpos%
         )
      )
   )

   echo.
   echo.
   echo Cannot find "%text%" folder.
exit /b



:: ---------------------------------------------------------------------------------
:: MENU
:: ---------------------------------------------------------------------------------
:menu
   cls
   for /l %%a in (1,1,%vpos%) do echo.

   echo %hpos%--------------------------------
   echo %hpos%            MAIN MENU
   echo %hpos%--------------------------------

   for /l %%a in (1,1,%folder%) do (
      echo %hpos%%%a. !folder[%%a]!
   )
     
   echo.
   set /p selected_folder=%hpos%Enter your selection:

   for %%a in (%selected_folder%) do (
      if "!folder[%%a]!"=="" (
         echo.
         echo %hpos%Please select from 1..!folder!
         echo.
         set /p .=%hpos%<nul
         pause
      ) else (
         if /i "!folder[%%a]!"=="Exit" (
            echo.
            echo %hpos%Goodbye.
            exit /b
         ) else (
            call :list_files "!folder[%%a]!"
         )
      )
   )
goto :menu



:: ---------------------------------------------------------------------------------
:: LIST FILES
:: ---------------------------------------------------------------------------------
:list_files
   set file=0
   for /f "tokens=*" %%a in ('dir /a-d /b "%source%\%~1\*.txt"') do (
      set /a file+=1
      set file[!file!]=%%~nxa
   )

   set /a file+=1
   set file[%file%]=Back to Main Menu

   :loop
      cls
      for /l %%a in (1,1,%vpos%) do echo.

      echo %hpos%--------------------------------
      echo %hpos%          %~1
      echo %hpos%--------------------------------
           
      for /l %%a in (1,1,%file%) do (
         echo %hpos%%%a. !file[%%a]!
      )
     
      echo.
      set /p selected_file=%hpos%Enter your selection:

      for %%a in (%selected_file%) do (
         if "!file[%%a]!"=="" (
            echo.
            echo %hpos%Please select from 1..!file!
            echo.
            set /p .=%hpos%<nul
            pause
         ) else (
            if /i "!file[%%a]!"=="Back to Main Menu" (
               exit /b
            ) else (
               notepad "%source%\%~1\!file[%%a]!"
            )
         )
      )
   goto :loop
::
0
cunocAuthor Commented:
Hi all Experts,
Thanks all the works and the time you guys have spent on this.

Special thanks to t0t0,
Your script works exactly as the way I want, and again I am so sorry for not providing you the information you need.

Thanks all...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.