%~x0 is Null if Batch is executed with quotes.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

%~x0 is Null if Batch is executed with quotes.

#1 Post by Meerkat » 23 Jan 2016 10:16

Dealing with Bath Hybrids, I see that if Batch file is executed in CMD with quotes, %x~0 is blank. This is a serious thing if Batch file names have spaces, because we use quotes on it.

Example: Sample session...
CMD.EXE wrote:C:\>type NoSpace.bat
@echo off
echo(%~f0

C:\>NoSpace
C:\NoSpace.bat

C:\>ren NoSpace.bat "With Space.bat"

C:\>"With Space"
C:\With Space <<<No Extensions.

C:\>

Assuming that we will not edit the file name, how will get the REAL full path of the Batch file with quotes? Thanks!

Meerkat

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: %~x0 is Null if Batch is executed with quotes.

#2 Post by ShadowThief » 23 Jan 2016 11:52

The extension appears if you include the extension when you call the script.

Image

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

Re: %~x0 is Null if Batch is executed with quotes.

#3 Post by penpen » 23 Jan 2016 12:02

This may help you (tested on Win 8.1, 32 bit):

Code: Select all

@echo off
call :showName
goto :eof

:showName
echo(%~f0
goto :eof


penpen

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

Re: %~x0 is Null if Batch is executed with quotes.

#4 Post by foxidrive » 23 Jan 2016 19:42

Or use this:

Code: Select all

@echo off
for %%a in ("%~f0") do echo "%%~xa" - "%%~a"
pause

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

Re: %~x0 is Null if Batch is executed with quotes.

#5 Post by dbenham » 23 Jan 2016 19:54

foxidrive wrote:Or use this:

Code: Select all

@echo off
for %%a in ("%~f0") do echo "%%~xa" - "%%~a"
pause

That is of no help, as I expected. The extension is still missing if the called script is launched with quotes around the name.

I believe the only solution is to CALL into a subroutine as penpen has shown.


Dave Benham

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

Re: %~x0 is Null if Batch is executed with quotes.

#6 Post by foxidrive » 24 Jan 2016 00:10

I initially had this: and removed the .* after a brief test - my test was flawed.

This does work - but it relies on not have extra files with the same name.

Code: Select all

@echo off
for %%a in ("%~f0.*") do echo "%%~xa" - "%%~a"
pause

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: %~x0 is Null if Batch is executed with quotes.

#7 Post by mcnd » 25 Jan 2016 03:15

Maybe this (http://stackoverflow.com/a/26851883/2861476) could help. As far as I know, the penpen's answer is the only reliable way to ensure you retrieve a valid reference to the current batch file.
Last edited by mcnd on 25 Jan 2016 13:46, edited 1 time in total.

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: %~x0 is Null if Batch is executed with quotes.

#8 Post by Meerkat » 25 Jan 2016 05:10

Thanks everyone! :D

Meerkat

Post Reply