d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help With Batch Script > Filter Numbers From Variable
Add Reply New Topic New Poll
Member
Posts: 4,612
Joined: Jul 23 2007
Gold: 60.00
Oct 5 2018 02:13am
Hello and thank you for reading.

I want to write short script to upate files and creating a backup before overwriting.
My problem now is to rename the file with a timestamp before i overwrite is with the new one.

At the moment i'm getting the timestamp this way:

Code
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%

set stamp=%YYYY%%MM%%DD%


Unfortnuatly this doesnt work on every PC - i guess the reason is that wmic is not supported in every Windows Versions.
(I want to use the script on WIN NT, XP, 7, 10)

I was thinking about the %date% variable but also here the content is different from version to version - but the date numbers are inside.

Now my question is: is it possible to just filter out the numbers in the %date% variable

Here i got 2 different date contents from Win10 and Win7:

Win 10: 05.10.2018
Win 7: Fri 10/05/2018

The Win 10 content would work with the rename step: filename_%date%.exe ->filename_05.10.2018.exe

With the Win7 i got problems due the "/" symbols.

If i can get in my example just 05102018 out of the %date% that would be great - is it possible?

This post was edited by Phiro on Oct 5 2018 02:21am
Member
Posts: 5,348
Joined: Sep 15 2017
Gold: Locked
Oct 5 2018 04:38am
Hey, i found this out on google. give it a try
your timestamp is supposed to be in the "fullstamp" variable

lmk if it worked as expected :)

Code
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "fullstamp=%YYYY%%MM%%DD%%HH%%Min%%Sec%"
echo fullstamp: "%fullstamp%"
pause
Member
Posts: 4,612
Joined: Jul 23 2007
Gold: 60.00
Oct 8 2018 12:54am
Quote (minuteMed95 @ 5 Oct 2018 11:38)
Hey, i found this out on google. give it a try
your timestamp is supposed to be in the "fullstamp" variable

lmk if it worked as expected :)

Code
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "fullstamp=%YYYY%%MM%%DD%%HH%%Min%%Sec%"
echo fullstamp: "%fullstamp%"
pause




hello thank you for your answer but i'm looking for another solution without the wmic command - since wmic is not possible on Win NT
Member
Posts: 2,903
Joined: Aug 25 2009
Gold: 170.00
Oct 9 2018 02:29am
use "ver" instead of wmic ?


https://en.wikipedia.org/wiki/Ver_(command)

Here is a list with given replies compared to the OS ;) Works from windows 1.0 to 10 =)
Member
Posts: 3,028
Joined: Mar 23 2016
Gold: 7,568.50
Oct 13 2018 03:10am
You can format %date% yes. More details here: https://stackoverflow.com/questions/1192476/format-date-and-time-in-a-windows-batch-script

To get it formatted as 13102018:

Code
%date:~0,2%%date:~3,2%%date:~-4%


I have to say though, I didn't realize how crap and inconsistent the time handling is in batch.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll