Windows Command queue?

7,951

Solution 1

easiest thing to do is to write a batch file for what you want do, for example open notepad and copy the following:

@echo off
echo Starting batch script
copy c:\temp\*.* c:\temp2\*.*
del c:\temp2\test.exe
echo all file operations complete

Next, save this somewhere as anything .bat and you can run it either by double clicking, or going to the command prompt and typing its name. All operatings you write in it will get processed in order.

Solution 2

I wrote this little batch script some time ago called QSTART. Maybe it will help.

It allows you (in theory) to create and execute queues of any BATCH commands. Queues are plain text files stored in %TEMP% directory.

It's a very simple script I use i.e. to queue zip commands when doing backups.

I didn't do much debugging, therefor feel free to correct any existing bugs. I'm not 100% sure if it handles properly every BATCH command string, but it should suit for simple tasks.

Important. Every queued command is executed exactly as it is. The script doesn't check for any errors, but simple ERRORLEVEL validation inside the FOR loop can fix that.

Also I'm not a professional programmer and am aware that the script is a bit messy (ie. I didn't know any other way to run queued command; the built-in START command sometimes doesn't work for me).

There is help and some examples at the end of the script. Use QSTART without arguments to display it.

@echo OFF
setlocal

rem qstart root directory
set ROOT=%~dp0
call :DEBUG "qstart root directory: %ROOT%"

rem queue storage directory
set QDIR=%TMP%
call :DEBUG "queue directory: %QDIR%"

rem 1.parameter (mandatory) - queue ID
rem if no queue ID given display help message
set QID=%~1
call :DEBUG "queue ID: %QID%"
if "%QID%"=="" goto :MSG_HELP
set QFILE=%QDIR%\%QID%.Q
call :DEBUG "queue file: %QFILE%"
shift

rem 2. parameter (mandatory) - queue operation
set QOP=%~1
shift
call :DEBUG "queue operator: %QOP%"

if "%QOP%"=="add" goto :QADD
if "%QOP%"=="list" goto :QLIST
if "%QOP%"=="load" goto :QLOAD
if "%QOP%"=="new" goto :QNEW
if "%QOP%"=="remove" goto :QREMOVE
if "%QOP%"=="run" goto :QRUN
if "%QOP%"=="save" goto :QSAVE
goto :ERR_SYNTAX

rem add command to queue
rem create queue if not exists
:QADD
    if not exist "%QFILE%" call :QNEW
    set QCMD=:
    :NEXTPAR
        set QCMD=%QCMD% %1
        shift
        if not "%~1"=="" goto :NEXTPAR
    set QCMD=%QCMD:: =%
    call :DEBUG "queued command: %QCMD%"
    echo %QCMD% >>"%QFILE%"
    goto :EOF

rem list queued commands
rem warn if queue not exists
:QLIST
if not exist "%QFILE%" (
    call :ERR_BADQID
) else (
    type "%QFILE%"
)
goto :EOF

rem import queue from file
rem create queue if not exists
rem warn if file not exists
:QLOAD
    if not exist "%QFILE%" call :QNEW
    set FILE=%~1
    call :DEBUG "load file: %FILE%"
    if not exist "%FILE%" (
        call :ERR_NOFILE
    ) else (
        copy /B /Y "%QFILE%"+"%FILE%" "%QFILE%" >NUL
    )
    goto :EOF

rem clear queue
rem create queue if not exists
:QNEW
    if exist "%QFILE%" call :QREMOVE
    copy /B /Y NUL "%QFILE%" >NUL
    goto :EOF

rem remove queue
rem warn if queue not exists
:QREMOVE
    if not exist "%QFILE%" (
        call :ERR_BADQID
    ) else (
        del /F /Q "%QFILE%" >NUL
    )
    goto :EOF

rem execute queued commands
rem clear queue after execution
:QRUN
    if not exist "%QFILE%" (
        call :ERR_BADQID
    ) else (
        setlocal ENABLEDELAYEDEXPANSION
        for /F "tokens=* delims=" %%C in (%QFILE%) do (

            %%C
            rem alt.way of execution: start "" /B /WAIT %%C

            call :DEBUG "ERROR LEVEL of last operation: !ERRORLEVEL!"
        )
        endlocal
        rem call :QREMOVE
        call :QREMOVE
    )
    goto :EOF

rem export queue to file
rem warn if queue not exists
rem overwrite file if exists
:QSAVE
    set FILE=%~1
    call :DEBUG "save file: %FILE%"
    if not exist "%QFILE%" (
        call :ERR_BADQID
    ) else (
        copy /B /Y "%QFILE%" "%FILE%" >NUL
    )
    goto :EOF


rem messages ------------------------------------------------------------------

rem bad syntax error
rem show help
:ERR_SYNTAX
    echo ERROR: syntax error
    call :MSG_HELP
    goto :EOF

rem bad queue id error
:ERR_BADQID
    echo ERROR: bad queue ID '%QID%'
    goto :EOF

rem file not found error
:ERR_BADFILE
    echo ERROR: file not found '%FILE%'
    goto :EOF

rem usage information
:MSG_HELP
    echo qstart v.0.1.5 - by [email protected]
    echo Allows to create and execute queues of BATCH commands.
    echo.
    echo USAGE: qstart {QUEUE_ID} {QUEUE_OPERATOR} {COMMAND or FILE}
    echo        qstart {-h^|--help^|?^|/?}
    echo   {QUEUE_ID}          queue ID
    echo   {QUEUE_OPERATOR}    queue operator
    echo   {COMMAND}           queued command call
    echo   {FILE}              import/export filename
    echo   -h --help ? or /?   shows ^(this^) help message
    echo Allowed operations:
    echo   add {COMMAND}   adds command to the queue
    echo   list            lists all queued commands
    echo   load {FILE}     imports ^(appends^) queued commands from a file
    echo   new             creates new or clears existing queue
    echo   remove          deletes queue
    echo   run             executes all queued command and deletes queue
    echo   save {FILE}     exports queue to a file
    echo ALSO:
    echo   set QDEBUG=1    turns on displaying debug messages
    echo EXAMPLES:
    echo   qstart Hello add echo "Hello world!"
    echo   qstart Hello add pause
    echo   qstart Hello list
    echo   qstart Hello save Hello-copy.txt
    echo   qstart Hello new
    echo   qstart Hello load Hello-copy.txt
    echo   qstart run
    pause
    goto :EOF

rem display debug message and pause
:DEBUG
    if "%QDEBUG%"=="1" (
        echo ### DEBUG INFO ### %~1
        pause >NUL
    )
    goto :EOF
Share:
7,951

Related videos on Youtube

Stefano
Author by

Stefano

Updated on September 17, 2022

Comments

  • Stefano
    Stefano over 1 year

    I'm thinking if does exist some kind of software that can put in a queue a bunch of windows commands. For example I can say to first copy some file somewhere, then rename those, then delete the old files, then edit one of them etc. without waiting the effective execution of any of those passages. This could be useful when copying big files that take a lot and I don't want to sit in front of the computer keeping the eyes on the progress bar.

    Does anything like this exist?

  • Stefano
    Stefano over 14 years
    yes i know this solution, but it's a bit frustrating to write down paths everytime... this functionality should be integrated wihtin windows, but they are too lazy :(
  • DaveParillo
    DaveParillo over 14 years
    So... you're looking for a program that "knows" where you want to copy files and which ones you might want to edit? How about in Wil's example change the paths with variables: copy %1% %2% ??
  • William Hilsum
    William Hilsum over 13 years
    @syockit - used the 7zip command line version, extract to a temporary directory, do the operations on that etc. There is nearly always a way to use the command line for anything!
  • syockit
    syockit over 13 years
    @Wil note that it doesn't have to be just 7zip; it can be any windows action for that matter. Batch is great when you know what you want to do beforehand. But sometimes, after starting a task, you want to add another task to the queue, and explicitly want it to start after the last task has finished. Now, is there any way to do this? Most schedulers I know will let you set the time, but not other conditions.
  • William Hilsum
    William Hilsum over 13 years
    @Syockit - you can actually modify a .bat whilst it is running, and if it hasn't reached that line yet, it will run the new command... E.g. if I write "echo test, pause" and during the pause, I modify to "echo test, pause, echo blabla"... when I press enter, I will actually see blabla.
  • syockit
    syockit about 13 years
    @Wil: Neat! I've always thought that .bat is read to memory before execution. Now I know that it reads line by line after last command is executed.
  • William Hilsum
    William Hilsum about 13 years
    @syockit - tbh, even after 15+ years of using them, I only discovered this was possible by accident a few weeks ago!
  • Arjan
    Arjan over 10 years
    Ahum, "little" batch script? Nice!