Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string question

2 views
Skip to first unread message

Eric B.

unread,
May 31, 2002, 12:59:32 PM5/31/02
to
Hi,
I'd like to build a routine that takes a name concatenates the date to it
and then adds a .log extension. If that file exists in the current directory
then
it would add "a" and try again and if that existed replace the "a" with "b"
etc etc
so it would construct names something like this:

set Name=Test_

Test_052202.log
Test_052202a.log
Test_052202b.log
Test_052202c.log

The final log name would be stored in some env var like LogName
I hope I've explained this well and not just toaly confused the issue
Thanks
Eric

OS: Win2k & WinXP pro

--
...
...
...This Post Has Been Tested and is Anthrax Free...
...
...


Eric B.

unread,
May 31, 2002, 2:03:29 PM5/31/02
to
Well, i found someone elses earlier post and used that to construct part of
the following
In the 2nd for loop, the 1st statement is skipped but Echo %LogName% is
executed 26
times like i wanted it to. I cant figure out why the 1st statement is
ignored.
Can some one tell me what i'm doing wrong here?
Thanks
Eric

ps: Thanks! to the poster who posted that neat for loop at the top. So nice!

@ECHO OFF
set TestName=NewTest

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Month=%%j
set Day=%%k
set Year=%%l
set Date=%%i %%j/%%k/%%l
)

ECHO Date is %Date%
ECHO Day of Week is "%DayOfWeek%", Month is "%Month%"
ECHO Day is "%Day%", Year is "%Year%"

set LogName=%TestName%_%Year%%Month%%Day%.lox
rem if NOT EXIST %LogName goto GotIt

for %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
set LogName=%TestName%_%Year%%Month%%Day%%%i.loy
Echo %LogName%
rem if NOT EXIST %LogName goto GotIt
)

:GotIt


--
...
...
...This Post Has Been Tested and is Anthrax Free...
...
...

"Eric B." <NoS...@NoSpammers.com> wrote in message
news:UZNJ8.67292$ux5....@rwcrnsc51.ops.asp.att.net...

Garry Deane

unread,
Jun 2, 2002, 10:11:06 PM6/2/02
to
On Fri, 31 May 2002 18:03:29 GMT, "Eric B." <NoS...@NoSpammers.com>
wrote:

You have encountered the current environment variable expansion
problem where %logname% is expanded to its current value BEFORE the
FOR loop is executed. In fact the 2nd line is being executed correctly
but you're just not seeing those results. When 'echo %logname%' is
executed, %logname% has already been replaced with the value it had
before the FOR command commenced. See SET /? for more information.

There are 2 ways to fix this. In W2k/XP you can enable delayed
variable expansion by adding the line 'setlocal
enabledelayedexpansion' near the start of the file and replace 'echo
%logname%' with 'echo !logname!'. That is, surround the variable with
"!" instead of "%".

The other method is to CALL echo with an extra level of % expansion.
Replace 'echo %logname% with 'call echo %%logname%%'. This will work
in NT as well as W2k/XP.

Garry

Eric B.

unread,
Jun 3, 2002, 11:55:56 PM6/3/02
to
Thanks, i had nearly figured the problem out, in fact by experimentation i
was seeing that
something was odd with the For execution. What i ended up doing was Do (Echo
TestName_%%i >>Names.txt )
followed by another ForLoop:
for /F %%i in (Names.txt) do (set LogName=%%i && if Not Exist %%i goto
GotIt)
goto :End

:GotIt

Echo Log: %LogName%


The info you posted is very helpfull, i am examining ways to
simplify what i already have by using the info you posted.
Thanks Again
Eric

--
...
...
...This Post Has Been Tested and is Anthrax Free...
...
...

"Garry Deane" <garrydeane_at_yahoo.com.au> wrote in message
news:3cfacab6...@192.168.0.2...

0 new messages