Shift command not working in wrapper script

Microsoft Windows
Post Reply
jason404
Posts: 10
Joined: 2022-Oct-16, 4:17 am

Shift command not working in wrapper script

Post by jason404 »

I am using scoop . The search command is very slow, so there is a drop-in replacement written in Golang which is much faster: https://github.com/shilangyu/scoop-search

While there is a PowerShell function to replace `scoop search [package]` with `scoop-search.exe [package]`, there isn't one for cmd.exe, so I am trying to make one. This wrapper script is what I have come up with, but it isn't working:

Code: Select all

@echo off
if "%1" == "search" (
    shift
    scoop-search.exe %*
) else (
    powershell scoop.ps1 %*
)
The problem is that the `shift` line isn't working for some reason, so the command that gets passed to the shell is `scoop-search search [package]`. How do I get rid of the `search`? Why isn't `shift` doing what I expected?
jason404
Posts: 10
Joined: 2022-Oct-16, 4:17 am

Re: Shift command not working in wrapper script

Post by jason404 »

I tried everything, but couldn't get the shift command to work, even outside the if conditional and in its own subroutine. After a lot of guiding and swearing I got ChatGPT to come up with this, and it finally works:

Code: Select all

@echo off

if "%1" == "search" (
    call :search_subroutine %*
) else (
    powershell scoop.ps1 %*
)
goto :eof

:search_subroutine
set "args=%*"
set "newargs=%args:* =%"
scoop-search.exe %newargs%
goto :eof
I would still like to know why `shift` didn't work though.
jeb
Posts: 12
Joined: 2023-May-10, 1:28 pm

Re: Shift command not working in wrapper script

Post by jeb »

The SHIFT command only move the numbered arguments %1,%2,...,%9, but has no effect on %*

The solution of chatGPT works in this case, because the first argument seems to be a single word.
But it's not a general solution, because it fails in the case where the first argument includes a space .

Code: Select all

myBatch "go to" arg2 arg 3
jason404
Posts: 10
Joined: 2022-Oct-16, 4:17 am

Re: Shift command not working in wrapper script

Post by jason404 »

Ah, I see. Thanks. That wouldn't be a problem here as all the scoop subcommands are single words and I think that would generally be the case in things like this. Cheers.
Post Reply