Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file day/month/year syntax?

I can't find a simple breakdown of the batch file syntax for extracting the current day/month/year.

I have the following syntax for declaring a variable used as a directory name;

set folder=%date:~10,4%%date:~7,2%%date:~4,2%

Can anyone shed some light (or post a link) on what the tilde, the double percentage means? I can't seem to fully decipher it from intuition alone.

like image 799
mtallyn Avatar asked Dec 28 '11 23:12

mtallyn


People also ask

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Which command is used to display date in dd mm yyyy format in MS DOS?

Type date without parameters to display the current date setting and a prompt for a new date. Press Enter to keep the same date. Provide a date as MM-DD-YY to set the system date to that date.

How do I run a batch file daily?

Run batch files on startupOpen File Explorer. Open the folder containing the batch file. Right-click the batch file and select the Copy option. Use the Windows key + R keyboard shortcut to open the Run command.

What is the format of datetime?

For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".


3 Answers

The double percentage means absolutely nothing. It is simply a result of having two variable expansions side-by-side such as:

echo %firstname%%lastname%
     \_________/\________/
      Two separate expansions.

The tilde gives you a substring. In your case, %date:~10,4% gives you four characters at offset ten of the date environment variable (the year in this case since the format is likely Thu 29/12/2011, with offsets starting at zero).

If you enter set /? at a Windows command pronpt, it will explain all the options for you, including the nifty trick of using negative offsets to extract from the end of the string.

However, you should keep in mind that the date environment variable format depends on the locale so this simplistic string extraction is unlikely to work across all international versions of Windows (this bit me a couple of years back).

A better solution is to use WMI to get the date components such as on Rob van der Woude's excellent scripting pages, copied here for completeness:

FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Dayˆ,Hourˆ,Minuteˆ,Monthˆ,Secondˆ,Year /Format:table') DO (
    IF NOT "%%~F"=="" (
        SET /A SortDate = 10000 * %%F + 100 * %%D + %%A
        SET /A SortTime = 10000 * %%B + 100 * %%C + %%E
        SET SortTime=0000000!SortTime!
        SET SortTime=!SortTime:~-6!
    )
)
like image 115
paxdiablo Avatar answered Nov 07 '22 08:11

paxdiablo


The ~ and %%s are splitting the string (if you just type date into command it shows you the full string).

%date:~10,4% means get next 4 characters from the 10th character along.

Also watch out for different PCs using different Regional Settings, as they change the order of those characters in the string.

like image 38
daniel Avatar answered Nov 07 '22 07:11

daniel


@paxdiablo beat me to it. Here's a link to a site that explains how it works, with plenty of examples.

like image 2
Aaron Avatar answered Nov 07 '22 08:11

Aaron