Write a Windows batch shell script to shift and call a command using script arguments #12
Replies: 2 comments
-
|
Engine: Qwen Final result: Iterations:
Final code: @echo off
setlocal enabledelayedexpansion
REM This script shifts the first argument and calls a command with the remaining arguments
REM Display the original arguments
echo Original arguments: %*
REM Shift the first argument
shift
REM Display the shifted arguments
set "shifted_args="
:loop
if "%~1"=="" goto endloop
set "shifted_args=!shifted_args! %1"
shift
goto loop
:endloop
REM Trim leading space from shifted_args
set "shifted_args=%shifted_args:~1%"
echo Shifted arguments: %shifted_args%
REM Call a command with the remaining arguments
REM For demonstration, we'll use "echo" as the command, but you can replace it with any command you need
echo Calling command with remaining arguments...
call echo %shifted_args%
REM Uncomment the following line to call a different command with the remaining arguments
REM call your_command %shifted_args%
endlocalSeems the result is a bit better with less iterations. But mostly the same. |
Beta Was this translation helpful? Give feedback.
-
|
Engine: Qwen Final result: Iterations:
Final code: @echo off
setlocal enabledelayedexpansion
:: Check if at least one argument is provided
if "%~1"=="" (
echo Usage: %~nx0 ^<num_shifts^> ^<command^> [args...]
echo Example: %~nx0 2 dir /s /w "c:\Program Files"
exit /b 1
)
:: Get the number of shifts
set "NUM_SHIFTS=%~1"
shift
:: Validate that the first argument is a number
for /f "delims=0123456789" %%i in ("!NUM_SHIFTS!") do (
if defined NUM_SHIFTS (
echo Error: First argument must be a number
exit /b 1
)
)
:: Check if we have a command after shifting
if "%~1"=="" (
echo Error: No command specified after shift count
exit /b 1
)
:: Store the command (unquoted)
set "COMMAND=%~1"
shift
:: Perform the specified number of additional shifts (NUM_SHIFTS - 1, since we already shifted once for the command)
set /a "remaining_shifts=!NUM_SHIFTS! - 1"
set "counter=0"
:do_shifts
if !counter! lss !remaining_shifts! (
if "%~1"=="" (
echo Warning: Not enough arguments to shift by !NUM_SHIFTS! positions
goto execute_command
)
shift
set /a "counter+=1"
goto do_shifts
)
:execute_command
:: Build the shifted argument string manually
set "SHIFTED_ARGS="
:collect_args
if "%~1"=="" goto run_command
:: Add the current argument with its quotes preserved
set "SHIFTED_ARGS=!SHIFTED_ARGS! %1"
shift
goto collect_args
:run_command
:: Remove leading space from SHIFTED_ARGS if any
if defined SHIFTED_ARGS set "SHIFTED_ARGS=!SHIFTED_ARGS:~1!"
:: Execute the command with the shifted arguments
if defined SHIFTED_ARGS (
echo Executing: !COMMAND! !SHIFTED_ARGS!
!COMMAND! !SHIFTED_ARGS!
) else (
echo Executing: !COMMAND!
!COMMAND!
)
endlocalIt has made the same mistake has using the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Engine: Qwen
Model:
Qwen2.5-Plus(Capable of complex tasks)Asked:
Help me write a Windows batch shell script to shift and call a command using script arguments.Final result:
https://chat.qwenlm.ai/s/85c7ba7b-1a16-47ac-afd5-0aab25566ad9
Iterations:
Final code:
Seems it can not reach the mine variant of the code:
https://github.com/andry81/contools/tree/HEAD/scripts/tools/std/callshift.bat
😄
Beta Was this translation helpful? Give feedback.
All reactions