Using a batch modifier on a variable

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.

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Think about what you're trying to do....

   %1 equals NOTHING

so, something like:

   SET %1=nis19_201002.cvs

is no different to:

   SET =nis19_201002.cvs

which is nonsense as there is no Lvalue to assign anything to.

Nevertheless, suppose you intentionally passed the 'name of a varialbe' as a command line parameter, %1 would pick this uu. You can then do something like this:


   set %1=john
   echo %name%

or even:

   echo !%1!

(if you have enabled delayed expansion)

Now to answer your question.

Passing a filename as a prarmeter would enable %1 to expand it#s size, date and time etc...

As a single batch file you could do something like this:

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

   set %1=%~t2
   got :eof

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day 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

   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:


   @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:


   @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.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Bill PrewCommented:
Here's how I would do it, using the same variable modifiers that are available on FOR loop variables.  Let me know if you have questions:

for %%A in ("NIS19_201002.csv") do set ModDate=%%~tA
echo Modified date is %ModDate%

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!

fvgAuthor Commented:
Thanks guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.