# Solved: Use of Wildcards



## computerman29642 (Dec 4, 2007)

I am trying to create a batch file that will open an excel file.

Here is what I have so far....


```
start excel.exe "C:\weekending *.xls"
```
I am trying to use a wildcard that will find/determine the remaining filename. The rest of the filename is a date, and will change on a weekly basis so I cannot hard code the filename.

Does anyone know how to use wildcards in a batch file?

Any help with this will be greatly appreciated.


----------



## TheOutcaste (Aug 8, 2007)

Seems Excel won't accept wildcards for filenames. You can open multiple files, but have to give the full name.
So, a For statement will do the trick:
(note that those are not single quotes around the dir command, they are Accent Graves)

```
@echo off
set filepath=c:\test1\
For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%weekending*.xls"`) do start /I excel.exe "%%I"
```
Be sure to change the path in the set statement as needed. You can also just specify it in the For command.

Note that this will open EVERY spreadsheet in the folder that starts with *weekending

*If these spreadsheets are stored in this same folder and you just want to open the one for the previous week, it would be easiest to create a macro in an Excel spreadsheet that gets the weekending date for the previous week and then opens that file. The batch program would then just open the spreadsheet with the autorun macro.

The folks in the Business Applications Forum can probably whip up a macro or VB script to do that for you.

HTH

Jerry


----------



## madd74 (Nov 9, 2003)

another option would be to use:

start excel.exe "C:\weekending %1.xls"

then you would type after the BATCH file what you wanted run, for example, if you wanted to run the file test.xls you would do

batchfile test

and if batchfile is the .bat it would run, but of course, that requires you knowing and running the actual name of the file


----------



## computerman29642 (Dec 4, 2007)

TheOutcaste,

I tried your method. However I get an error message that shows the Excel filename (which is correct), but teh error message states that the file cannot be found. I have the Excel found located on the C: drive for testing.


----------



## TheOutcaste (Aug 8, 2007)

If the weekending*.xls file is in the root of the C drive, meaning the full path is *C:\Weekending05-17.xls* for example, make sure you changed the filepath line in the batch file:

change:
set filepath=c:\test1\
to
set filepath=c:\

If you still get an error, can you paste the error here? Right click in the command prompt window, choose Select All, then press enter.
CTRL+V in the reply window will paste the contents of the command prompt window.

Jerry


----------



## computerman29642 (Dec 4, 2007)

I have attached a screenshot of the error message that comes up when the batch file is ran.


----------



## TheOutcaste (Aug 8, 2007)

Opps, my booboo, forgot to put the filepath variable into the start statement.
Try this (change is in red)


```
@echo off
set filepath=c:\test1\
For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%weekending*.xls"`) do start /I excel.exe "[COLOR=Red]%filepath%[/COLOR]%%I"
```
I usually put my test scripts in one folder, and the sample files in another to catch these senior moments, but I managed to have the batch file and the sample xls files in the same folder and missed it this time.
Jerry


----------



## computerman29642 (Dec 4, 2007)

Jerry,

That worked perfectly. Thank you very much.


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!:up:

Please mark solved if your issue is resolved using the *Mark Solved* button at the top left of this thread.

Jerry


----------



## computerman29642 (Dec 4, 2007)

How would I do the "FilePath" if the Excel file is found on the server?

For Example: \\server\folder1\folder2\my folder\folder 3

I tried placing the path in the "FilePath", but when I run the batch file the command prompt flashes on the screen and nothing happens.


----------



## TheOutcaste (Aug 8, 2007)

if the file is \\server\folder1\folder2\my folder\folder 3\weekending 05-24-2008.xls
use
set filepath=\\server\folder1\folder2\my folder\folder 3\
The trailing slash is needed.

Or, change the For statement to include the slash:
For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%\weekending*.xls"`) do start /I excel.exe "%filepath%\%%I"

You might be able to do both, but it may not work on all versions of Windows/DOS

Jerry


----------



## computerman29642 (Dec 4, 2007)

That did the job. Thank you.


----------

