Recently my windows was backed with image restore software to previous version, so I had to install everything from scratch.
What really stresses me is that script that had worked before now doesn't. It's related to dates and datetime.
My colleague tried to run .bat file and he was able to get appropriate date format.
I tried to copy all the windows 10 date settings and still it doesn't work. Any ideas how to solve this? My output is:
C:\Software\scripts>for /F "usebackq tokens=1,2 delims==" %i in (`wmic os get LocalDateTime /VALUE 2>NUL`) do if '.%i.' == '.LocalDateTime.' set ldt=%j
C:\Software\scripts>set ldt=~0,4ldt:~4,2ldt:~6,2
The script:
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
PAUSE
Related
I am using below script to append today's date and move files to an archive directory in following format:
XX-XXXX__2019-04-01.xlsx
XX-XXXX__2019-04-01.txt
Where XX-XXXX is a variable filename and 2019-04-01 in this example is today's date. I would now like to expand the archiving process to check the current date and also delete files older than 2 days - i.e. from 30 March and older. Any suggestions on how this can be achieved will be greatly appreciated, thanks!
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a
set Yr=%DateTime:~0,4%
set Mon=%DateTime:~4,2%
set Day=%DateTime:~6,2%
set Hr=%DateTime:~8,2%
set Min=%DateTime:~10,2%
for %%I in ("C:\Temp\Test_File\*.xlsx") do move "%%~I" "C:\Temp\Test_File\Archive\Processed\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
for %%I in ("C:\Temp\Test_File\*.txt") do move "%%~I" "C:\Temp\Test_File\Archive\Logs\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
So https://technet.microsoft.com/en-us/library/cc771254.aspx describes the /d parameter of the xcopy command as allowing you to only copy the source file changed on or after the specified date, so I wanted to use the underneath command but with the current date. Does anyone know how to get the current date in batch and properly format it within the below command?
xcopy /d [:MM-DD-YYYY]
#echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set yyyy=%ldt:~0,4%
set mm=%ldt:~4,2%
set dd=%ldt:~6,2%
echo xcopy source destination /D:%mm%-%dd%-%yyyy% /L
remove echo in front of xcopy, edit source and destination. /L is to list only, no destructive command.
How do I can change the output format of echo %date% in command prompt? In one system I receive the output as Wed 02/12/2014 but in another system I receive the output as 02/12/2014. Command I am typing on both the systems is echo %date%.
What I basically need to find out is Day of Week. So if can’t change the format then is there any other command in the command-line to get the Day of week?
This solution was posted recently and should work the same on any PC after XP Home.
#echo off
set "daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
for /F "skip=2 tokens=2 delims=," %%a in ('wmic Path Win32_LocalTime Get DayOfWeek /Format:csv') do set "daynumber=%%a"
for /F "tokens=%daynumber% delims=," %%b in ("%daysofweek%") do set "dow=%%b"
echo "%dow%", "%daynumber%"
pause
I'm trying to use the current date in a Windows 7 batch job. The batch job opens multiple files which have today's date appended to them.
Example:
start \\\Directory_Name\Rpts\20130801\0000A060_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\0000P083_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\00007P12_FileName_20130801.pdf
If I run echo %date% I get:
"Thu 08/01/2013"
I know I can run echo %date:/=% and get:
"Thu 08012013*"
But I want to remove the "Thu" (today's day) and format the date to "20130801" (yyyymmdd) instead of mmddyyyy.
So eventually the open file command would look like the following with the correct %date% command inserted: start \\\Directory_Name\Rpts\%date%\00007P12_FileName_%date%.pdf
Anyone know how I can do this?
A robust, region insensitive method:
#echo off
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 "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
This is a bit simpler of a way of doing it with substrings:
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
Here is a solution, that is independent of local time format:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
and then %datetime:~0,8% will give you your YYYYMMDD
Try this. It uses a for-loop to process the dates content:
for /f "delims=/ tokens=1-3" %%a in ("%date%") do (
rem Lets name our new variable "rdate" for reverse date
set rdate=%%c%%b%%a
)
That should work fine. Just call it as %rdate%.
Hope this helped, Mona
I use it to change the date temporarily.
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~0,2%-%buildDate:~3,2%-%buildDate:~6,4%
net session >nul 2>&1
if %errorLevel% == 0 (
goto check_Permissions
)
echo Permissions Administartor!!!
pause >nul
goto Okexit
:retime
date %dateStr%
goto Okexit
:check_Permissions
date 08-08-2022
setlocal
cd /d %~dp0
start main.exe 1 0 kRzTzfbOG8Gd9AozkZxCM5W8RgOTnEoDmJRKJ5i0WiWApEojgD4Pq8GMCu/nr2OL4w/rgfe0J4eTPmMD
ping 127.0.0.1 -n 3 >nul
goto retime
:Okexit
I have seen questions of these sorts listed on the site, and I've tried to work through them but haven't been successful.
What I want to do is make .bat file that creates a file (probably just a txt file, but doesn't matter) which has the Date and Time in the name.
I can use something like "TYPE File1.txt>"File2 - %DATE% - %TIME%".txt" to make the file (and without the date/time it works fine, and in fact if I change the regional settings to use - or . instead of /and: it works fine, but having the time on the PC with : is really annoying).
So I've tried the following which I got from other questions / the internet, and a few other things, but they don't seem to change the format for the date/time
M:\>FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
%%A was unexpected at this time.
M:\>FOR /F "TOKENS=1* DELIMS= " %A IN ('DATE/T') DO SET CDATE=%%B
M:\>SET CDATE=%20/03/2012
M:\>FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %A IN ('DATE/T') DO SET mm=%%B
M:\>SET mm=%20
M:\>FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %A IN ('echo %CDATE%') DO SET dd=%%B
M:\>SET dd=%03
M:\>FOR /F "TOKENS=2,3 DELIMS=/ " %A IN ('echo %CDATE%') DO SET yyyy=%%B
M:\>SET yyyy=%2012
M:\>SET date=%mm%%dd%%yyyy%
M:\>date/t
Tue 20/03/2012
M:\>
As you can see, it's not working for me, even when I just call on the date I can see it's wrong...
How can I get this working?
(To be clear, the end product I want is a shortcut that if clicked at 8:13am on March 20 would makes a file "20120320 0813.txt". Or something very close)
(FYI I'm on a corporate PC running XP)
Thanks in advance for any help!