[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Using a batch modifier on a variable

Posted on 2010-04-08
6
Medium Priority
?
1,002 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:fvg
  • 4
6 Comments
 
LVL 16

Accepted Solution

by:
t0t0 earned 1400 total points
ID: 30105711
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:

   CALL BATCHFILE name

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

   :GetFileDate
   set %1=%~t2
   got :eof
0
 
LVL 16

Expert Comment

by:t0t0
ID: 30105749
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
0
 
LVL 16

Expert Comment

by:t0t0
ID: 30107034
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.
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
LVL 60

Assisted Solution

by:Bill Prew
Bill Prew earned 600 total points
ID: 30113610
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%

~bp
0
 
LVL 16

Expert Comment

by:t0t0
ID: 30115434
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!

0
 

Author Closing Comment

by:fvg
ID: 31712232
Thanks guys
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question