How to open a directory (using Explorer) that contains a user defined input string
I am working on a batch file to open (with explorer) a user specified folder from a directory. The desired folder would only need to contain the input string (number) and all sub-directories in this directory follow the same naming format: "xxx xxx-%INPUT% yyyy...".
The issue I am having is there are also unique descriptions after the %INPUT% number of the directories (shown as "y" in naming format example). Is there any way to ignore these descriptions and just open the folder beginning with the string "xxx xxx-%INPUT%" or simply containing the %INPUT% (as this would also be unique)? I tried using an asterisk after xxx xxx-%INPUT% and hoped the find or find string commands could help but no such luck.
Here is my current code, thanks in advance for your assistance
@ECHO OFF C: CD\ CLS :MENU CLS ECHO ======= Input Job Number ======== SET /P INPUT=Please input job number or press 'Q' to quit... IF /I '%INPUT%'=='Q' GOTO Quit IF %INPUT% LSS 1000 set INPUT=0%INPUT% IF NOT EXIST "P:\a\b\c\%INPUT%" GOTO INVALID ELSE GOTO NOWINDIR ::ideally this would check if any folder contains the input string ::NOWINDIR %SystemRoot%\explorer.exe "P:\a\b\c\%INPUT%" ::ideally this would open the folder containing that input string ::Invalid CLS ECHO ============INVALID INPUT============ ECHO ------------------------------------- ECHO Please input valid job number or 'Q' to quit. ECHO ------------------------------------- ECHO ======PRESS ANY KEY TO CONTINUE====== PAUSE > NUL GOTO MENU ::Quit EXIT
@ECHO OFF SETLOCAL C: CD\ :MENU CLS ECHO ======= Input Job Number ======== :again SET "INPUT=Q" SET /P INPUT=Please input job number or press 'Q' to quit... IF /I "%INPUT%"=="Q" GOTO Quit set INPUT=0000%INPUT% set INPUT=%INPUT:~-4% FOR /f "delims=" %%i IN ( 'dir /b /ad "U:\a\b\c\*%INPUT%*" 2^>nul' ) DO SET "input=U:\a\b\c\%%i"&GOTO nowindir ::Invalid ECHO ============INVALID INPUT %input%=========== GOTO again :NOWINDIR %SystemRoot%\explorer.exe "%INPUT%" :Quit GOTO :EOF EXIT
This should get you started.
SET /P leaves the variable unchanged if the user simply presses enter. Normally, it would be an idea to SET the variable's value to an empty string to ensure stale data isn't unexpectedly retained, but in this case, you can initialise it to a default. The quoted arguments ensure that any stray spaces on the batch line are not included in the value assigned to the variable.
Rabbit's ears enclose strings in batch, not single-quotes. Use this on each side of the comparison to allow for strings containing spaces.
You've added an arbitrary 0 before the input string, which should work for 100..999 to convert it to a 4-character numeric string. You imply that yur target is a 4-character numeric string, but don't say so explicitly. What happens for 1..99?
Stringing 0000 before the input data, then setting the value to its own last 4 characters will ensure that the string is 4 characters long.
The for/f processes a dir listing of the target directory, produced in /b basic (names-only) form and /ad only the directories matching *%input%* (btw - the * character is an asterisk...) The "delims=" clause ensures that the entire string including spaces is applied to %%i if a match is found.
If the match is found, input is set to the fullpath and processing switched to the label :nowindir, which runs explorer and then exits. I've added a GOTO :EOF there which ends the batch but does not terminate the cmd.exe session - for testing where the batch may need to be run repeatedly. In your case, you'd be likely to need to delete that goto :eof which will then cause the batch to EXIT
Note the difference between :label and ::comment - actually, ::comment is an unreachable label (as it starts with two :) but it's often preferred to the more intrusive REM statement.
I also added the (modified) input value to the INVALID INPUT line and simplified...
Finally, note that I used my U: drive for testing - you'd need to change that back to your P: