Set parameters by filename in working directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chuck4100
Posts: 4
Joined: 03 Jun 2013 04:33

Set parameters by filename in working directory

#1 Post by chuck4100 » 08 Apr 2016 21:35

Is there a way to set a parameter to the full name of a file when giving a portion of the name in a batch script?

Like, if I have Stuff.bat and file1-v2.5.exe in a folder,

Is there a way to have it find a file with "file1" in the name then set "var=file1-v2.5"
and if I rename it to 'file1-version2.5' next time I run the batch it would set "var=file1-version2.5"

Thanks for any help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Set parameters by filename in working directory

#2 Post by foxidrive » 09 Apr 2016 08:15

I find your question a little unclear.

You can find a file with a piece of the filename by using wildcards, as long as no other file contains the same piece.

It can be made more targeted in locating the file, but see here: viewtopic.php?f=3&t=6108

chuck4100
Posts: 4
Joined: 03 Jun 2013 04:33

Re: Set parameters by filename in working directory

#3 Post by chuck4100 » 09 Apr 2016 13:08

I want to set variables based of of files that are in the same working directory as the batch.

There are parts of the file's names that are constant but some parts may change and I want to know if there is a way to have to batch file check, using the constant portion, for the file then grab the name and set a variable as the file's name.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Set parameters by filename in working directory

#4 Post by foxidrive » 10 Apr 2016 10:45

chuck4100 wrote:I want to set variables based of of files that are in the same working directory as the batch.

There are parts of the file's names that are constant but some parts may change and I want to know if there is a way to have to batch file check, using the constant portion, for the file then grab the name and set a variable as the file's name.


Someone may figure out what you want to do, but your details are lacking details and it's a waste of our time to try and give you help - because this sort of thing with poor information has happened many times in the past - and the volunteers end up posting dozens of posts with code and questions, and.... it wastes their time.

chuck4100
Posts: 4
Joined: 03 Jun 2013 04:33

Re: Set parameters by filename in working directory

#5 Post by chuck4100 » 10 Apr 2016 16:27

Oh well I suppose. I honestly have no idea to be more specific. I am speaking about some extremely basic things here and speaking at a kindergarten level is the only way to make it more simple, I just don't know how to make it happen in a batch.
It's a waste of time to help and that's why people still need help or don't know things.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Set parameters by filename in working directory

#6 Post by Aacini » 10 Apr 2016 16:50

If these files are in the folder:

Code: Select all

Stuff.bat
file1-v2.5.exe

... then the line below:

Code: Select all

for %%a in (file1*.*) do set "var=%%~Na"

... assign "file1-v2.5" to "var".

Is this what you want?

Antonio

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Set parameters by filename in working directory

#7 Post by penpen » 10 Apr 2016 17:53

chuck4100 wrote:Oh well I suppose. I honestly have no idea to be more specific. I am speaking about some extremely basic things here and speaking at a kindergarten level is the only way to make it more simple, I just don't know how to make it happen in a batch.
It's a waste of time to help and that's why people still need help or don't know things.
It has nothing to do with "speaking at a kindergarten level".

Your request just is much to vague, so foxidrive has written"I find your question a little unclear.";
in that post he sketched a part of a solution (to demonstrate his will to help you), and
also pointed out a problematic part (to demonstrate that details of your task are missing).

So in short he wanted you to provide more information;
according to the linked sticky, it should be something like that:
1) What is the format of the files you want to process:
Is it "<name><no>-v(ersion)?<verison>(.<subversion>(.<build>)?)?(.exe)?" (in terms of regular expressions)?
Are these also possible: "file1.v2", "file3-v2.4.txt", "a file10-v-2.5", ...
(This is unclear because in your opening post you are using the filenames "file1-v2.5.exe" and "'file1-version2.5" {without the ".exe"}, so they are probably "fake names".)

2) Is the "<name>" restricted to alphanumeric characters ({'_', 'a' ... 'z', 'A' ... 'Z', '0' ... '9'}) only?
Is it allowed to contain any other subset of unicode?
Any special characters (for example ampersand, percentage sign, space character, ...)?

3) What should the batch do if there are multiple matches on the search phrase 'file1' for example on
"file1-v2.5.exe", "file10-v3.5.exe", "file11-v1.1.exe", "file12-1-v1.1.exe", "file12-2-v1.1.exe", ... .
Should it raise an error?
Should it set the environment variable to the first/last/... match?
Should it set n environment variable (and what's their naming convention?)

4) Are you using the word "parameter" (in your OP) for an environment variable, or do you really want to set a parameter in a batch file;
in this case which kind of parameter do you want to set:
- a "command line argument" of the called batch file (%0 ... %9 ...)
- a "command line argument" of a new batch file context ("call :label") (also: %0 ... %9 ...)
- a for variable (%%0, %%a, %%?, ...)
- really a parameter (and what's its syntax):

Code: Select all

yourBatch.bat /parameter1=defaultvalue /parameter2 /parameter3="var" /"parameter4=variable"


(...)


penpen

chuck4100
Posts: 4
Joined: 03 Jun 2013 04:33

Re: Set parameters by filename in working directory

#8 Post by chuck4100 » 11 Apr 2016 00:47

penpen, I see what you are saying. Honestly, none of those points even occurred to me, that's how little I know of what I am asking.
Aacini wrote:for %%a in (file1*.*) do set "var=%%~Na"

But... Thank you Aacini, that is exactly (as far as I know) what I am looking for. It seems to work perfect.

I will take this thread to heart for the future but i was already pulling my hair out thinking of how I could reword what I was asking.

Thank you all though for the insight and Aacini for understanding my noob speak.

Post Reply