path with exclamation mark...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

path with exclamation mark...

#1 Post by catalinnc » 27 Jan 2015 22:19

i made this batch witch works OK only if this path "%cd%\target" has no exclamation mark(s) in it.

"setlocal enabledelayedexpansion" removes the exclamation mark(s) from the path and "split_tool.exe" errors out because it can not find the file.

how do i improve it to make it work with a path with exclamation mark(s) in it?

Code: Select all

@echo off & cls

setlocal disabledelayedexpansion

for /r "%cd%\target" %%G in (*.extension) do (

setlocal enabledelayedexpansion

set /a "_offset=(%%~zG)-(0x30)"

split_tool.exe !_offset! end "%%G" "%%G.sig"

endlocal

)

endlocal

pause

exit /b

_

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

Re: path with exclamation mark...

#2 Post by Squashman » 27 Jan 2015 22:36


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

Re: path with exclamation mark...

#3 Post by jeb » 28 Jan 2015 02:54

Squashman wrote:viewtopic.php?f=3&t=3980

I can't see the relation to this problem?

@catalinnc

As you remarked, it's a problem with the setlocal EnableDelayedExpansion.

Simply don't use it before you get the content from %%G.
You need a temporarxy variable to store the content of %%G.
Expanding with exclamation marks itself is always safe, but not FOR-Loop variables like %%G.

Code: Select all

@echo off 
cls
setlocal disabledelayedexpansion
for /r "%cd%\target" %%G in (*.extension) do (
  set "dir=%%~G"
  set /a "_offset=(%%~zG)-(0x30)"
  setlocal enabledelayedexpansion
  split_tool.exe !_offset! end "!dir!" "!dir!.sig"
  endlocal
)

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

Re: path with exclamation mark...

#4 Post by Squashman » 28 Jan 2015 07:19

jeb wrote:
Squashman wrote:viewtopic.php?f=3&t=3980

I can't see the relation to this problem?

Well I see it as a problem with the environmental variable %CD% not expanding correctly. If that is not what the issue was then I guess I don't understand why Dave spent all that time writing that jscript and batch file to safely expand any variable.

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

Re: path with exclamation mark...

#5 Post by jeb » 29 Jan 2015 01:34

Squashman wrote: If that is not what the issue was then I guess I don't understand why Dave spent all that time writing that jscript and batch file to safely expand any variable.

Indeed, Dave tried to expand any variable in a safe way.

But the problem in his cases is not the content of the variables it's the variable name.

He tried to expand variables like

Code: Select all

set "var%%!^&=hello word"

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

Re: path with exclamation mark...

#6 Post by Squashman » 29 Jan 2015 08:24

jeb wrote:Indeed, Dave tried to expand any variable in a safe way.

But the problem in his cases is not the content of the variables it's the variable name.

He tried to expand variables like

Code: Select all

set "var%%!^&=hello word"

Ahh. I see that now that I read the whole thread. Should probably change the Thread Title to Safely expand any variable name.

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: path with exclamation mark...

#7 Post by catalinnc » 29 Jan 2015 16:09

jeb wrote:@catalinnc

As you remarked, it's a problem with the setlocal EnableDelayedExpansion.

Simply don't use it before you get the content from %%G.
You need a temporarxy variable to store the content of %%G.
Expanding with exclamation marks itself is always safe, but not FOR-Loop variables like %%G.

Code: Select all

@echo off 
cls
setlocal disabledelayedexpansion
for /r "%cd%\target" %%G in (*.extension) do (
  set "dir=%%~G"
  set /a "_offset=(%%~zG)-(0x30)"
  setlocal enabledelayedexpansion
  split_tool.exe !_offset! end "!dir!" "!dir!.sig"
  endlocal
)


@jeb thank you very much for this simple and elegant solution
_

Post Reply