Getting extension of a file and forcing to uppercase

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
AlbertG
Posts: 4
Joined: 14 Nov 2012 10:45

Getting extension of a file and forcing to uppercase

#1 Post by AlbertG » 14 Nov 2012 11:01

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting extension of a file and forcing to uppercase

#2 Post by Squashman » 14 Nov 2012 11:27

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Getting extension of a file and forcing to uppercase

#3 Post by dbenham » 14 Nov 2012 11:31

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

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: Getting extension of a file and forcing to uppercase

#4 Post by timbertuck » 14 Nov 2012 11:34

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..

AlbertG
Posts: 4
Joined: 14 Nov 2012 10:45

Re: Getting extension of a file and forcing to uppercase

#5 Post by AlbertG » 14 Nov 2012 12:23

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....

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

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

Re: Getting extension of a file and forcing to uppercase

#6 Post by foxidrive » 14 Nov 2012 22:49

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
  )
)

AlbertG
Posts: 4
Joined: 14 Nov 2012 10:45

Re: Getting extension of a file and forcing to uppercase

#7 Post by AlbertG » 15 Nov 2012 08:35

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

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

Re: Getting extension of a file and forcing to uppercase

#8 Post by foxidrive » 15 Nov 2012 09:39

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.

AlbertG
Posts: 4
Joined: 14 Nov 2012 10:45

Re: Getting extension of a file and forcing to uppercase

#9 Post by AlbertG » 22 Nov 2012 16:01

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Getting extension of a file and forcing to uppercase

#10 Post by Squashman » 22 Nov 2012 16:08

A whole library of stuff to learn from on the main website page.

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

Re: Getting extension of a file and forcing to uppercase

#11 Post by foxidrive » 22 Nov 2012 16:11

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.

Post Reply