Getting Return vars from Function Call
Posted: 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.
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