@echo off
setlocal EnableDelayedExpansion

rem Create or clear st_result.txt at the beginning
echo. > st_result.txt

:loop
rem Step 1: Loop through each store code in st.txt
for /f "usebackq delims=" %%i in ("st.txt") do (
    set "storeCode=%%i"

    rem Step 2: Transform store code to PTH12345
    set "transformedCode=PTH!storeCode!"
    echo Processing code: !transformedCode!
 
    rem Step 3: Check if the PTH12345 file exists
    if exist "!transformedCode!" (
        echo Found the file: !transformedCode!
        echo Skipping to next store code.
        goto :next
    )

    rem Step 4: Ping PTH12345
    ping -n 1 "!transformedCode!" > nul 2>&1
    if errorlevel 1 (
        echo !transformedCode! is offline. Stopping for this store code.
        goto :next
    )

    rem Step 5: Get Date
    osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "set nocount on; select convert(varchar(10), getdate()-1, 120)" -h-1 -w150 > date.dat
    if errorlevel 1 (
        echo Failed to retrieve date. Check OSQL command.
        goto :next
    )
    
    set /p day=<date.dat
    set mm=!day:~6,2!
    set dd=!day:~9,2!
    del date.dat
    set "brandFile=BRAND_!mm!!dd!.!storeCode!"
    set "departmentFile=DEPARTMENT_!mm!!dd!.!storeCode!"
    set "productFile=PRODUCT_!mm!!dd!.!storeCode!"
    echo !productFile!

    rem Step 6: Run the osql command and check result for the specific product pattern
    set "result="  rem Initialize result variable to empty

    (
    rem The outer single quotes are to ensure the SQL command is executed inside SQL Server
    echo exec xp_cmdshell 'dir d:\FTPRoot\transfIN\!productFile!'
    ) > check_product.sql

    osql -UTRANSF -PFSNART -S!transformedCode! -i check_product.sql > result.dat
    type result.dat
    del /q check_product.sql > nul
    rem Check if result.dat contains the product filename
    findstr /c:"PRODUCT_!mm!!dd!.!storeCode!" result.dat > nul
    if not errorlevel 1 (
        set "result=PRODUCT_!mm!!dd!.!storeCode!"
    )

    rem Clean up the output file
    del result.dat

    rem Step 7: Check result
    if defined result (
        echo Found !result!
        echo Next Store
        echo !storeCode! >> st_result.txt
        goto :next
    )

    rem If not found, proceed to download
    echo Not Found !result!
    (
    echo user hotrn
    echo hotrn
    echo cd ..
    echo cd ..
    echo cd Aldata/LIFE/HEAD/SAV/DOWN/POS
    echo hash
    echo get !productFile! D:\map_drive\wprx\!productFile!
    echo hash
    echo get !departmentFile! D:\map_drive\wprx\!departmentFile!
    echo hash
    echo get !brandFile! D:\map_drive\wprx\!brandFile!
    echo bye
    ) > ftpcmd.dat
    type ftpcmd.dat
    echo ftp -n -s:ftpcmd.dat 10.4.9.128 & pause
    rem START /B cmd /c "ftp -n -s:ftpcmd.dat 10.4.9.128 > NUL 2>&1"
    del ftpcmd.dat
    echo Downloaded files to D:\map_drive\wprx\

    rem Step 8: Copy files to the PTH12345 folder
    (
        echo exec xp_cmdshell 'net use x: \\172.16.141.210\map_drive "user&pass002" /user:ho\2083761'
        echo exec xp_cmdshell 'copy x:\wprx\* d:\FTPRoot\TransfIN\ /y'
        echo exec xp_cmdshell 'net use x: /d /y' 
    ) > transfer2store.sql
    
    echo osql -UTRANSF -PFSNART -S!transformedCode! -i transfer2store.sql & pause
    echo Copied files complete for store code !storeCode!
    del /q transfer2store.sql > nul

    rem Caution: Ensure you want to delete all files in the D:\map_drive\wprx directory.
    del /q D:\map_drive\wprx\* > nul

    :next
    echo continue this loop > nul
)

rem Step 9: Compare st.txt and st_result.txt
fc /b st.txt st_result.txt > nul 2>&1
if errorlevel 1 (
    echo st.txt and st_result.txt are different. Restarting loop.
    timeout /t 10 /nobreak
    goto loop
) else (
    echo st.txt and st_result.txt are the same. Stopping program.
)

:end
echo Script finished processing all store codes.
pause
endlocal

can you make it like

@echo off
setlocal EnableDelayedExpansion

:loop
rem Step 1: Loop through each store code in st.txt
for /f "usebackq delims=" %%i in ("st.txt") do call :process "%%i"
:next

:process
    rem Step 2: Transform store code to PTH12345
    rem Step 3: Check if the PTH12345 file exists
    rem Step 4: Ping PTH12345
    rem Step 5: Get Date
    rem Step 6: Run the osql command and check result for the specific product pattern
    rem Step 7: Check result
    rem Step 8: Copy files to the PTH12345 folder
    :next
)
rem Step 9: Compare st.txt and st_result.txt
:end
echo Script finished processing all store codes.
pause
endlocal