PC Review


Reply
Thread Tools Rate Thread

DELAYEDEXPANSION and exclamations

 
 
Michael Herz
Guest
Posts: n/a
 
      16th Jun 2004
Hi all,

If I'm running a "FOR /R" loop that encounters a directory with an
exclamation in it's name and DELAYEDEXPANSION is on, it fails because the
exclamation is left out of the command that FOR builds. The following code
will demonstrate this if you run it on a dir that has a subdir with an ! in
it's name:

setlocal enabledelayedexpansion
for /r "%~1" %%a in (.) do (
dir "%%a">nul
)
endlocal

You will see a "The system cannot find the file specified." pop up.

Any thoughts would be appreciated.


 
Reply With Quote
 
 
 
 
Jerold Schulman
Guest
Posts: n/a
 
      16th Jun 2004
On Wed, 16 Jun 2004 12:54:34 -0400, "Michael Herz" <(E-Mail Removed)> wrote:

>Hi all,
>
>If I'm running a "FOR /R" loop that encounters a directory with an
>exclamation in it's name and DELAYEDEXPANSION is on, it fails because the
>exclamation is left out of the command that FOR builds. The following code
>will demonstrate this if you run it on a dir that has a subdir with an ! in
>it's name:
>
>setlocal enabledelayedexpansion
>for /r "%~1" %%a in (.) do (
> dir "%%a">nul
> )
>endlocal
>
>You will see a "The system cannot find the file specified." pop up.
>
>Any thoughts would be appreciated.
>


If the folder was C:\\Exc!mark

use C:\\Exc^^!mark

Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      16th Jun 2004
"Michael Herz" wrote:

>Hi all,
>
>If I'm running a "FOR /R" loop that encounters a directory with an
>exclamation in it's name and DELAYEDEXPANSION is on, it fails because the
>exclamation is left out of the command that FOR builds. The following code
>will demonstrate this if you run it on a dir that has a subdir with an ! in
>it's name:
>
>setlocal enabledelayedexpansion
>for /r "%~1" %%a in (.) do (
>dir "%%a">nul
>)
>endlocal
>
>You will see a "The system cannot find the file specified." pop up.
>
>Any thoughts would be appreciated.
>

Beside avoiding either exclamation marks or enabledelayedexpansion
(or temporarily switching to disabledelayedexpansion) I see no cure for
your problem.

The for variable passes a value without exclamation mark when delayed
exp. is enabled - and if a folder with that name exists its the wrong
one.

setlocal enabledelayedexpansion
for /r "%~1" %%a in (.) do (dir "%%a">nul)
setlocal disabledelayedexpansion
for /r "%~1" %%a in (.) do (dir "%%a">nul)
endlocal


HTH
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
Michael Herz
Guest
Posts: n/a
 
      17th Jun 2004
Exclamations really should have been in the list of disallowed characters
for file names :-(

I thought about scanning for !s and possibly replacing with ^^!s but that's
a lot of work if it's doable at all.

Mike

"Matthias Tacke" <(E-Mail Removed)> wrote in message
news:caqfgc$1e9$01$(E-Mail Removed)...
> "Michael Herz" wrote:
>
> >Hi all,
> >
> >If I'm running a "FOR /R" loop that encounters a directory with an
> >exclamation in it's name and DELAYEDEXPANSION is on, it fails because the
> >exclamation is left out of the command that FOR builds. The following

code
> >will demonstrate this if you run it on a dir that has a subdir with an !

in
> >it's name:
> >
> >setlocal enabledelayedexpansion
> >for /r "%~1" %%a in (.) do (
> >dir "%%a">nul
> >)
> >endlocal
> >
> >You will see a "The system cannot find the file specified." pop up.
> >
> >Any thoughts would be appreciated.
> >

> Beside avoiding either exclamation marks or enabledelayedexpansion
> (or temporarily switching to disabledelayedexpansion) I see no cure for
> your problem.
>
> The for variable passes a value without exclamation mark when delayed
> exp. is enabled - and if a folder with that name exists its the wrong
> one.
>
> setlocal enabledelayedexpansion
> for /r "%~1" %%a in (.) do (dir "%%a">nul)
> setlocal disabledelayedexpansion
> for /r "%~1" %%a in (.) do (dir "%%a">nul)
> endlocal
>
>
> HTH
> --
> Greetings
> Matthias________________________________________
> For help on nt commands enter in a cmd window:
> W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm



 
Reply With Quote
 
Michael Herz
Guest
Posts: n/a
 
      17th Jun 2004

"Jerold Schulman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >

>
> If the folder was C:\\Exc!mark
>
> use C:\\Exc^^!mark
>


The problem is that "FOR /F" is going to be filling in the data based on
what it finds in your dir tree. It's not a user entry.


 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      17th Jun 2004
"Michael Herz" wrote:

>Exclamations really should have been in the list of disallowed characters
>for file names :-(
>
>I thought about scanning for !s and possibly replacing with ^^!s but that's
>a lot of work if it's doable at all.
>
>Mike
>


Not a lot of work, but I'd replace with a minus. But if programs with
registry entries rely on the exclamation mark there is a problem.

@echo off
setlocal disabledelayedexpansion
for /F "delims=" %%A in ('dir /S/B \*!*') do (
set new=%%A
call set new=%%new:!=-%%
call echo ren "%%A" %%new%%
rem call echo ren "%%A" %%new%% >>renexclam.cmd
)

But I'd edit renexclam.cmd before executing it. There are some files
(like PLUS!.hlp in w98) which should keep their names.

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
Michael Herz
Guest
Posts: n/a
 
      17th Jun 2004
Thanks Matthias but that's not what I meant. I meant that I thought about
catching the offending files while in the enabledelayedexpansion FOR loop
and somehow specially handling them there.

This limitation almost makes any cmd script that uses "FOR /F" unusable.

Mike


 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      17th Jun 2004
"Michael Herz" wrote:

>Thanks Matthias but that's not what I meant. I meant that I thought about
>catching the offending files while in the enabledelayedexpansion FOR loop
>and somehow specially handling them there.
>
>This limitation almost makes any cmd script that uses "FOR /F" unusable.
>
>Mike
>

You realized, that my batch uses some tricks to circumvent the usage of
enabledelayedexpansion? This way your batches also stay compatible with
win nt4 which doesn't know of delayedexpansion at the cost of slower
execution when using additional calls.

You may also call internal subs, so I can't fully agree with your
statement. For /f is still one of the most useful and used enhanced
commands.

--
Greetings_Matthias___________________________________________________
Have a look at: XP> <hh.exe ntcmds.chm::/ntcmds_new_tools.htm>
W2k and XP> <hh.exe ntcmds.chm::/dos_diffs.htm>
 
Reply With Quote
 
Michael Herz
Guest
Posts: n/a
 
      17th Jun 2004
The reason that I say this limitation almost makes any cmd script that uses
"FOR /F" unusable is because if you run into a directory with an ! in it's
name, the for loop will process the dir with the name minus the !. Hopefully
you have error proofed your code so you handle "file not found" errors
properly but in a bad case, processing could pass to an unintended
directory. For example, files in a "!windows" dir would process directing
the commands to the "windows" dir. Depending on what you are doing, this
could be catastrophic.


 
Reply With Quote
 
Al Dunbar [MS-MVP]
Guest
Posts: n/a
 
      19th Jun 2004

"Michael Herz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The reason that I say this limitation almost makes any cmd script that

uses
> "FOR /F" unusable is because if you run into a directory with an ! in it's
> name, the for loop will process the dir with the name minus the !.

Hopefully
> you have error proofed your code so you handle "file not found" errors
> properly but in a bad case, processing could pass to an unintended
> directory. For example, files in a "!windows" dir would process directing
> the commands to the "windows" dir. Depending on what you are doing, this
> could be catastrophic.


The factors you describe are a good reason to do either or both of the
following:

- develop a standard file and folder naming convention that avoids
problematic filename characters such as "!", "(", and ")".

- test all batch code using the most obtuse file names allowed by the above
policy, and consider using vbscript or some other less syntactically
limiting language when your find most of your coding time is dealing with
filename issues.

Given the above, I use FOR /F in a large proportion of my batch scripts, and
find that it works flawlessly.


/Al


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing questions and exclamations in Spanish? =?Utf-8?B?R2FuemE=?= Windows Vista Installation 0 13th Sep 2007 08:32 PM
POST ok, but Device Mgr shows yellow exclamations for CD drives Ritter197 Computer Hardware 4 5th Aug 2006 07:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:03 AM.