user2646075
user2646075

Reputation: 1

Writing file from batch not working

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.

Upvotes: 0

Views: 104

Answers (2)

Alex
Alex

Reputation: 917

Looks like it's not hitting the if condition, can you post the whole block? Here are the lines that i'm trying and it's still working for me (i tried both true and false statements):

if 1 equ 1 (
echo abc,123,%date%,%time%,true >>true.txt
) else (
echo abc,123,%date%,%time%,false >>false.txt

)

EDIT: Seems like that problem is how you're setting the variables. You need to put the = right next to the var (unless you're intending to use the var with a space) example set /a Counter+=1. Like so:

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
)

If you don't change that then you need to use the variable with the space, like so: if %Counter % equ %SuccessCounter%

Upvotes: 0

Get-HomeByFiveOClock
Get-HomeByFiveOClock

Reputation: 148

Just ran the code exactly as-is except for stubbing the unknown variables:

set counter=1
set successcounter=1
set statusfile=output.txt

Ran just fine. Can you post up where you set the variables for counter, successcounter and statusfile? I highly suspect you may be having issues setting variables in a way that batch files read.

P.S. also changed

successcounter=2 

to see "False", behaved as expected, so the syntax is not a problem.

Upvotes: 1

Related Questions