0

I have wrote the below script to read the customers from customer_record.txt, file then go inside each customer folder and finding the count of account number for that customer, similarly get the count of other customers account information until read gets completed. After that I am trying to compare count value based on that it should call specific function but it didn't work.

@echo off
SET PATH=C:\Program Files (x86)\
set date=%date:~-4,4%%date:~-10,2%%date:~7,2%
set t=%time: =0%
set time1=%t:~0,2%%t:~3,2%
set log_file=C:\Desktop\Log_%date%%time1%.txt
echo %date%%time1%
setlocal enabledelayedexpansion
for /f "tokens=*" %%K in (C:\Desktop\Customer_record.txt) do (
    echo %%K
    set var=%%K
    cd C:\Desktop\!var!
    set /a count = 0
    for /f "tokens=*" %%a in (Account_number.txt) do (
        set /a count += 1
        echo !count! >>%log_file%
    )
    set count=%count%
    if "%count%" == "10" (
        echo "welcome to ABC bank" 
    )ELSE if "%count%" == "11" (
        echo "Welcome to XYZ bank"
    )
)
PAUSE

Probable output should we expect:

martin--7777777777
10
"welcome to ABC bank" 
James--88888888888
11
"Welcome to XYZ bank"
Scott--55555555555
11
"Welcome to XYZ bank"
Philip--6666666666
10
"welcome to ABC bank"
Compo
  • 36,585
  • 5
  • 27
  • 39
Ravi
  • 11
  • 2
  • 1
    You can't have labels within a code block (your outer `for` loop here). Well, technically you *can*, but they tend to do strange things and jumping to them makes no sense (as jumping breaks the loop) anyway. Also, a `goto` immediately breaks the loop, so `goto` does also not work in a loop or code block (except you are intentionally leaving the loop, but then the label is supposed to be *outside* the loop) – Stephan Mar 02 '23 at 08:28
  • thanks for the reply, how do we achieve above similar results in that case, i want to run the sequence of commands to perform based on count(if) condition satisfies any suggestion. – Ravi Mar 02 '23 at 10:11
  • I should also say, based upon your code, the output you are hoping for is never going to happen. The count will always reach 10, before 11, and as such will always run `echo "welcome to ABC bank"`, and not increment further beyond that. It would be better therefore if you [Edit] your question, to better explain the intended task. It is not currently possible for us to determine what your intention is. It is also important to mention that unless you are absolutely certain what you are doing in a cmd.exe session, you should not overwrite the critical `PATH` variable value list. – Compo Mar 02 '23 at 11:44
  • use `if` with brackets instead of a `goto`. – npocmaka Mar 02 '23 at 12:10

1 Answers1

0
@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

echoing !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.

Magoo
  • 77,302
  • 8
  • 62
  • 84