1.

Solve : Can I use a batch file to open a file with a certain extension??

Answer»

I have a batch file I have set up so when i right click on a folder i have the choice to run it and open a file inside that's always got the same name and extension. This one works fine.

Code: [Select]if EXIST JOB.TWJ start JOB.TWJ
exit
Now for my problem. I would like to be able to do the same thing with a file that does not have the same name in each folder but always has the extension .wbl and is the only file with that ext in that folder.

This is what i tried. Which didn't work but i don't know enough about coding to understand why.

Code: [Select]if exist "*.WBL" start "*.WBL"
exit
I don't have much experience with batch files but i did search Google and looked on any site I thought might have an answer, but I couldn't find anything relating to opening a file by checking its extension.

Thanks for helping.

TopDawgPlease do not double post; it only confuses the natives.

The if statement does not accept wildcards in this context. Which file do you want to start?...the processor doesn't know either.

It's possible to iterate all the files with a wbl extension and start each individually but that can get really messy with all those WINDOWS popping open.

By the way, what program is associated with wbl extensions?

Sorry about the double topic post. The other one was in the wrong place but its deleted now.

The .wbl extension is for a program at work to design walls.

Their will only ever be one file with the .wbl extension in a folder so multiple windows opening shouldn't be a problem.

This is the file structure I'm working with in case in helps.

--JOBS
----JOB1
------WALL
------TRUSS
----JOB2
------WALL
------TRUSS
----JOB3
ECT.


I added a right click entry through the registry so I can click on the truss folder and chose open job and it will open job.twj. That works fine. I want to be able to right click on the wall folder and open .wbl. The context menu is already set up I just don't know how the code that second batch file.

Thanks I suspect the problems lies with SELECTING a folder and expecting Windows to grab a handle to a file within the folder. I don't think that will happen.

Code: [Select]@echo off
for /f "tokens=* delims=" %%x in ('dir /b selectedfolder\*.wbl') do start "%%x"

I'm curious, how do you capture the name of the folder you select?

This is what the registry entry I use to make the right click menu is.

Code: [Select][HKEY_CLASSES_ROOT\Folder\shell\Access_Truss_Job\command]
@="\"C:\\Documents and Settings\\TrussXXXX\\My Documents\\Shortcuts\\@Open Truss Job.bat\" \"%1\""
That effectively runs the batch file in the folder that I right click on no matter where it is.

I tried the code you provided but it doesnt seem to work. I may be doing something wrong though.Still not sure how this all goes together. It seems %1 (the clicked file) gets passed as a parameter to Open Truss Job.bat. What's in Open Truss Job.bat?

I was thinking along the lines of this article (scroll down to Open the Command Prompt from the Right-Click Menu) where you could get a handle on the folder, iterate thru the contents and start each wbl file. There is only one wbl file per directory anyway. Are wbl files even executable or is there a program processing them using the file name as a parameter?

If all you need to do is pass the file name of the wbl file, setting up a file association has gotta be easier than hacking the registry. The downside of an association would be you have to drill down to the file level.

The reason im trying to do this is so I can have the folder tree open for the job im doing and right click on the truss and wall folder to open the different parts of the job without having to go all the way into each folder then back out and go to the next folder.

The open truss job.bat has this code in it.

Code: [Select]if exist JOB.TWJ start JOB.TWJ
exit
I have the registry and right click menu set up, so all I need now is to get the batch file to start the wbl file.

The .wbl files are already associated to the program that they work with, so if you were to just double click on the .wbl file it will open in the program. Also in cmd I can cd to the folder the file is in and input "start FileName.wbl" and it works so I just need to get the batch file to run all files with the .wbl extension.

Thanks for you help and sorry if that still isnt clear.The article seemed to say that the %L variable held the clicked folder name. How or why this CAME to be I cannot answer, but if true it should be easy to set up the batch code.

In the registry entry (based on your code) something like this should work:

Code: [Select]@="\"C:\\Documents and Settings\\TrussXXXX\\My Documents\\Shortcuts\\@Open WBL Job.bat\" \"%L\""

The contents of Open WBL Job.bat would be:

Code: [Select]@echo off
for /f "tokens=* delims=" %%x in ('dir /b %1\*.wbl') do start "%1\%%x"

Your first example shows you can reference the clicked folder name. The WBL example works the same way except it FINDS the contents of the clicked folder before launching anything.

Hope this helps.

Edit: The %L is a system variable that holds the name of the file that you are right-clicking on.*

* Source

Okay I changed the reg entry from %1 to %L. This is the output I get when I try to run it with the new code you provided.

W:\NEW TREE FORMAT\JOBS\772822\772822P>for /F "tokens=* delims=" %x in ('dir /b"W:\NEW TREE FORMAT\JOBS\772822\772822P"\*.wbl') do start ""W:\NEW TREE FORMAT\JOBS\772822\772822P"\%x"

W:\NEW TREE FORMAT\JOBS\772822\772822P>start ""W:\NEW TREE FORMAT\JOBS\772822\772822P"\772822-GH278-WAINWRIGHT-E-RS-FUL-CUSTOM.WBL"

W:\NEW TREE FORMAT\JOBS\772822\772822P>pause
Press any key to continue . . .

I looks like that should work but it doesn't.
Any more ideas? It looks to me like the positions of the quotes when it tries to do the start command might be wrong, but I'm the one needing help so I really have no idea.

Sorry for the trouble but I appreciate your help SidewinderI guess the quotes make sense, around a file name they can be helpful, around a folder name they're just pesky little dit marks. Cheesh!

I'm guessing it's just a quick fix. Forgetting about the quotes, is the path correct?

Code: [Select]@echo off
for /f "tokens=* delims=" %%x in ('dir /b "%~1\*.wbl"') do start "%~1\%%x"



Geez I hate quotes and you can quote me on that.
The path is correct and the quotes appear to be in the right places now.
Here is the new output.
The title of the first window is

C:\WINDOWS\system32\cmd.exe

and the contents are

W:\NEW TREE FORMAT\JOBS\772822\772822P>for /F "tokens=* delims=" %x in ('dir /b "W:\NEW TREE FORMAT\JOBS\772822\772822P\*.wbl"') do start "W:\NEW TREE FORMAT\JOBS\772822\772822P\%x"

W:\NEW TREE FORMAT\JOBS\772822\772822P>start "W:\NEW TREE FORMAT\JOBS\772822\772822P\772822-GH278-WAINWRIGHT-E-RS-FUL-CUSTOM.WBL"

W:\NEW TREE FORMAT\JOBS\772822\772822P>pause
Press any key to continue . . .

Then up pops another window with this title

W:\NEW TREE FORMAT\JOBS\772822\772822P\772822-GH278-WAINWRIGHT-E-RS-FUL-CUSTOM.WBL

and this content

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

W:\NEW TREE FORMAT\JOBS\772822\772822P>

With what little knowledge I have, I would say that should do it but it opens the file in CMD instead of the program that uses it. Well at least the syntax is correct

I'd be interested to know what occurs when you type:

Code: [Select]start "W:\NEW TREE FORMAT\JOBS\772822\772822P\772822-GH278-WAINWRIGHT-E-RS-FUL-CUSTOM.WBL"

at the command prompt.

What program is associated with wbl files? Does this program require the cmd shell to run or does it run in Windows? If it runs in the cmd shell, you may be able to run with cmd instead of start. The start command merely open another window but it's the association of the extension that determines what happens next. If it runs in Windows, you may be able to run it directly without the start and let Windows use the association to find the executable.

Oh yeah, where did this pause come from?

When I type that code in the command prompt it does the same thing as my last post with the new window thing.

The wbl files are associated with a program called Intellibuild. We use it at work to design wall panels. It runs from windows.

I added the pause so I could see what the window said before it closed.

OK going by what you said I just tried this

Code: [Select]@echo off
for /f "tokens=* delims=" %%x in ('dir /b "%~1\*.wbl"') do "%~1\%%x"
exit
And guess what... It works *almost* perfect.

I wasn't aware that you could run a program without the start command. Well, if anything this has really got me intrested in batch files and I'm going to start learning more about them.

Thanks for all your help Sidewinder.

Edit: Well there is still one problem. The cmd window stays open untill I exit the program. Is there a way to make it close after running the .wbl file?



Discussion

No Comment Found