escaping the % of filenames ???

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

escaping the % of filenames ???

#1 Post by colargol » 18 Nov 2011 11:24

Hi all
A very last question before the next one

I'm trying to undestand how we should manipulate filename given in parameters by third part application.
My batchs still have issues with some specials chars.

1. First, I think every time we have to assign a var to a filename, we should have Delayed Expansion disabled, like this:

Code: Select all

setlocal DisableDelayedExpansion
set "_file1=%~1"
set _file1&     rem "%_file1%" will work with any characters, in any command line

setlocal EnableDelayedExpansion
set _file1&     rem "!_file1!" will also work with any characters, in any command line
By working like this, we'll never have to escape chars in the main batch???
Am I right or wrong???

2. The % issue
As an example, let's take a file named a%b.bat:

Code: Select all

set "_file=%~nx0"
set _file
>"new_file.bat" echo call "%_file%"
In this case, new_file.bat won't work because the % of a%b.bat has to be escaped...

but how??? I didn't found the solution :?

Thanks for help

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: escaping the % of filenames ???

#2 Post by colargol » 18 Nov 2011 15:02

Ok, I can escape % of a %~1 filename like this:

Code: Select all

set "filename=%~1"
setlocal EnableDelayedExpansion
set "filename=!filename:%%=%%%%!"


but it seems it is not possible to escape % of a filename that has been declared like this for example:
set "filename=a%b.bat"
This must be logical for everyone, but not for me :lol:

ps: it's hard to explain in english :oops:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: escaping the % of filenames ???

#3 Post by Ed Dyreen » 18 Nov 2011 15:19

'
colargol wrote:set "filename=a%b.bat"
that's because it isn't really there.

Code: Select all

@echo off &setlocal enabledelayedexpansion
echo on
set "filename=a%%b.bat"
set "filename=!filename:%%=%%%%!"

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: escaping the % of filenames ???

#4 Post by colargol » 18 Nov 2011 16:55

Okay!

In fact, a single % in a string is (in most cases) the same as nothing. It took me 2 months to understand this :roll:

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: escaping the % of filenames ???

#5 Post by jeb » 19 Nov 2011 16:07

colargol wrote:In fact, a single % in a string is (in most cases) the same as nothing. It took me 2 months to understand this :roll:


Sorry, but you didn't understand why it is removed :)

A % is the beginning for a variable expansion, this is also true in set "filename=a%b.bat", but the problem is here that the parser finds only one %, so he decides this must be a wrong variable expansion format and the % is removed.
You can see this with a filename like

Code: Select all

set "filename=a file %with two %.bat"
set filename

This results in "a file .bat" as the variable with the name "with two " is normaly not defined.

But now it you got another result.

Code: Select all

set "with two =XYZ"
set "filename=a file %with two %.bat"
set filename

This results in "a file XYZ.bat".

Ok but how to escape a % the right way?
Simply with another %

Code: Select all

set "filename=a file %%with two %%.bat"
set filename


Your replacings like

Code: Select all

set "filename=!filename:%%=%%%%!"

Are useless or even contra productive.

hope it helps
jeb

colargol
Posts: 49
Joined: 28 Sep 2011 13:23
Location: france

Re: escaping the % of filenames ???

#6 Post by colargol » 19 Nov 2011 16:49

That's very clear, thanks again jeb :wink:

Post Reply