if else not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
komathi
Posts: 14
Joined: 18 May 2014 04:48

if else not working

#1 Post by komathi » 18 May 2014 05:01

Hi ,

I want to count the lines in my input csv file , success and error files and based on the condition i need to write the status file

if the success count equals to the input count then
echo List.csv,%Counter%,%SuccessCounter%,%date% %time%,True >> in the statusfile
else
echo List.csv,%Counter%,%SuccessCounter%,%date% %time%,false >> in the statusfile

if the error count > 1
send email to user by attaching the error file

I am using this code in bat file to write a file. i set the file path in the %statusfile%, my code not write file neither in if part nor else part

if %Counter% equ %SuccessCounter% (
echo List.csv,%Counter%,%SuccessCounter%,%date% %time%,True >> %statusfile%
) else (
set partUpload_flag=1
echo List.csv,%Counter%,%SuccessCounter%,%date% %time%,False >>%statusfile%
)



this is the code where i'm getting counter, i try printing the counter , getting the line count correct only. also i'm getting first line in the file.
set statusfile=%rootpath%Input\Status.csv
echo FileName,TotalLines,processedLines ,DateTime,status > %statusfile%
set /a Counter =0 FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\Input.csv) Do
( set "line=%%a" set "line="!line:,=","!""
set /a Counter +=1 )

Also one more thing, the condition worked fine for me before i add some more lines to them. once after adding the more lines this stop working, even after removing them it is not working .. the added lines are as below. i added this in the else part.
if %ErrorCounter% gtr 1 (
set success_flag=0
set Attachment= %Attachment% -attach %rootpath%extras\error.csv
echo %date%%time% ERROR:Error found in records : %ErrorCounter% lines >> %logpath%
)


How to fix the issue, as i'm new cant find the way to debug.
Last edited by komathi on 18 May 2014 06:19, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#2 Post by foxidrive » 18 May 2014 05:13

Can you explain what you need to do?

It helps us to understand the situation because code that doesn't work is often not helpful.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#3 Post by komathi » 18 May 2014 05:30

Hi,

I want to count the lines in input file, success file and error files.
if the success count equal to the input count, then
echo List.csv,%Counter%,%SuccessCounter%,%date% %time%,True >> %statusfile%
else
echo List.csv,%Counter%,%SuccessCounter%,%date% %time%,False>> %statusfile%

If error count greater than 1 then
send mail to user by attaching the error.csv

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#4 Post by foxidrive » 18 May 2014 06:49

This is all broken as it is posted.

set statusfile=%rootpath%Input\Status.csv
echo FileName,TotalLines,processedLines ,DateTime,status > %statusfile%
set /a Counter =0 FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\Input.csv) Do
( set "line=%%a" set "line="!line:,=","!""
set /a Counter +=1 )


Try it like this, but the set "line="!line:,=","!"" could be an issue.

Code: Select all

set statusfile=%rootpath%Input\Status.csv
echo FileName,TotalLines,processedLines ,DateTime,status > %statusfile%
set /a Counter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\Input.csv) Do (
set "line=%%a"
set "line="!line:,=","!""
set /a Counter+=1
)


Then run your code and tell us what error messages appear. Put @echo off as the first line.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#5 Post by komathi » 18 May 2014 07:17

Hi

This is my full bat file. i'm calling another bat files in the same folder to do the required operation and i will repeat the same for 3 more files in same script. i'm using blat to send mail.


calling another bat, inserting records are working proper. getting the correct line counts too.
But the condition checking the counter value is not working. not doing the action in if part as well as in else part

if i test each of this condition in separate bat file means working, but all together not working

Code: Select all


@echo off
setlocal EnableDelayedExpansion
:::::::::::::: Lets set variables from XML file ::::::::::::::
SET rootpath=%~dp0
set rootpath=%rootpath:~0,-4%
set logpath=%rootpath%logs\debug.log

for /f %%i in ('XML.EXE sel -t -v "//to" %rootpath%conf\Settings.xml') do set to=%%i
for /f %%i in ('XML.EXE sel -t -v "//port" %rootpath%conf\Settings.xml') do set port=%%i
for /f %%i in ('XML.EXE sel -t -v "//username" %rootpath%conf\Settings.xml') do set username=%%i
for /f %%i in ('XML.EXE sel -t -v "//password" %rootpath%conf\Settings.xml') do set password=%%i
for /f %%i in ('XML.EXE sel -t -v "//server" %rootpath%conf\Settings.xml') do set server=%%i


set PartialUploadMailSubject=file Partially Uploaded
set EmptyInputFileSubject=input file has no data
set debug=-debug -log %logpath% -timestamp
set success_flag=0


set statusfile=%rootpath%InputCSV\ProcessStatus.csv


echo   ---------------------start process----------------------- >> %logpath%
echo   ---------------------start process-----------------------

echo FileName,TotalLines,processedLines ,DateTime,status > %statusfile%

echo Checking the input files

echo  Login to salesforce
call ExtractPreStagingData.bat


echo Extracting PreStagingData



::Delete the existing data in the Pre-Staging object
echo Deleting the Pre-Staging customer code and account code records
echo %date%%time% INFO:Deleting the Pre-Staging customer code and account code records >> %logpath%
call DeletePreStagingData.bat
echo Extracting the Pre-Staging sharing usage records



:: Call the bat file for insert

echo Inserting the records in Pre-Staging
echo %date%%time% INFO:Inserting the CustomerCodeUsage records in Pre-Staging >> %logpath%
call InsertUsage.bat

::Check for the error and success

echo %date%%time% INFO:Checking the Input record counts >> %logpath%

set /a Counter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%InputCSV\Usage.csv) Do (
    set "line=%%a"
    set "line="!line:,=","!""
  set /a Counter+=1
    )
echo %date%%time% INFO: Success Line count in Usage is %Counter% >> %logpath%

echo %date%%time% INFO:Checking the success records >> %logpath%

set /a UsageSuccessCounter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\successUsage.csv) Do (
    set "line=%%a"
    set "line="!line:,=","!""
  set /a UsageSuccessCounter+=1
    )
echo %date%%time% INFO: Success Line count in Usage is %UsageSuccessCounter% >> %logpath%

echo %date%%time% INFO:Checking the error records >> %logpath%

set /a UsageErrorCounter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\errorUsage.csv) Do (
    set "line=%%a"
    set "line="!line:,=","!""
  set /a UsageErrorCounter+=1
    )
echo %date%%time% INFO:Error Line count in Usage is %UsageErrorCounter% >> %logpath%


echo success_flag = %success_flag% >> %logpath%

::to find the object not available error
>nul findstr /c:"PartnerClient.java:1243" %logpath% && (
:SendMailObjectNotFound
blat   -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Object Not Available in Salesforce"  -debug -log %logpath% -timestamp -body %rootpath%EmailBody\ObjectNotAvailableMail.txt -attach %logpath%

)

set zerorec_flag=0
if %Counter% les 2 (
set zerorec_flag=1
set EmptyInputFileSubject=Account List %EmptyInputFileSubject%
)


echo %date%%time% INFO: Deleting the extract files >> %logpath%
del "%rootpath%output\*.*
 
set partUpload_flag=0
if %UsageSuccessCounter% equ %Counter% (
echo Usage.csv,%Counter%,%SuccessCounter%,%date% %time%,True >> %statusfile%
) else (
set partUpload_flag=1
set PartialUploadMailSubject=Usage %PartialUploadMailSubject%
echo Usage.csv,%Counter%,%UsageSuccessCounter%,%date% %time%,False >> %statusfile%
)



set /a one=1
if %ErrorCounter% gtr %one% (
     set success_flag=0
     set Attachment= %Attachment% -attach %rootpath%extras\error.csv
     echo %date%%time% ERROR:Error found in records : %ErrorCounter% lines >> %logpath%
)





if %partUpload_flag% equ 1 (
blat  %rootpath%EmailBody\PartialUploadMail.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "%PartialUploadMailSubject%" -debug -log %logpath% -timestamp
)


if %zerorec_flag% equ 1 (
blat  %rootpath%EmailBody\EmptyInputFile.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "%EmptyInputFileSubject%" -debug -log %logpath% -timestamp
)

::call InsertMPXDataImportprocessStatus.bat

if success_flag=1 GOTO :SendSuccessMail

echo %date%%time% INFO: Process completed >> %logpath%


:SendLoginFailMail
 set success_flag=0
    echo Login failed
    echo  Salesforce Login failed >> %logpath%
blat  %rootpath%EmailBody\ConnectionFailure.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Salesforce Connection Failed" -debug -log %logpath% -timestamp

:SendMailWithErrorFileAttached
blat   %rootpath%EmailBody\FailMail.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Upload Process Failed"  -debug -log %logpath% -timestamp %Attachment%

:SendSuccessMail
blat  %rootpath%EmailBody\SuccessMail.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Upload Process Successfully Completed" -debug -log %logpath% -timestamp


:FileNotFound
blat  %rootpath%EmailBody\FileNotFound.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "%FileNotFoundSubject%" -debug -log %logpath% -timestamp





ENDLOCAL



foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#6 Post by foxidrive » 18 May 2014 07:35

What is the error message?

Place pause here and there and isolate which part is broken.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#7 Post by komathi » 18 May 2014 07:41

Not getting any error
The statusfile is empty, im not getting any line in that

set statusfile=%rootpath%InputCSV\ProcessStatus.csv
if %UsageSuccessCounter% equ %Counter% (
echo Usage.csv,%Counter%,%SuccessCounter%,%date% %time%,True >> %statusfile%
) else (
set partUpload_flag=1
set PartialUploadMailSubject=Usage %PartialUploadMailSubject%
echo Usage.csv,%Counter%,%UsageSuccessCounter%,%date% %time%,False >> %statusfile%
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#8 Post by foxidrive » 18 May 2014 07:42

Put this above that section and see what it shows:

Code: Select all

echo if %UsageSuccessCounter% equ %Counter%
pause


and place pause after that section. Look for error messages.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#9 Post by komathi » 18 May 2014 09:09

If add the pause above the code in red i can see the line count if its below the window get closed.

pause in green is working but the pause in darkred is not working..


@echo off
setlocal EnableDelayedExpansion
:::::::::::::: Lets set variables from XML file ::::::::::::::
SET rootpath=%~dp0
set rootpath=%rootpath:~0,-4%
set logpath=%rootpath%logs\debug.log

for /f %%i in ('XML.EXE sel -t -v "//to" %rootpath%conf\Settings.xml') do set to=%%i
for /f %%i in ('XML.EXE sel -t -v "//port" %rootpath%conf\Settings.xml') do set port=%%i
for /f %%i in ('XML.EXE sel -t -v "//username" %rootpath%conf\Settings.xml') do set username=%%i
for /f %%i in ('XML.EXE sel -t -v "//password" %rootpath%conf\Settings.xml') do set password=%%i
for /f %%i in ('XML.EXE sel -t -v "//server" %rootpath%conf\Settings.xml') do set server=%%i


set PartialUploadMailSubject=file Partially Uploaded
set EmptyInputFileSubject=input file has no data
set debug=-debug -log %logpath% -timestamp
set success_flag=0


set statusfile=%rootpath%InputCSV\ProcessStatus.csv


echo ---------------------start process----------------------- >> %logpath%
echo ---------------------start process-----------------------

echo FileName,TotalLines,processedLines ,DateTime,status > %statusfile%

echo Checking the input files

echo Login to salesforce
call ExtractPreStagingData.bat


echo Extracting PreStagingData



::Delete the existing data in the Pre-Staging object
echo Deleting the Pre-Staging customer code and account code records
echo %date%%time% INFO:Deleting the Pre-Staging customer code and account code records >> %logpath%
call DeletePreStagingData.bat
echo Extracting the Pre-Staging sharing usage records



:: Call the bat file for insert

echo Inserting the records in Pre-Staging
echo %date%%time% INFO:Inserting the CustomerCodeUsage records in Pre-Staging >> %logpath%
call InsertUsage.bat

::Check for the error and success

echo %date%%time% INFO:Checking the Input record counts >> %logpath%

set /a Counter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%InputCSV\Usage.csv) Do (
set "line=%%a"
set "line="!line:,=","!""
set /a Counter+=1
)
echo %date%%time% INFO: Success Line count in Usage is %Counter% >> %logpath%

echo %date%%time% INFO:Checking the success records >> %logpath%

set /a UsageSuccessCounter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\successUsage.csv) Do (
set "line=%%a"
set "line="!line:,=","!""
set /a UsageSuccessCounter+=1
)
echo %date%%time% INFO: Success Line count in Usage is %UsageSuccessCounter% >> %logpath%

echo %date%%time% INFO:Checking the error records >> %logpath%

set /a UsageErrorCounter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%extras\errorUsage.csv) Do (
set "line=%%a"
set "line="!line:,=","!""
set /a UsageErrorCounter+=1
)
echo %date%%time% INFO:Error Line count in Usage is %UsageErrorCounter% >> %logpath%


echo success_flag = %success_flag% >> %logpath%

echo if %UsageSuccessCounter% equ %Counter%
echo if %ErrorCounter% gtr 1
echo if %Counter% les 2
pause


set zerorec_flag=0
if %Counter% les 2 (
set zerorec_flag=1
set EmptyInputFileSubject=Account List %EmptyInputFileSubject%
)


echo %date%%time% INFO: Deleting the extract files >> %logpath%
del "%rootpath%output\*.*



echo if %UsageSuccessCounter% equ %Counter%
echo if %ErrorCounter% gtr 1
echo if %Counter% les 2
Pause


set partUpload_flag=0
if %UsageSuccessCounter% equ %Counter% (
echo Usage.csv,%Counter%,%SuccessCounter%,%date% %time%,True >> %statusfile%
) else (
set partUpload_flag=1
set PartialUploadMailSubject=Usage %PartialUploadMailSubject%
echo Usage.csv,%Counter%,%UsageSuccessCounter%,%date% %time%,False >> %statusfile%
)



set /a one=1
if %ErrorCounter% gtr %one% (
set success_flag=0
set Attachment= %Attachment% -attach %rootpath%extras\error.csv
echo %date%%time% ERROR:Error found in records : %ErrorCounter% lines >> %logpath%
)





if %partUpload_flag% equ 1 (
blat %rootpath%EmailBody\PartialUploadMail.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "%PartialUploadMailSubject%" -debug -log %logpath% -timestamp
)


if %zerorec_flag% equ 1 (
blat %rootpath%EmailBody\EmptyInputFile.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "%EmptyInputFileSubject%" -debug -log %logpath% -timestamp
)

::call InsertMPXDataImportprocessStatus.bat

if success_flag=1 GOTO :SendSuccessMail

echo %date%%time% INFO: Process completed >> %logpath%


:SendLoginFailMail
set success_flag=0
echo Login failed
echo Salesforce Login failed >> %logpath%
blat %rootpath%EmailBody\ConnectionFailure.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Salesforce Connection Failed" -debug -log %logpath% -timestamp

:SendMailWithErrorFileAttached
blat %rootpath%EmailBody\FailMail.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Upload Process Failed" -debug -log %logpath% -timestamp %Attachment%

:SendSuccessMail
blat %rootpath%EmailBody\SuccessMail.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "Upload Process Successfully Completed" -debug -log %logpath% -timestamp


:FileNotFound
blat %rootpath%EmailBody\FileNotFound.txt -server %server% -f %username% -u %username% -pw %password% -port %port% -to %to% -subject "%FileNotFoundSubject%" -debug -log %logpath% -timestamp




)


ENDLOCAL



foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#10 Post by foxidrive » 18 May 2014 09:13

This line has a syntax error. You need LSS or LEQ etc.

if %Counter% les 2 (

See how you go debugging it now.

Open a cmd window and launch the batch file - you will see any fatal errors too, though sometimes the error is still not obvious.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#11 Post by komathi » 18 May 2014 10:45

I changed the les to lss, and run in cmd

getting error as "less was unexpected at this time"
i tryied by changing the place of the code

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#12 Post by foxidrive » 18 May 2014 11:12

komathi wrote:I changed the les to lss, and run in cmd

getting error as "less was unexpected at this time"
i tryied by changing the place of the code


You typed less instead of lss the error message is telling us, or somewhere else there is less in the wrong place.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#13 Post by komathi » 18 May 2014 12:30

yes i had 'less' in my file..

Thank a lot, It's running now without error.

Is there any way to write the error we getting in cmd window in log file?
in case of the error my log file ends without any information.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if else not working

#14 Post by foxidrive » 18 May 2014 12:36

I don't know what error you are referring to.

komathi
Posts: 14
Joined: 18 May 2014 04:48

Re: if else not working

#15 Post by komathi » 18 May 2014 12:56

I mean error getting in CMD windows, even any thing like fatal error

for eg: "less was unexpected at this time"

If i want to write this error in my log means what need to do?

Post Reply