You are not logged in.

#1 07 Jul 2016 15:48

wquatan
Member
Registered: 19 Jun 2012
Posts: 16

Variable Replace within For Loop

I have a problem with using the "Replace" within a For-loop.
How can I achive that OriginalPathFileName=PathFileName with abc%~1 (ex abc4,abc15,...) removed from it

@echo off
setlocal enableDelayedExpansion

FOR /L %%I IN (1,1,99) DO call :Process-Number %%I

:Process-Number
for /R "E:\Files" %%A in ("*abc%~1.doc") do (
	set PathFileName=%%A
	set PathFileNameNumber=abc%~1
	set OriginalPathFileName=!!PathFileName:%PathFileNameNumber%=!!
)

Last edited by wquatan (08 Jul 2016 09:53)

Offline

#2 07 Jul 2016 18:22

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Re: Variable Replace within For Loop

Look through this thread for some ideas and post your working code here.

Last edited by sambul35 (07 Jul 2016 18:23)

Offline

#3 08 Jul 2016 10:45

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Variable Replace within For Loop

You have double "!!" -- they don't make sense syntactically.

You're setting PathFileNameNumber inside the "for /R" loop and then trying to use it within the same loop using "%" notation (and you can't nest ! notation) -- this won't work.

Try this:

:Process-Number
set "PathFileNameNumber=abc%~1"
for /R "E:\Files" %%A in ("*%PathFileNameNumber%.doc") do (
	set "PathFileName=%%A"
	set "OriginalPathFileName=!PathFileName:%PathFileNameNumber%=!"
)

Last edited by bluesxman (08 Jul 2016 14:06)


cmd | *sh | ruby | chef

Offline

#4 08 Jul 2016 14:25

sambul35
Member
Registered: 29 Feb 2016
Posts: 18

Re: Variable Replace within For Loop

wquatan wrote:

How can I achive that OriginalPathFileName=PathFileName with abc%~1 (ex abc4,abc15,...) removed from it

Something seems wrong in your problem newly edited definition or code. Did you mean that %~1 is the 1st and only argument of your batch? Why then it has a series of values like abc4,abc15,...? Can you post that code printout, when the batch is called with all arguments and executed?

Last edited by sambul35 (08 Jul 2016 14:26)

Offline

#5 08 Jul 2016 23:29

wquatan
Member
Registered: 19 Jun 2012
Posts: 16

Re: Variable Replace within For Loop

@bluesxman
Yeah, the !! was the last thing I tried.
It's solved know, and as you suggested, it was the [set PathFileNameNumber=abc%~1] which needed to be before the FOR.
The  [set OriginalPathFileName=!PathFileName:%PathFileNameNumber%=!], one of those I tried, works now.
Thanks !!!

@sambul35
The (ex abc4,abc15,...) was from the beginning in my post and was intented as a list of examples for the %1 parameter
The call is also in the code I posted
Thanks

The working code :

@echo off
setlocal enableDelayedExpansion

FOR /L %%I IN (1,1,99) DO call :Process-Number %%I

:Process-Number
set PathFileNameNumber=abc%~1
for /R "E:\Files" %%A in ("*%PathFileNameNumber%.doc") do (
	set PathFileName=%%A
	set OriginalPathFileName=!PathFileName:%PathFileNameNumber%=!
)

Last edited by wquatan (08 Jul 2016 23:35)

Offline

Board footer

Powered by