3

Batch Script:

@echo off
if not "!!"=="" setlocal enabledelayedexpansion

for /f "tokens=4*" %%a in (
'dir /AD "%USERPROFILE%\" ^| find "DIR" ^| Find /v " ."'
) do if "%%b"=="" (set $dirs=!$dirs!"%%a" ) else (set $dirs=!$dirs!"%%a %%b" )

call :getmaxlen !$dirs!
echo Dirs !$dirs!
echo maklen '%max%'
pause 

:getmaxlen
set var=%*
for %%a in (!var!) do (
    set #L=%%~a
    for /L %%a in (0 1 100) do if not "!#L:~%%a,1!"=="" set len=%%a
    set /a len+=1& set "$Lens=!$Lens!!len! "
)
for %%a in (!$Lens!) do (
    if not defined max Set max=%%a
    if %%a GTR !max! Set max=%%a
)
exit /b

PowerShell Script:

function demo {
    $dirs = (gci $Home -Directory -force).Name
    getmaxlen
    "max len $max"
    pause 
}

function getmaxlen {
    $var = $dirs
    #$var =???
    $Script:max  = ($var | Measure -Maximum -Property Length).Maximum
}

demo

From Batch Script above, what is equivalent of set var=%* in PowerShell call function ?

I mean equivalent of %* in PowerShell. $var =???

So. I can use Powershell function getmaxlen every time i need.

2
  • Please don't edit the answer into your question. Super User is a question and answer site and answers should be separate from questions. Please read Can I answer my own question?
    – DavidPostill
    Commented Apr 10 at 7:05
  • 1
    Thank you for the advice
    – Mr.Key7
    Commented Apr 10 at 8:57

2 Answers 2

3

There is no comparison between the mechanisms of batch and PowerShell for passing arguments.

Basically, PowerShell uses two built-in variables :

Some references that can get you started :

1
  • 1
    Thanks for the reference
    – Mr.Key7
    Commented Apr 10 at 9:02
0

based on an answer in the post: What is the Powershell equivalent for bash $*?

function demo {
    $dirs = (gci $Home -Directory -force).Name
    getmaxlen $dirs
    $max; pause; CLS 
    $dirs = (gci / -Directory -force).Name
    getmaxlen $dirs
    $max; pause; CLS 
    $dirs = (gci $env:Windir -Directory -force).Name
    getmaxlen $dirs
    $max; pause; exit 0
}

function getmaxlen {
    $var = $args | foreach {$_}
    $Script:max  = ($var | Measure -Maximum -Property Length).Maximum
}

demo

in CMD, %* represents all arguments, in PowerShell it is $Args, one of automatic variables.

$var = $args | foreach {$_}

So. function getmaxlen can be called repeatedly

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .