Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

@echo off setlocal EnableDelayedExpansion rem Create or clear stresult.txt at the…

  1. User

    @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

  2. Assistant

    Certainly! Below is the modified batch script that encapsulates the processing logic for each store code into a separate subroutine, allowing you to maintain a cleaner and more organized loop structure.

    @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 call :process "%%i"
    goto :compare
    
    :process
        set "storeCode=%~1"
        
        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="
        
        (
        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
        exit /b 0
    
    :compare
    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
    

    Key Changes Made:

    1. Subroutine for Processing: The main processing logic for each store code is encapsulated in the :process subroutine, making the main loop cleaner.
    2. Parameter Handling: The store code is passed as a parameter to the :process subroutine, which is then processed within the subroutine.
    3. Flow Control: The program returns to the main loop with a goto :compare for comparison after finishing processing each store code, promoting a clear separation of concerns.

    This structure allows easier maintenance and understanding of the script, as each section is more self-contained.

  3. User

    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 )

    if found !trasformedCode! in st_result.ext goto next

  4. Assistant

    To check if the !transformedCode! is already present in st_result.txt and skip the corresponding processing if found, you can adjust your batch script to add an additional conditional check prior to proceeding with the actual processing for that store code.

    Here’s how you can incorporate this into the existing structure:

    @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 call :process "%%i"
    goto :compare
    
    :process
        set "storeCode=%~1"
        
        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 Check if !transformedCode! already exists in st_result.txt
        findstr /x /c:"!transformedCode!" st_result.txt > nul
        if not errorlevel 1 (
            echo Found !transformedCode! in st_result.txt. 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="
        
        (
        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
        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
        exit /b 0
    
    :compare
    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
    

    Changes Made:

    1. Check if Transformed Code Exists in st_result.txt: After checking whether the corresponding file exists, the script now also checks whether !transformedCode! exists within st_result.txt using findstr. If it does, the script skips processing for that store code.

    2. Condition to Skip: If findstr identifies the transformed code as present in st_result.txt, the script echoes a message and proceeds to the :next label.

    This approach ensures that store codes that have already been processed successfully and logged in st_result.txt are not processed again.