Link to home
Start Free TrialLog in
Avatar of fvg
fvgFlag for United States of America

asked on

Using a batch modifier on a variable

Hello,
I'm writing a batchfile that has to extract the date-modified from a specific file.
I known you can use the special modifier ~t to extract the date/time.
This works only when using one of the internal parameters %1-%9.
Is it possible to assign a value to such an internal parameter (for example %1).
With the SET command it is not working.

Example
set %1=NIS19_201002.csv
echo Modified date is %~t1
pause

Thanks
Frank
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or you could just as well use global variables and do this:

   @echo off
   call :GetFileDate NIS19_201002.csv
   echo %FileDate%
   exit /b

   :GetFileDate
   set FileDate=%~t1
   got :eof
using an external batch file as a function, you could do something similar but this time do not inlcude the ':' before the CALLed batch file (function):

First, create and save the following one-line batch file:

   GETFILEDATE.BAT

   @set %1=%~t2


Next, in your batch file, CALL the function by passing it a variable name and the filename whose dat and time you assigning to the variable. Like this:


   YOURBATCHFILE.BAT

   @echo off
   ::
   CALL getfiledate Fdate NIS19_201002.csv
   echo File's date is: %Fdate%
   ::
   :: etc...

This means you can re-use the same functions with other batch files. You could build up your own library of function like this however, you must remember to deplot any functions alongside any batch files that may depend on them if you intend to use your batch files on another computer.

The simplest way to do this is to copy the contents of the functions and append them to you your batch file under their respective labels (generated from their filenames, minus the extsion of course). Then scan you batch file for occurances of 'CALL :FunctionName' - in particular, the 'CALL :'-bit with the ':' (colon) and use string substitution to remove the colon like this:

   set line=%line:CALL :=CALL %

but this is theoretical stuff and might not even appeal to you at this stage.
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would too. I find it really odd that I went down the road I did having been influenced by the asker's description.

I can't believe I didn't cover the most obvious!

Avatar of fvg

ASKER

Thanks guys