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.
Set parameters by filename in working directory
Moderator: DosItHelp
Re: Set parameters by filename in working directory
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
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
Re: Set parameters by filename in working directory
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.
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.
Re: Set parameters by filename in working directory
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.
Re: Set parameters by filename in working directory
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's a waste of time to help and that's why people still need help or don't know things.
Re: Set parameters by filename in working directory
If these files are in the folder:
... then the line below:
... assign "file1-v2.5" to "var".
Is this what you want?
Antonio
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
Re: Set parameters by filename in working directory
It has nothing to do with "speaking at a kindergarten level".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.
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
Re: Set parameters by filename in working directory
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.
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.
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.