tags:

views:

655

answers:

2

I have a directory structure like this:

- Root
  - Versioned deployment folder
    - config file

The "Versioned deployment folder" name varies with each version of the application. The config file name stays the same.

I'd like to write a batch file that opens a program on the config file. The batch file should stay the same regardless of the name of the intermediate folder. The config file will be the only file with its name under the Root.

How do I get the path to the config file consistently from within the batch file?

+1  A: 

It seems like there's some missing bit of information here... The batch file simply must have some idea of what the versioned folder name is. Is there a rule? Do you always want the latest versioned folder? If so, how do you name the deployment versions?

Update: So if you only have one sub-folder (the versioned folder) and you want a fixed batch file to always execute some call on a fixed file name in that sub-folder, you can do this:

@for /f "tokens=* delims= " %%a in ('@dir /ad /b') do @set FOLDER=%%a
echo %FOLDER%
call notepad %FOLDER%\test.config

Obviously change test.config to be the fixed name of the folder.

Does that meet your needs...?

Andrew Flanagan
In this scenario, note that I said "The config file will be the only file with its name under the Root" - a recursive search for the file name is what I'm looking for.
JoshL
I didn't understand that -- your question makes more sense now. Is the config file's name unique for each version? Or do you need to open the file to check some value?
Andrew Flanagan
Well, in this case there is only one versioned sub-folder. After the batch file modifies the config file, the versioned folder will be copied to a location that contains multiple folders with different version numbers in their names.
JoshL
Perfect - thank-you! I added a "/ON" to the dir statement in order to handle a situation requiring multiple sub-folders, in order to ensure that I got the last one in sorted order.
JoshL
A: 

You could use dir /s /b configfile to get the file path. Store this in a variable and you can strip out the file name using %variable:~0,-x%. x is the file name length.

Hope this helps.

Vasu Balakrishnan
You can use a subroutine and get the full file name with %~nx1.
Joey