is it possible to use Delayed Extended in a command line?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hugo
Posts: 5
Joined: 07 Apr 2013 20:25
Location: Veracruz, Mexico

is it possible to use Delayed Extended in a command line?

#1 Post by Hugo » 07 Apr 2013 20:40

This works ok in a bat file:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%i in (*.txt) do (set Name=%%~nxi&echo ren "%%i" "!Name:~0,5!-MyNewText-!Name:~5!")

(insert text in the middle of the file name)

But in a single command line:

Code: Select all

@echo off&setlocal enabledelayedexpansion&(for %i in (*.txt) do (set Name=%~nxi&echo ren "%i" "!Name:~0,5!-MyNewText-!Name:~5!"))&echo on

It does not work because of the variable Name

I wonder if there is any way to do this (just curious)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: is it possible to use Delayed Extended in a command line

#2 Post by abc0502 » 07 Apr 2013 20:46

remove the extra ( and ) from your command, that could be the reason i'm not sure though :?

@echo off&setlocal enabledelayedexpansion&(for %i in (*.txt) do (set Name=%~nxi&echo ren "%i" "!Name:~0,5!-MyNewText-!Name:~5!"))&echo on

Hugo
Posts: 5
Joined: 07 Apr 2013 20:25
Location: Veracruz, Mexico

Re: is it possible to use Delayed Extended in a command line

#3 Post by Hugo » 07 Apr 2013 21:05

Hi abc0502

No, the result is the same.
The problem is with Delayed Extended applied to the variable "Name", !Name! does not work

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: is it possible to use Delayed Extended in a command line

#4 Post by Aacini » 07 Apr 2013 21:47

There are a few commands that don't work in the command line, like CALL, GOTO and SETLOCAL. This way, it is not possible to Enable Delayed Expansion from the command line. However, if you start cmd.exe with Delayed Expansion enabled (with CMD /V:ON or via a registry value), then your command line should work.

Antonio

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: is it possible to use Delayed Extended in a command line

#5 Post by Endoro » 07 Apr 2013 23:24

Yes, this works:

Code: Select all

for %i in (*.txt) do @cmd /vo:n /c "set name=%~nxi&@echo ren "%i" "!Name:~0,5!-MyNewText-!Name:~5!""

Hugo
Posts: 5
Joined: 07 Apr 2013 20:25
Location: Veracruz, Mexico

Re: is it possible to use Delayed Extended in a command line

#6 Post by Hugo » 07 Apr 2013 23:46

It works!!!
Thanks Endoro! :)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: is it possible to use Delayed Extended in a command line

#7 Post by Endoro » 08 Apr 2013 00:16

It works, but for the records this is better:

Code: Select all

for %i in (*.txt) do @cmd /v:on /c "set "name=%~nxi"&@echo ren "%~i" "!Name:~0,5!-MyNewText-!Name:~5!""

Hugo
Posts: 5
Joined: 07 Apr 2013 20:25
Location: Veracruz, Mexico

Re: is it possible to use Delayed Extended in a command line

#8 Post by Hugo » 09 Apr 2013 20:21

Hi Endoro,
if I understand correctly, %~i is to remove the doble quotes inside %i. But I think that in

Code: Select all

for %i in (*.txt) ...

the var %i never has doble quotes. Am I wrong?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: is it possible to use Delayed Extended in a command line

#9 Post by Endoro » 09 Apr 2013 20:29

No, you are completely right. I do this by custom :)

Hugo
Posts: 5
Joined: 07 Apr 2013 20:25
Location: Veracruz, Mexico

Re: is it possible to use Delayed Extended in a command line

#10 Post by Hugo » 09 Apr 2013 20:56

Thanks :)

Post Reply