Batch File variable set

February 12, 2010 at 06:21:54
Specs: Windows XP
I'm setting the value of the variable from the text file. {One line has three values split by categories}

e.g: - File.txt
DOMAIN\ABC,ABC CAR,abc.car@email.com
DOMAIN\XYZ,XYZ 123,xyz123@email.com

Now, my batch file has the following logic

for /f "tokens=1,2,3 delims=," %%a in (File.txt) do (
set domain=%%a
echo %%a
set user=%%b
echo %%b
set email=%%c
echo %%c
pause
)

This shows me each ',' splitted value and pauses ... which is correct

But if I use the following by displaying the variable after setting the value


for /f "tokens=1,2,3 delims=," %%a in (File.txt) do (
set domain=%%a
echo %domain%
set user=%%b
echo %user%
set email=%%c
echo %email%
pause
)
This piece of logic shows the second row value in the first iteration. Howz that ... what am I doing wrong?


See More: Batch File variable set

Report •

#1
February 12, 2010 at 06:50:41
Variables inside of loops are temporary. You have to make a call to a sub routine outside of the loop to get the effect you are looking for...

@echo off

for /f "tokens=1,2,3 delims=," %%a in (File.txt) do (
  Call :Setvar %%a %%b %%c
  echo %domain%
  echo %user%
  echo %email%
  pause
)
rem Your code here.
goto :eof

:Setvar
set domain=%1
set user=%2
set email=%3
goto :eof


Report •

#2
February 12, 2010 at 07:12:08
Thanks for quick reply :)

I've done this ... but my requirement is a bit more ... which is failing cos of the two do loops (i.e. do loop inside a do loop)

Below is the logic
As described above, we have a text file where a list of the Domain ID, user names & their respective email id's are stored (ideally 5-7 rows)

For each row we need to fire an email ... this is email has list of all the files that are checked-out by that particular users and for which we create dynamic emails and send them one by one

In my example I've two unique rows ... but it still sends two email to the first user ... Do I need to store the second do loop values again in another variable set outside the loop?

Below is the code:
set notifygroup=
for /f "tokens=1,2,3 delims=," %%a in (NotifyGroup.txt) do (
Call :SetNotifyVar %%a %%b %%c
echo domain
echo user
echo email
pause


:: @echo off & setlocal
:: set the temp file location
set tempmail=%temp%\tempmail.%random%.txt
:: echo the basic headers to the temp file
echo To: "VSS Middle Tier-USER" ^<%email%^> > %tempmail%
echo From: "Admin" ^<admin@admin.com^> >> %tempmail%
echo Subject: Checked-Out File/s Status >> %tempmail%
pause

cd\
cd "Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"
tf status /s:server /user:%hqdomain%>>"C:\Documents and Settings\NotifyCheckedOutStatus\%user%.txt"
pause

:: echo the blank line that separates the header from the body text
echo.>>%tempmail%
:: echo the body text to the temp file
echo %user%,>> %tempmail%

:: here I need to change directory cos the batch files are lying in this folder
cd\
cd "Documents and Settings\NotifyCheckedOutStatus"

setlocal enabledelayedexpansion
set SEPARATOR=/
set filecontent=
for /f "tokens=*" %%d in (%user%.txt) do (
set filecontent=%%d!%SEPARATOR%!
echo filecontent=!filecontent!
echo !filecontent!>> %tempmail%
pause
)

:: delete the file
DEL %user%.txt


:: move the temp file to the mail pickup directory
:: adjust this location for your system
move %tempmail% c:\inetpub\mailroot\pickup


set tempmail=endlocal

)

:SetNotifyVar
set domain=%1
set user=%2
set email=%3


Report •

#3
February 12, 2010 at 07:20:34
you forgot the goto :eof s

set notifygroup=
for /f "tokens=1,2,3 delims=," %%a in (NotifyGroup.txt) do (
    Call :SetNotifyVar %%a %%b %%c
    echo domain
    echo user
    echo email
    pause


    :: @echo off & setlocal
    :: set the temp file location
    set tempmail=%temp%\tempmail.%random%.txt
    :: echo the basic headers to the temp file
    echo To: "VSS Middle Tier-USER" ^<%email%^> > %tempmail%
    echo From: "Admin" ^<satishc@synechron.com^> >> %tempmail%
    echo Subject: Checked-Out File/s Status >> %tempmail%
    pause

    cd\
    cd "Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"
    tf status /s:server /user:%hqdomain%>>"C:\Documents and Settings\satish.chilukury\Desktop\NotifyCheckedOutStatus\%user%.txt"
    pause

    :: echo the blank line that separates the header from the body text
    echo.>>%tempmail%
    :: echo the body text to the temp file
    echo %user%,>> %tempmail%

    :: here I need to change directory cos the batch files are lying in this folder
    cd\
    cd "Documents and Settings\NotifyCheckedOutStatus"

    setlocal enabledelayedexpansion
    set SEPARATOR=/
    set filecontent=
    for /f "tokens=*" %%d in (%user%.txt) do (
        set filecontent=%%d!%SEPARATOR%!
        echo filecontent=!filecontent!
        echo !filecontent!>> %tempmail%
        pause
    )

    :: delete the file
    DEL %user%.txt

    :: move the temp file to the mail pickup directory
    :: adjust this location for your system
    move %tempmail% c:\inetpub\mailroot\pickup


    set tempmail=endlocal
)
goto :eof

:SetNotifyVar
    set domain=%1
    set user=%2
    set email=%3
    goto :eof


Report •

Related Solutions

#4
February 12, 2010 at 07:23:32
You have to enable the Delayed Expansion of variables by issuing at the top of batch

setlocal EnableDelayedExpansion

then mark your environment variables by the ! symbol instead of usual %, e.g

set email=%%c
echo.!email!

The temporary variables are the internal ones (%%a, %%b...) the issue is due to the behavior of environmental variables inside loops as they are expanded before the block sequence (statements embraced by ()). To avoid that effect Delayed Expansion is needed.


Report •

#5
February 12, 2010 at 07:26:38
I did that, but it is still sending two emails to the first row ... Its not considering the second row of NotifyGroup.txt

Report •

#6
February 12, 2010 at 07:57:35
I rewrote your code to make it easy to follow and debug. It is written using proper coding techniques. Use it or not but it may help you...

@echo off setlocal enabledelayedexpansion

:: Set Vars
set notifygroup=
set tempmail=%temp%\tempmail.%random%.txt
set tfdir="Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"
set SEPARATOR=/
set filecontent=
set notifydir="%userprofile%\Desktop\NotifyCheckedOutStatus" 
::Note the %userprofile% because this will work with Windows Vista and 7.


:Main
for /f "tokens=1,2,3 delims=," %%a in (NotifyGroup.txt) do (
    Call :SetNotifyVar %%a %%b %%c
    echo domain
    echo user
    echo email
    pause

    :: echo the basic headers to the temp file
    echo To: "VSS Middle Tier-USER" ^<%email%^> > %tempmail%
    echo From: "Admin" ^<satishc@synechron.com^> >> %tempmail%
    echo Subject: Checked-Out File/s Status >> %tempmail%
    pause

    "%tfdir%\tf status /s:server /user:%hqdomain%>> %notifydir%\%user%.txt"
    ::Note I removed the CD because you don't need it calling your command this way.
    pause

    :: echo the blank line that separates the header from the body text
    echo.>>%tempmail%
    :: echo the body text to the temp file
    echo %user%,>> %tempmail%

    :: here I need to change directory cos the batch files are lying in this folder
    :: cd\
    :: cd "Documents and Settings\NotifyCheckedOutStatus"
    :: Note, "cd \Documents and Settings\NotifyCheckedOutStatus" would be simpler.

    :: I am not sure why you wanted to cahnge to directory "Documents and Settings\NotifyCheckedOutStatus"
    :: This seems like a bug so I removed it. Also you do not need to do CD any more because of the changes I did.
    

    for /f "tokens=*" %%d in (%notifydir%\%user%.txt) do (
        set filecontent=%%d!%SEPARATOR%!
        echo filecontent=!filecontent!
        echo !filecontent!>> %tempmail%
        pause
    )

    :: delete the file
    del %notifydir%\%user%.txt 
    
    :: move the temp file to the mail pickup directory
    :: adjust this location for your system
    move %tempmail% c:\inetpub\mailroot\pickup


    set tempmail=endlocal
)
goto :eof

:SetNotifyVar
    set domain=%1
    set user=%2
    set email=%3
    goto :eof


Report •

#7
February 12, 2010 at 08:19:26
Yet another revision. Doing what IVO suggested you could do this...

@echo off setlocal enabledelayedexpansion

:: Set Vars
set notifygroup=
set tempmail=%temp%\tempmail.%random%.txt
set tfdir="Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"
set SEPARATOR=/
set filecontent=
set notifydir="%userprofile%\Desktop\NotifyCheckedOutStatus" 
::Note the %userprofile% because this will work with Windows Vista and 7.

:Main
for /f "tokens=1,2,3 delims=," %%a in (NotifyGroup.txt) do (
    set user=%%b
    
    ::set domain=%%a
    ::set email=%c
    ::Note you are not using the other variables so I removed them.

    :: echo the basic headers to the temp file
    echo To: "VSS Middle Tier-USER" ^<%email%^> > %tempmail%
    echo From: "Admin" ^<satishc@synechron.com^> >> %tempmail%
    echo Subject: Checked-Out File/s Status >> %tempmail%
    pause

    "%tfdir%\tf status /s:server /user:%hqdomain%>> %notifydir%\!user!.txt"
    ::Note I removed the CD because you don't need it calling your command this way.
    pause

    :: echo the blank line that separates the header from the body text
    echo.>>%tempmail%
    :: echo the body text to the temp file
    echo !user!,>> %tempmail%

    :: Get File Content
    for /f "tokens=*" %%d in (%notifydir%\!user!.txt) do (
        set filecontent=%%d!%SEPARATOR%!
        echo filecontent=!filecontent!
        echo !filecontent!>> %tempmail%
        pause
    )

    :: delete the file
    del %notifydir%\!user!.txt 
    
    :: move the temp file to the mail pickup directory
    :: adjust this location for your system
    move %tempmail% c:\inetpub\mailroot\pickup

    set tempmail=endlocal
)

I think this makes your code simpler and easy to debug.


Report •

#8
February 14, 2010 at 09:57:18
Ace_Omega,

I'll use the above logic and get back to you on the same.

BTW ... sorry for the late reply and thanks again for the prompt response.

Thanks IVO.


Report •

#9
February 15, 2010 at 00:04:15
Its giving me the following error:
'""C:\Program' is not recognized internal or external command

-> no further processing to create the file
-> Emails blank files

I'm using user, domain and email variables
Following is the code:
(pls note .. from notifygroup.txt its using last row values and iterating twice (e.g. I've two rows)... instead it first time it should pick first row values and second time the second row values)

:: @echo off & setlocal
@echo off setlocal enabledelayedexpansion
set tfdir="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\"
set notifydir="%userprofile%\Desktop\NotifyCheckedOutStatus"
set SEPARATOR=/

:: set the temp file location
set tempmail=%temp%\tempmail.%random%.txt


for /f "tokens=1,2,3 delims=," %%a in (NotifyGroup.txt) do (
set user=%%b
set domain=%%a
set email=%%c

:: echo the basic headers to the temp file
echo To: "Scripting Test" ^<%email%^> > %tempmail%
echo From: "Admin" ^<admin@admin.com^> >> %tempmail%
echo Subject: Checked-Out File/s Status >> %tempmail%
pause

"%tfdir%\tf status /s:server /user:%domain%>>%user%.txt"
pause

:: echo the blank line that separates the header from the body text
echo.>>%tempmail%
:: echo the body text to the temp file
echo %user%.>> %tempmail%


cd\
cd "Documents and Settings\myuser\Desktop\NotifyCheckedOutStatus"

::Get File Content
for /f "tokens=*" %%d in (%user%.txt) do (
set filecontent=%%d!%SEPARATOR%!
echo filecontent=!filecontent!
echo !filecontent!>> %tempmail%
pause
)

:: delete the file
DEL %user%.txt

:: move the temp file to the mail pickup directory
:: adjust this location for your system
move %tempmail% c:\inetpub\mailroot\pickup

set tempmail=endlocal

)


Report •

#10
February 15, 2010 at 03:22:27
I'm able to make it work with the following logic:

PF below:


@echo off

REM @echo off & setlocal
setlocal enabledelayedexpansion

set tfdir=%programfiles%\Microsoft Visual Studio 9.0\Common7\IDE
REM echo %tfdir%

set notifydir=%userprofile%\Desktop\NotifyCheckedOutStatus
REM echo %notifydir%

for /f "tokens=1,2,3 delims=," %%a in (NotifyGroup.txt) do (

REM hqdomain=%%a
REM user=%%b
REM email=%c

REM set the temp file location
set tempmail=%temp%\tempmail.%random%.txt

REM echo the basic headers to the temp file
echo To: "Scripting Test" ^<%%c^> > %tempmail%
echo From: "Admin" ^<admin@admin.com^> >> %tempmail%
echo Subject: Checked-Out File/s Status >> %tempmail%
REM pause

"%tfdir%\tf" status /s:nrtapptf501 /user:%%a>> "%notifydir%\%%b.txt"
echo hi %%b,>>%tempmail%
echo The following files are checked-out on your name. Please ensure that you check-in the files before you leave for the day.>>%tempmail%

REM Get File Content and write into email
type "%notifydir%\%%b.txt" >> %tempmail%

REM delete the file
DEL "%notifydir%\%%b.txt"

REM move the temp file to the mail pickup directory
REM adjust this location for your system
move %tempmail% c:\inetpub\mailroot\pickup

set tempmail=endlocal

)


Report •

Ask Question