Page 1 of 1

Functions Wanted

Posted: 03 Jun 2011 02:33
by Cleptography
I would like to build a script that consist of nothing more then functions that can be called from any other script.
Something like Script.bat :_label arguments.
If wanted to use delete files by date, or create a macro, etc...
There seem to be a lot of bright minds here, so I am looking for some script donations that can be added to this library.

Thank You-

Re: Functions Wanted

Posted: 03 Jun 2011 02:48
by Ed Dyreen
digg, digg digg

Did you look for milk and cookies at:
http://www.dostips.com/

Look for @DBenham's files, his links are all over the place. He's a very skilled programmer.

I also will post some more in my 'My contribution, a function definitions list' thread in the coming days. not yet though :|

Re: Functions Wanted

Posted: 04 Jun 2011 06:43
by allal
Cleptography wrote:I would like to build a script that consist of nothing more then functions that can be called from any other script.
Something like Script.bat :_label arguments.


Please cmd tell me where can you make a place for my custom functions

be fair and stop dreaming

what you really want and what is needed and what is useful in this case is to call directly the function followed by its parameter and unfortunately this doesn't exist in cmd,use vbscript wsc file or powershell and put your functions to be called in powershell profile which loaded with powershell interactive shell and script executing and this is the flexible way to do it i know so far

powershell example:

Code: Select all

  function test-space {
  Param
   (
      [parameter(Mandatory=$true)]
      [String]$path
   )
 
   if (($path | select-string " ") -ne $null) {return $true} else {return $false}



to call the function test-space simply write

Code: Select all

 ps > test-space "c:\program files"   


output

Code: Select all

ps > true



you may try this,this is not what you need and sorrily this is all what cmd can do for you

Code: Select all

@echo off

set "calledfunction=%~1"
set count=0
set "params="
for %%G in (%*) do ( call echo %%count%%|findstr /i /l "0" >nul 2>&1 && (
    set /a count+=1 ) || call set "params=%%params%% %%~G"
)

echo function to call is %calledfunction%
echo parameter is %params%
pause
call %calledfunction% %params%
goto :eof

:joinpath
set "_path=%~1"
set "_childpath=%~2"

REM set _path=%_path:"=%
REM set _childpath=%_childpath:"=%

if not %_path:~-1%==\ set "_path=%_path%\"
if %_childpath:~0,1%==\ set "_childpath=%_childpath:~1%"
set joined="%_path%%_childpath%"
endlocal & echo fullpath is %joined% &exit /b




script.bat :joinpath %windir% system32

the function exist in the script.bat and the function is nothing but a parameter

Re: Functions Wanted

Posted: 04 Jun 2011 08:35
by Ed Dyreen
°
DOS Batch Forum
Discussion forum for all WinXP batch related topics.

PowerShell is NOT supported!

If you don't know the answer, it's better to remain silent.

MyBatch.CMD

Code: Select all

call MacroDEFs.CMD

for %%a in ( Param1 Param2 ) do !@MacroFunc!

call Functions.CMD ThisFunction TheseParameters

pause
exit /b

MacroDEFs.CMD

Code: Select all

set "@MacroFunc=echo.Param1=%%a_ &echo.Param2=%%b_"

Functions.CMD

Code: Select all

@rem :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@rem:: Suppress all 'direct call' related errors, all Windows OSes.
@rem :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@rem::(
@   if "%FullPathFile.Host.SCRIPT%"=="" 2>nul goto :ENDOLDOS ()
@   if "%FullPathFile.Host.SCRIPT%"=="" 2>nul goto  ENDOLDOS ()
@rem::)
@rem :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
::Call_FAST ( :SubRoutine star )
::
:: SJAB v140e beta
::
:: call @SubRoutine @star
::
::INPUT:
:: @SubRoutine    STRING    Required;    SubRoutine that will be called
:: @star    STRING    Optional;    Parameters that will be passed to @SubRoutine
::
:: WARNING (
::
::    -Needs to stay on top since;
::    ;. This in no SubRoutine !
::
::    -Cannot use :Debug () since;
::    ;. This in no SubRoutine !
::
::    Only if using Call_FAST:
::    -The '*' symbol must be noted as '^*' otherwise;
::    ;. for won't detect it as an argument !
::    ;. Can be avoided by loopin however we won't as this is too much of a performance hit !
::
::    -if %* contains "(" or ")" a normal if /i [""] == [""] () construction fails !
::    ;) This is solved by storing %* inside a variable first.
:: )
::
::(
   ::if /i ["!Debug.Sub!"] == [":WinWaitOpen"] set "Debug.Sub=:Call"
   ::set "Debug.Sub=:Call"
   ::(
      ::if /i ["!Debug.Sub!"] == [":Call"] echo. &echo.inside :Call star: &call echo.%*_
   ::)

   ::if /i ["!Debug.Sub!"] == [":Call"] echo.PROC PAR
   ::(
      ::if /i ["!Debug.Sub!"] == [":Call"] echo. Load called variables &pause
      ::(
         set /a Sub.Depth += 1
         ::
         set "Par.AsIs="

         set /a Par.C = -1

         call set star=%%*

         ::if /i ["!Debug.Sub!"] == [":Call"] echo. star=!star!_
         set "star=!star:?=#____CMD_ESC_SYM[Question]____#!"
         set "star=!star:^*=#____CMD_ESC_SYM[Star____]____#!"
         ::if /i ["!Debug.Sub!"] == [":Call"] echo. star=!star!_
         echo.>nul

         ::if /i ["!Debug.Sub!"] == [":Call"] for %%! in ( !star! ) do echo.token=%%!_
         echo.>nul

         for %%! in (

            !star!

         ) do (
            set /a Par.C += 1

            call set Par.!Par.C!=%%1

            call set Sub.!Sub.Depth!.Par.!Par.C!=%%1

            if !Par.C! gtr 0 if defined Par.AsIs (
               ::
               call set Par.AsIs=!Par.AsIs! %%1

            ) else    call set Par.AsIs=%%1

            ::if /i ["!Debug.Sub!"] == [":Call"] call echo. Par.!Par.C!=%%Par.!Par.C!%%_
            shift
         )

         set "Sub.!Sub.Depth!.Name=:!Par.0!"
         ::
         set "Sub.!Sub.Depth!.Par.AsIS=!Par.AsIs!"
         ::
         set "Sub.!Sub.Depth!.Par.C=!Par.C!"
      ::)

      ::if /i ["!Debug.Sub!"] == [":Call"] for %%! in ( Sub.Depth Par.AsIS Par.C Sub.!Sub.Depth!.Par.C Sub.!Sub.Depth!.Name Sub.!Sub.Depth!.Par.AsIS  ) do echo. %%!=!%%!!_
      ::if /i ["!Debug.Sub!"] == [":Call"] for /l %%! in ( 0, 1, !Par.C! ) do echo. Par.%%!=!Par.%%!!_
      ::if /i ["!Debug.Sub!"] == [":Call"] for /l %%! in ( 0, 1, !Sub.%Sub.Depth%.Par.C! ) do echo. Sub.!Sub.Depth!.Par.%%!=!Sub.%Sub.Depth%.Par.%%!!_
   ::)

   ::if /i ["!Debug.Sub!"] == [":Call"] echo.VALID
   ::(
      if not defined Par.0 echo. :( not defined Par.0 [ERROR] &pause &exit 1

      ::if /i ["!Debug.Sub!"] == [":Call"] for %%! in ( Sub.Depth Par.AsIS Par.C Sub.!Sub.Depth!.Par.C Sub.!Sub.Depth!.Name Sub.!Sub.Depth!.Par.AsIS  ) do echo. %%!=!%%!!_
      ::if /i ["!Debug.Sub!"] == [":Call"] for /l %%! in ( 0, 1, !Par.C! ) do echo. Par.%%!=!Par.%%!!_
      ::if /i ["!Debug.Sub!"] == [":Call"] for /l %%! in ( 0, 1, !Sub.%Sub.Depth%.Par.C! ) do echo. Sub.!Sub.Depth!.Par.%%!=!Sub.%Sub.Depth%.Par.%%!!_
   ::)

   ::if /i ["!Debug.Sub!"] == [":Call"] echo.Perform
   ::(
      ::if /i ["!Debug.Sub!"] == [":Call"] echo. call :!Par.0! !Par.AsIS! &pause
      ::(
         (
            ::if /i ["!Debug.Sub!"] == [":Call"]    set "Debug.Sub=:!Par.0!"
            call :!Par.0! !Par.AsIS!
            ::if /i ["!Debug.Sub!"] == [":!Par.0!"] set "Debug.Sub=%Debug.Sub%"
            echo.>nul
         )
      ::)

      ::if /i ["!Debug.Sub!"] == [":Call"] echo. Save called variables
      ::(
         for /l %%! in ( 0, 1, !Sub.%Sub.Depth%.Par.C! ) do set "Sub.!Sub.Depth!.Par.%%!="
         ::
         set "Sub.!Sub.Depth!.Par.C="

         set "Sub.!Sub.Depth!.Par.AsIS="
         ::
         set "Sub.!Sub.Depth!.Name="
         ::
         set /a Sub.Depth -= 1
      ::)

      ::if /i ["!Debug.Sub!"] == [":Call"] echo. Load caller variables
      ::(
         set "Par.AsIS=!Sub.%Sub.Depth%.Par.AsIS!"
         for /l %%! in ( 0, 1, !Par.C! ) do set "Par.%%!="
         ::
         set "Par.C=!Sub.%Sub.Depth%.Par.C!"

         :: -Set will set @ErrorLevel 1 if variable isn't found in environment therefor,
         :: ;) Using this method to preserve @ErrorLevel.
         ::(
            for /l %%! in ( 0, 1, !Par.C!
            ) do (
               if defined Sub.%Sub.Depth%.Par.%%! (
                  ::
                  set "Par.%%!=!Sub.%Sub.Depth%.Par.%%!!"

               ) else    if defined Par.%%! set "Par.%%!="
            )
         ::)

         ::if /i ["!Debug.Sub!"] == [":Call"] for %%! in ( Par.AsIS Par.C ) do echo. %%!=!%%!!_
         ::if /i ["!Debug.Sub!"] == [":Call"] for /l %%! in ( 0, 1, !Par.C! ) do echo. Par.%%!=!Par.%%!!_
      ::)
   ::)
::
::if /i ["!Debug.Sub!"] == [":Call"] echo.outside :Call () &pause
goto :eof ()
::)

Re: Functions Wanted

Posted: 04 Jun 2011 12:25
by Cleptography
allal wrote:
Cleptography wrote:I would like to build a script that consist of nothing more then functions that can be called from any other script.
Something like Script.bat :_label arguments.


Please cmd tell me where can you make a place for my custom functions

be fair and stop dreaming

what you really want and what is needed and what is useful in this case is to call directly the function followed by its parameter and unfortunately this doesn't exist in cmd,use vbscript wsc file or powershell and put your functions to be called in powershell profile which loaded with powershell interactive shell and script executing and this is the flexible way to do it i know so far

powershell example:

Code: Select all

  function test-space {
  Param
   (
      [parameter(Mandatory=$true)]
      [String]$path
   )
 
   if (($path | select-string " ") -ne $null) {return $true} else {return $false}



to call the function test-space simply write

Code: Select all

 ps > test-space "c:\program files"   


output

Code: Select all

ps > true



you may try this,this is not what you need and sorrily this is all what cmd can do for you

Code: Select all

@echo off

set "calledfunction=%~1"
set count=0
set "params="
for %%G in (%*) do ( call echo %%count%%|findstr /i /l "0" >nul 2>&1 && (
    set /a count+=1 ) || call set "params=%%params%% %%~G"
)

echo function to call is %calledfunction%
echo parameter is %params%
pause
call %calledfunction% %params%
goto :eof

:joinpath
set "_path=%~1"
set "_childpath=%~2"

REM set _path=%_path:"=%
REM set _childpath=%_childpath:"=%

if not %_path:~-1%==\ set "_path=%_path%\"
if %_childpath:~0,1%==\ set "_childpath=%_childpath:~1%"
set joined="%_path%%_childpath%"
endlocal & echo fullpath is %joined% &exit /b




script.bat :joinpath %windir% system32

the function exist in the script.bat and the function is nothing but a parameter


@allal
You apparently do not read any of this forum. You my friend have walked into the lions den with your smarta$$ remarks, you must earn the right to talk shi* in my house. I'm dreaming of a white Christmas, you "Timmy". All I am doing is putting together a library of funcs.... You know like the one found here http://www.dostips.com/DtCodeCmdLib.php You sir are a fizzle, now go take a walk down a short peer. :twisted:
@Ed get me my gat and my perl we is ganna go do a network drive by on this foo.

Re: Functions Wanted

Posted: 04 Jun 2011 13:08
by Ed Dyreen
Dostips has a lot of great functions, I'm going to bring my library online in the near future.
FILEDIR.SUB.CMD
MATH.SUB.CMD
STRING.SUB.CMD
REG.SUB.CMD
USER.SUB.CMD
NET.SUB.CMD
ERR.SUB.CMD
With mine &the dostips functions, there will be few problems we won't be able to handle.

But I'm working on them because functions are not fast enough for me, I'm converting them all to macro's. For example a typical dostips function that takes about 3secs to return a result, may only take about 0.270secs if converted to a macrofunction. But it's complex, I need more time :|

Re: Functions Wanted

Posted: 04 Jun 2011 14:46
by allal
what a waste of time
ok i give up