@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM SET PATH=C:\Program Files (x86)\
set "date1=%date:~-4,4%%date:~-10,2%%date:~7,2%"
set "t=%time: =0%"
set "time1=%t:~0,2%%t:~3,2%"
set "log_file=U:\Desktop\Log_%date1%%time1%.txt"
echo %date1%%time1%
for /f "tokens=*" %%K in (U:\Desktop\Customer_record.txt) do (
echo %%K
set "var=%%K"
REM cd U:\Desktop\!var!
set /a count = 0
for /f "tokens=*" %%a in (U:\Desktop\!var!\Account_number.txt) do (
set /a count += 1
)
echo !var! !count! >>"%log_file%"
REM set count=%count%
if "!count!" == "10" echo "welcome to ABC bank"
if "!count!" == "11" echo "welcome to XYZ bank"
)
TYPE "%log_file%"
GOTO :EOF
Tip: Use set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign "
or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value"
is used, then the quotes become part of the value assigned.
When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause
after statements & home in on the error, but better to open a 'command prompt' & run your batch from there so that the window remains open & any (error) messages will be displayed.
It is normal to use a setlocal
as the command directly following the @echo off
. It has the effect of re-setting all variables to their initial value when the batch finishes so that subsequent executables run in the same session do not receive an environment cluttered by earlier processes.
The variable path
contains a list of the directories that are examined (in order) to find an executable if it is not found in the current directory. Changing it is vigorously discouraged as it will often cause other problems.
date
is a variable that contains, of all things, the current date - in a format which depends on user-settings. time
contains the current time. If you set
either of these variables, the value you set
overrides the value set by the system. Hence, these variable-names should not be used for user-variables.
With these points in mind, I've changed all your C:
to U:
in the above code as U:
is my test drive.
setlocal
restored to its customary place.
Setting of path
commented-out. You appear to not be using it in any case.
date1
replaces date
as a user-variable.
I dislike using cd
as it's easy to lose track of precisely which directory is current, so I incorporated the account_number
filename into the for /f
- the same way as was done for customer_record
echo
ing !count!
to the log file within the for...%%a
loop would simply add lines 1,2,3...to the log. Moved outside of the loop and added !var!
to the report so that it will show the account name and record-count.
Common error : Variables are not behaving as expected
Hence, set count=%count%
would do nothing if that statement was outside of the code block
which starts at for /f ... %%K
and extends to the final )
. But since that statement is within the code block, it would set count
to the value that count
had when the for /f ... %%K
was encountered.
So - then use the run-time value of count
(!count!
) to do the tests. No need for an else
since count
can either be 10
or 11
or something else, but not more than one of these at any time.