Getting extension of a file and forcing to uppercase
Moderator: DosItHelp
Getting extension of a file and forcing to uppercase
Hi all,
Am trying to extract an extension from a file path-name. Found this function on another site that will successfully pull it from a passed in parameter e.g. if user drops the file C:\Temp\TestFile.txt onto my batch file and I run this code:
SET Ext1=%~x1
I successfully get ".txt" stuffed into my Ext1 var.
But I cannot do this in 2 steps (which I want/need to do) e.g.
SET MyFile=%1
SET Ext1=%~xMyFile%
So then the notes seemed to indicate that the "~" function only works with single character variable names. So I tried this:
SET a=%1
SET Ext1=%~x1
Still no work. How do I get this to work or is there another built in DOS function that will extract the "extension" from a string in a var?
Second question: is there a function that would force this string to uppercase (or lowercase) so I can do a case insensitive check on the extension (or is there a way to do a string insensitive comparison between 2 strings). Here is how I was comparing the strings (on what I got working):
SET Ext1=%~1
IF Ext1==.txt
Thanks.
Am trying to extract an extension from a file path-name. Found this function on another site that will successfully pull it from a passed in parameter e.g. if user drops the file C:\Temp\TestFile.txt onto my batch file and I run this code:
SET Ext1=%~x1
I successfully get ".txt" stuffed into my Ext1 var.
But I cannot do this in 2 steps (which I want/need to do) e.g.
SET MyFile=%1
SET Ext1=%~xMyFile%
So then the notes seemed to indicate that the "~" function only works with single character variable names. So I tried this:
SET a=%1
SET Ext1=%~x1
Still no work. How do I get this to work or is there another built in DOS function that will extract the "extension" from a string in a var?
Second question: is there a function that would force this string to uppercase (or lowercase) so I can do a case insensitive check on the extension (or is there a way to do a string insensitive comparison between 2 strings). Here is how I was comparing the strings (on what I got working):
SET Ext1=%~1
IF Ext1==.txt
Thanks.
Re: Getting extension of a file and forcing to uppercase
Use the "N" option to set a variable with the file name only.
set filename=%~n1
set extension=%~x1
If you look in our DosTips Library you will see an upper case function.
http://www.dostips.com/DtCodeCmdLib.php ... on.toUpper
set filename=%~n1
set extension=%~x1
If you look in our DosTips Library you will see an upper case function.
http://www.dostips.com/DtCodeCmdLib.php ... on.toUpper
Re: Getting extension of a file and forcing to uppercase
FOR variables and CALL parameters share the same expansion modifiers.
If your string is in an environment variable, then you need to transfer it to either a CALL parameter, or else a FOR variable. Using FOR is faster than using CALL.
The IF command has the /I option to do a case insensitive comparison.
Dave Benham
If your string is in an environment variable, then you need to transfer it to either a CALL parameter, or else a FOR variable. Using FOR is faster than using CALL.
The IF command has the /I option to do a case insensitive comparison.
Code: Select all
for %%F in ("%MyFile%") do if /i "%%~xF"==".txt" echo %%F is a .TXT file.
Dave Benham
-
- Posts: 76
- Joined: 21 Dec 2011 14:21
Re: Getting extension of a file and forcing to uppercase
AlbertG wrote:Hi all,
Am trying to extract an extension from a file path-name. Found this function on another site that will successfully pull it from a passed in parameter e.g. if user drops the file C:\Temp\TestFile.txt onto my batch file and I run this code:
SET Ext1=%~x1
I successfully get ".txt" stuffed into my Ext1 var.
But I cannot do this in 2 steps (which I want/need to do) e.g.
SET MyFile=%1
SET Ext1=%~xMyFile%
So then the notes seemed to indicate that the "~" function only works with single character variable names. So I tried this:
SET a=%1
SET Ext1=%~x1
Still no work. How do I get this to work or is there another built in DOS function that will extract the "extension" from a string in a var?
Second question: is there a function that would force this string to uppercase (or lowercase) so I can do a case insensitive check on the extension (or is there a way to do a string insensitive comparison between 2 strings). Here is how I was comparing the strings (on what I got working):
SET Ext1=%~1
IF Ext1==.txt
Thanks.
to answer number two, do this: IF /I var==var
for answer number one, do this:
@ECHO OFF
FOR /F "SKIP=4 TOKENS=1-4* DELIMS= " %%A IN ('DIR %1') DO (
IF "%%E"=="" GOTO :EOF
ECHO EXTENSION IS %%~XE
)
WELL DAVE BEAT ME TO IT..
Re: Getting extension of a file and forcing to uppercase
Okay...not "new" to batch programming but never got into the "deeper" stuff. So, would you mind "unpacking" some of the code below (or if you know of a good link to same, that would be great). Here are the questions I have:
1) what's the difference between a "regular" variable (what I assume is an environmental variable) and a "FOR" or "CALL" variable? I just assumed that vars created with SET X= were all the same. And somehow that parameters passed were just assigned to %1, %2 etc. Maybe this will help me understand more complex code going ahead.
2) what is an "expansion modifier"?
3) Can you unpack the FOR statement below for me? In my case, the user can pass 1 or 2 files to a batch file to be copied somewhere else. Could you change the code below to do that?
4) And if someone wanted me to help me do a quantum jump in my programming ability (in dos), here is what I am trying to do:
- user selects 1 or 2 files and drops them on a batch file (and have to account for them passing none by mistake)
- batch file has to check if the file is missing it's proper extension (previous IT guy had a routine where they could store data without the extension - not nice! - as each file has to go into the proper spot). So I have to check if missing an extension and if so, tack on ".df".
- copies the batch file to a specified directory on their machine (deep, deep inside C:\Program Files, hence the reason for using a batch file to help them put the files where they should go).
I have done a "crude" loop using GOTO statements but I am sure there is a better way to loop through both parameters (optionally) and process each file. Thanks.
Thanks,
Learning in Ontario....
1) what's the difference between a "regular" variable (what I assume is an environmental variable) and a "FOR" or "CALL" variable? I just assumed that vars created with SET X= were all the same. And somehow that parameters passed were just assigned to %1, %2 etc. Maybe this will help me understand more complex code going ahead.
2) what is an "expansion modifier"?
3) Can you unpack the FOR statement below for me? In my case, the user can pass 1 or 2 files to a batch file to be copied somewhere else. Could you change the code below to do that?
4) And if someone wanted me to help me do a quantum jump in my programming ability (in dos), here is what I am trying to do:
- user selects 1 or 2 files and drops them on a batch file (and have to account for them passing none by mistake)
- batch file has to check if the file is missing it's proper extension (previous IT guy had a routine where they could store data without the extension - not nice! - as each file has to go into the proper spot). So I have to check if missing an extension and if so, tack on ".df".
- copies the batch file to a specified directory on their machine (deep, deep inside C:\Program Files, hence the reason for using a batch file to help them put the files where they should go).
I have done a "crude" loop using GOTO statements but I am sure there is a better way to loop through both parameters (optionally) and process each file. Thanks.
Thanks,
Learning in Ontario....
dbenham wrote:FOR variables and CALL parameters share the same expansion modifiers.
If your string is in an environment variable, then you need to transfer it to either a CALL parameter, or else a FOR variable. Using FOR is faster than using CALL.
The IF command has the /I option to do a case insensitive comparison.Code: Select all
for %%F in ("%MyFile%") do if /i "%%~xF"==".txt" echo %%F is a .TXT file.
Dave Benham
Re: Getting extension of a file and forcing to uppercase
AlbertG wrote:4) And if someone wanted me to help me do a quantum jump in my programming ability (in dos), here is what I am trying to do:
- user selects 1 or 2 files and drops them on a batch file (and have to account for them passing none by mistake)
- batch file has to check if the file is missing it's proper extension (previous IT guy had a routine where they could store data without the extension - not nice! - as each file has to go into the proper spot). So I have to check if missing an extension and if so, tack on ".df".
- copies the batch file to a specified directory on their machine (deep, deep inside C:\Program Files, hence the reason for using a batch file to help them put the files where they should go).
This should solve that. (untested:
One issue that could crop up is if there is no extension but the file has periods in it, then the batch file will assume that the part after the period is an extension.
IE: if the file is called "My.description.of.Amsterdam" then it will assume the extension is ".Amsterdam"
Code: Select all
@echo off
if "%~1"=="" (
echo no files were dropped on the "%~nx0" batch file
pause
goto :EOF
)
for %%a in (%*) do (
if "%%~xa"=="" (
ren "%%a" "%%~na.df"
copy /b "%%~dpna.df" "c:\program files\..." >nul
) else (
copy /b "%%a" "c:\program files\..." >nul
)
)
Re: Getting extension of a file and forcing to uppercase
Great!
Ok...can I ask some questions to confirm what I am seeing:
1) using opening and closing brackets "()" around code: looks like I can do multiple lines after an IF statement (fairly obvious but wanted to confirm and are there any caveats?)
2) "%0" seems to be a reference back to the running batch file?
3) GOTO :EOF --> I am familiar with GOTO that goes to a label...is :EOF some sort of built in label (I noticed it was not in your code). And are there other built in labels?
4) Code of "for %%a in (%a)"
--> what does the double percent trigger? Is this like forcing an expansion of what is in %a?
--> I take it that "(%*)" is the way your reference "all parameters"?
That's it for now. Thanks for the help.
Albert
Ok...can I ask some questions to confirm what I am seeing:
1) using opening and closing brackets "()" around code: looks like I can do multiple lines after an IF statement (fairly obvious but wanted to confirm and are there any caveats?)
2) "%0" seems to be a reference back to the running batch file?
3) GOTO :EOF --> I am familiar with GOTO that goes to a label...is :EOF some sort of built in label (I noticed it was not in your code). And are there other built in labels?
4) Code of "for %%a in (%a)"
--> what does the double percent trigger? Is this like forcing an expansion of what is in %a?
--> I take it that "(%*)" is the way your reference "all parameters"?
That's it for now. Thanks for the help.
Albert
Re: Getting extension of a file and forcing to uppercase
AlbertG wrote:1) using opening and closing brackets "()" around code: looks like I can do multiple lines after an IF statement (fairly obvious but wanted to confirm and are there any caveats?)
Yes, you can use multiple statements. A common issue people find is when setting and using environment variables within ( ) - and you will need to use delayed expansion and then !var! instead of %var% but this means you cannot handle ! characters without extra processing.
2) "%0" seems to be a reference back to the running batch file?
Yes. The name as entered on the cmd line.
3) GOTO :EOF --> I am familiar with GOTO that goes to a label...is :EOF some sort of built in label (I noticed it was not in your code). And are there other built in labels?
:EOF (End Of File) is an implied label and is the only one I know of.
4) Code of "for %%a in (%a)"
--> what does the double percent trigger? Is this like forcing an expansion of what is in %a?
--> I take it that "(%*)" is the way your reference "all parameters"?
When using a for in do on the command prompt you only use %a but when using it in a batch file you need to double the % so that it is %%a
%* is the entire command line after the batch file name, yes.
Re: Getting extension of a file and forcing to uppercase
Thanks for all the responses.
Instead of learning this "bit by bit", is there a good book or some detailed help files out there that explain DOS loops (and also variable expansion and/or allocation etc)?
Might speed up the process.
Thanks,
Albert
Instead of learning this "bit by bit", is there a good book or some detailed help files out there that explain DOS loops (and also variable expansion and/or allocation etc)?
Might speed up the process.
Thanks,
Albert
Re: Getting extension of a file and forcing to uppercase
A whole library of stuff to learn from on the main website page.
Re: Getting extension of a file and forcing to uppercase
Also the built in help is useful
for /?
set /?
etc
And Windows has cmd.exe help built in too.
I don't know of any books that cover the subject but I've never looked.
for /?
set /?
etc
And Windows has cmd.exe help built in too.
I don't know of any books that cover the subject but I've never looked.