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

@echo off setlocal EnableDelayedExpansion ::set maximum job if not exist "jobs" ( mkdir…

  1. User

    @echo off setlocal EnableDelayedExpansion

    ::set maximum job if not exist "jobs" ( mkdir "jobs" ) else ( del /q jobs* )

    :start ::check if job is running set "max_job=5" set /a "job=0" for /f %%i in ('dir /b "jobs*.job" 2^>nul ^| find /c /v ""') do set job=%%i

    :: If we reached the max concurrent limit, wait if !job! geq %max_job% ( timeout /t 5 >nul goto start )

    ::read file st.txt set "store_id=st.txt" for /f "usebackq tokens=1 delims=| " %%a in (!store_id!) do call :process "%%a" goto start

    :process :: Create a job file for tracking (to manage concurrent jobs) echo Job for %%a > "jobs\job_%%a.job"

    :: Start the check_server.bat script in the background echo Starting Job for %%a ... start /b "" cmd /c "test.bat %%a >> "summary.txt" 2>&1"

    :: place delete command in batch that call :: del "jobs\job_%%a.job" :: exit /b 0

    :check :: Check if all jobs are done if exist "jobs*.job" ( timeout /t 5 >nul goto check )

    ::count pending job set /a "pending_job=0" for /f %%i in ('dir /b "jobs*.job" 2^>nul ^| find /c /v ""') do set pending_job=%%i if !pending_job! geq 1 ( echo There are still pending jobs. Restarting loop. timeout /t 10 /nobreak goto start )

    echo All jobs are done. pause

    endlocal

    correct code above

  2. Assistant

    Your batch script has a few issues and areas for improvement. Below is a revised version of your script that should work more effectively. Note that I added comments where necessary to explain the changes.

    @echo off
    setlocal EnableDelayedExpansion
    
    :: Set maximum job limit
    set "max_job=5"
    
    :: Create jobs directory if it doesn't exist, or clear it if it does
    if not exist "jobs" (
        mkdir "jobs"
    ) else (
        del /q jobs\*
    )
    
    :start
    :: Check how many jobs are currently running
    set /a "job_count=0"
    for /f %%i in ('dir /b "jobs\*.job" 2^>nul ^| find /c /v ""') do set job_count=%%i
    
    :: If we reached the max concurrent limit, wait
    if !job_count! geq %max_job% (
        echo Max concurrent jobs reached. Waiting...
        timeout /t 5 >nul
        goto start
    )
    
    :: Read from st.txt file
    set "store_id=st.txt"
    for /f "usebackq tokens=1 delims=| " %%a in (!store_id!) do (
        call :process "%%a"
    )
    
    :: Check if all jobs are done before starting over
    goto check
    
    :process
    :: Create a job file for tracking 
    echo Job for %~1 > "jobs\job_%~1.job"
    
    :: Start the test.bat script in the background
    echo Starting Job for %~1 ...
    start /b "" cmd /c "test.bat %~1 >> summary.txt 2>&1"
    
    :: Exit the process without returning an unintended value
    exit /b 0
    
    :check
    :: Check if there are still running jobs
    if exist "jobs\*.job" (
        echo Checking for pending jobs...
        timeout /t 5 >nul
        goto check
    )
    
    :: Count pending jobs
    set /a "pending_job=0"
    for /f %%i in ('dir /b "jobs\*.job" 2^>nul ^| find /c /v ""') do set pending_job=%%i
    if !pending_job! geq 1 (
        echo There are still pending jobs. Restarting loop.
        timeout /t 10 /nobreak
        goto start
    )
    
    echo All jobs are done.
    pause
    
    endlocal
    

    Key Changes:

    1. Job Count Management: I made sure to consistently use a variable called job_count instead of job, which might have been confusing.
    2. Process Calling: Moved the call :process to within parentheses to maintain scopes and ensure values are correctly passed.
    3. Job Deletion: You need to ensure that the job file is deleted after the job is done. You can include a del command inside the test.bat script after its execution to clean up the job file. However, if you want to manage it here, you can call another block of code to remove the job file after the related job process is done.
    4. Improved Output and Waiting Messages: Added some echo statements to inform the user of what is happening, particularly during waiting periods.

    Make sure that your test.bat script and st.txt are correctly formatted and available in the same directory where you run this script. Adjust the path to test.bat if necessary.

  3. User

    @echo off setlocal EnableDelayedExpansion

    :: Set maximum job limit set "max_job=3"

    :: Create jobs directory if it doesn't exist, or clear it if it does if not exist "jobs" ( mkdir "jobs" ) else ( del /q jobs* )

    :start :: Check how many jobs are currently running set /a "job_count=0" for /f %%i in ('dir /b "jobs*.job" 2^>nul ^| find /c /v ""') do set job_count=%%i

    :: If we reached the max concurrent limit, wait if !job_count! geq %max_job% ( echo Max concurrent jobs reached. Waiting... timeout /t 5 >nul goto start )

    :: Read from st.txt file set "store_id=st.txt" for /f "usebackq tokens=1 delims=| " %%a in (!store_id!) do ( call :process "%%a" )

    :: Check if all jobs are done before starting over goto check

    :process :: Create a job file for tracking echo Job for %~1 > "jobs\job_%~1.job"

    :: Start the test.bat script in the background echo Starting Job for %~1 ... start /b "" cmd /c "test.bat %~1 >nul"

    ::place delect job file in your batch file ::del "jobs\job_%~1.job"

    :: Exit the process without returning an unintended value exit /b 0

    :check :: Check if there are still running jobs if exist "jobs*.job" ( echo Checking for pending jobs... timeout /t 5 >nul goto check )

    :: Count pending jobs set /a "pending_job=0" for /f %%i in ('dir /b "jobs*.job" 2^>nul ^| find /c /v ""') do set pending_job=%%i if !pending_job! geq 1 ( echo There are still pending !pending_job! jobs. Restarting loop. timeout /t 10 /nobreak goto start )

    echo All jobs are done. pause

    endlocal

    ใน fold jobs ควรจะมีไฟล์ถูกสร้าง 3 ไฟล์ แต่ทำไมถูกสร้างทั้งหมดตามไฟล์ st.txt

  4. Assistant

    ปัญหาที่คุณพบเนื่องจากการล็อกฟังก์ชัน :process ทำให้ทุกๆ ไฟล์ใน st.txt ถูกเรียกใช้ โดยที่จำนวน job ที่คุณระบุใน max_job ไม่นำมาใช้เพื่อตัดสินใจจะเริ่มการทำงานของ job ใหม่ คุณจะต้องให้โปรแกรมรอให้ job ก่อนหน้าที่เริ่มทำงานเสร็จก่อนที่จะเรียกใช้งาน job ใหม่

    สิ่งที่คุณควรทำคือให้เลื่อนการอ่านไฟล์ st.txt ไปในขั้นตอนที่ตรวจสอบว่า job ที่กำลังทำงานอยู่มีอยู่หรือไม่ ทำให้ script รู้ว่าต้องรอที่จะแก้ไข job ใหม่ คุณสามารถใช้โครงสร้างนี้แทน:

    @echo off
    setlocal EnableDelayedExpansion
    
    :: Set maximum job limit
    set "max_job=3"
    
    :: Create jobs directory if it doesn't exist, or clear it if it does
    if not exist "jobs" (
        mkdir "jobs"
    ) else (
        del /q jobs\*
    )
    
    :start
    :: Check how many jobs are currently running
    set /a "job_count=0"
    for /f %%i in ('dir /b "jobs\*.job" 2^>nul ^| find /c /v ""') do set job_count=%%i
    
    :: If we reached the max concurrent limit, wait
    if !job_count! geq %max_job% (
        echo Max concurrent jobs reached. Waiting...
        timeout /t 5 >nul
        goto start
    )
    
    :: Read from st.txt file
    set "store_id=st.txt"
    for /f "usebackq tokens=1 delims=| " %%a in (!store_id!) do (
        if !job_count! lss %max_job% (
            call :process "%%a"
            set /a job_count+=1
        )
    )
    
    :: Check if all jobs are done before starting over
    goto check
    
    :process
    :: Create a job file for tracking 
    echo Job for %~1 > "jobs\job_%~1.job"
    
    :: Start the test.bat script in the background
    echo Starting Job for %~1 ...
    start /b "" cmd /c "test.bat %~1 >nul"
    
    :: Place delete job file in your batch file
    :: add a timeout or any other logic here to make sure it deletes after finished
    del "jobs\job_%~1.job"
    
    :: Exit the process without returning an unintended value
    exit /b 0
    
    :check
    :: Check if there are still running jobs
    if exist "jobs\*.job" (
        echo Checking for pending jobs...
        timeout /t 5 >nul
        goto check
    )
    
    :: Count pending jobs
    set /a "pending_job=0"
    for /f %%i in ('dir /b "jobs\*.job" 2^>nul ^| find /c /v ""') do set pending_job=%%i
    if !pending_job! geq 1 (
        echo There are still pending !pending_job! jobs. Restarting loop.
        timeout /t 10 /nobreak
        goto start
    )
    
    echo All jobs are done.
    pause
    
    endlocal
    

    คำอธิบายการเปลี่ยนแปลงในโค้ด:

    1. เพิ่มการตรวจสอบจำนวนงาน: ในโครงสร้างการอ่านจากไฟล์ st.txt, เพิ่มการตรวจสอบ !job_count! lss %max_job% เพื่อให้แน่ใจว่า script จะเรียกใช้ :process เฉพาะเมื่อจำนวน job ที่ทำงานอยู่ต่ำกว่าขีดจำกัด
    2. เพิ่มการปรับจำนวน jobs ที่ทำงานอยู่: หลังการสร้าง job ใหม่ จะมีการเพิ่มตัวแปร job_count เพื่อให้ตรงกับงานที่เกิดขึ้นในระบบทันที

    แบบนี้จะช่วยให้คุณควบคุมจำนวนไฟล์ที่ถูกสร้างในโฟลเดอร์ jobs ให้ไม่เกินจำนวนที่กำหนดได้