Page 1 of 1

escaping the % of filenames ???

Posted: 18 Nov 2011 11:24
by colargol
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

Re: escaping the % of filenames ???

Posted: 18 Nov 2011 15:02
by colargol
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:

Re: escaping the % of filenames ???

Posted: 18 Nov 2011 15:19
by Ed Dyreen
'
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:%%=%%%%!"

Re: escaping the % of filenames ???

Posted: 18 Nov 2011 16:55
by colargol
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:

Re: escaping the % of filenames ???

Posted: 19 Nov 2011 16:07
by jeb
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

Re: escaping the % of filenames ???

Posted: 19 Nov 2011 16:49
by colargol
That's very clear, thanks again jeb :wink: