Kaijyuu
Kaijyuu

Reputation: 21

Batch file to open one folder with a partial folder name

I need a batch to open directly a folder because the address is too big.

For example I have this folder stuct:

Z:\Folder_1\1234_name1\Folder_2\Folder_3\Folder_4\Folder_5
Z:\Folder_1\3456_name2\Folder_2\Folder_3\Folder_4\Folder_5
Z:\Folder_1\7891_name3\Folder_2\Folder_3\Folder_4\Folder_5
Z:\Folder_1\1596_name4\Folder_2\Folder_3\Folder_4\Folder_5
...

So I need a batch that I can enter the number, for example 7891 and it opens the folder that match that number Z:\Folder_1\7891_name3\Folder_2\Folder_3\Folder_4\Folder_5.

The problem is that I just know the number but never the name in front of it. The rest, Folder_1, Folder_2, Folder_3, Folder_4 and Folder_5 is always the same name.

I was trying something like:

cls
@ECHO OFF
:CALLNUMBER

echo Number?
set/p "Number=>"


%SystemRoot%\explorer.exe "Z:\Folder_1\%Number%*\Folder_2\Folder_3\Folder_4\Folder_5"

This is not working because it does not accept %Number%* so it opens my documents folder.

Upvotes: 2

Views: 745

Answers (1)

aschipfl
aschipfl

Reputation: 34909

You can use wild-cards (?, *) only in the very last element of a path; use for as a work-around:

@echo off
echo Number?
set /P Number=">"

for /D %%D in ("Z:\Folder_1\%Number%_*") do (
    "%SystemRoot%\explorer.exe" "%%~D\Folder_2\Folder_3\Folder_4\Folder_5"
)

Upvotes: 2

Related Questions