Getting Return vars from Function Call

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

Getting Return vars from Function Call

#1 Post by RElliott63 » 27 Mar 2009 14:53

I'm trying to build a File Info function because I'm always having to come up with a piece of info here and there and I'm tired of rebuilding every time I do. So, to combat that, I started the below script, but am running into issues surrounding the DelayedExpansion.

Any input will be greatly appreciated!

The Jist? I'm trying to populate a series of vars:

file.NAME
file.PATH
file.SIZE
file.DATE
file.TIME

I'm passing the path/filename.ext to the function and wanting these vars available AFTER the function call. I want this to be an external function. I just can't seem to get by the ENDLOCAL and populate the Env Vars as needed.

Example: Call GetFileInfo "\Dev\Working\Test.Dat"

I know the "ENDLOCAL" command below is wrong, I just need to figure out where to go with it.

Code: Select all

@Echo Off

 SETLOCAL ENABLEEXTENSIONS
 SETLOCAL ENABLEDELAYEDEXPANSION

 Set "FN=%1"
 Set "Display=%2"

:: Need Help?
 Set "Help="
 If /I [%FN%] Equ [/H] ( Set "Help=Y" )
 If /I [%FN%] Equ [/?] ( Set "Help=Y" )
 If Defined Help (
    Call :Syntax
    Goto Exit
 )

:: Still not getting it?
 Set "Help="
 If /I [%FN%] Equ [] ( Set "Help=Y" )
 If NOT Exist %FN%   ( Set "Help=Y" )
 If Defined Help (
    Set "Error=File name was Blank or Not Found"
    Call :Syntax
    Goto Exit
 )

:: Got something to do, get after it!
 For /F %%F in ('Dir /B %FN%') Do (

     Set "file.NAME=%%~nxF"
     Set "file.PATH=%%~pF"
     Set "file.SIZE=%%~zF"

     Set "d=%%~tF"
     Set "dD=!d:/=!"
     Set "dT=!d::=!"
     Set "dH=!dT:~11,2!"  -- Hours
     Set "dM=!dT:~13,2!"  -- Minutes
     Set "ap=!dT:~16,2!"  -- AM / PM

     If /I [!ap!] EQU [PM] (
        Set /A dH=dH+12
     )

     Set "file.DATE=!dD:~0,8%!
     Set "file.TIME=!dH!!dM!"

     If Defined Display (
       Cls
       Echo ==========================
       Echo     ** GetFileInfo **
       Echo ==========================
       Echo File:  !file.NAME!
       Echo Path:  !file.PATH!
       Echo Size:  !file.SIZE!
       Echo Date:  !file.DATE!
       Echo Time:  !file.TIME!
       Echo ==========================
       Echo;
     )
     
)

:Exit

( EndLocal & Rem Return Variables
  For /D %%v in (file.NAME file.PATH file.SIZE file.DATE file.TIME) do (
     Set "%%v=%%v"
  )
)
 
Goto:EOF

:Syntax
Cls
If Defined Error (
   Echo;
   Echo *** ERROR ***
   Echo %Error%
   Set "Error="
   Echo;
)
Echo =================================================
Echo Syntax:
Echo;
Echo GetFileInfo [Drive:\path\FileName.ext] [D]isplay
Echo;
Echo Returns Environment Variables:
Echo    file.NAME
Echo    file.PATH
Echo    file.SIZE
Echo    file.DATE
Echo    file.TIME
Echo =================================================
Goto:EOF

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 27 Mar 2009 15:41

I think this works:

Code: Select all

EndLocal &&set "file.NAME=%file.NAME%"&&set "file.PATH=%file.PATH%"&&set "file.SIZE=%file.SIZE%"&&set "file.DATE=%file.DATE%"&&set "file.TIME=%file.TIME%"


If that's the functionality you want, I have to admit I'm not sure how to get it into a for loop so it's easier to write -- though I've heard it can be done.

In your for loop, the set would have to be:

set "%%v=!%%v!"

which wouldn't work after the endlocal because of the delayedexpansion. Same thing (I think ) with this:

call set "%%v=%%%%v%%" (untested--those pesky %'s)

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#3 Post by RElliott63 » 02 Apr 2009 11:26

OK.. this still isn't working. Any other suggestions are appreciated...

This is the exit code I have validated that the file.* vars are populated via the set commands below, but when it exists the script, these values aren't defined.

Code: Select all

( If Not Defined Help (
     EndLocal ^
     && set "file.NAME=%file.NAME%" ^
     && set "file.PATH=%file.PATH%" ^
     && set "file.SIZE=%file.SIZE%" ^
     && set "file.DATE=%file.DATE%" ^
     && set "file.TIME=%file.TIME%" ^
  )
)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 06 Apr 2009 11:32

Try this:

Code: Select all

(
   If Not Defined Help (
     EndLocal
     set "file.NAME=%file.NAME%"
     set "file.PATH=%file.PATH%"
     set "file.SIZE=%file.SIZE%"
     set "file.DATE=%file.DATE%"
     set "file.TIME=%file.TIME%"
  )
)

Post Reply