I have a batch file where I have a variable which prints date in YYYYMMDD - 20160403
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%
echo Local date is %ldt%
then I am downloading a file which has the date a the end of the url
curl "http://www.example.com/cgi-bin/abc.xyz/%ldt%.txt" -o file-name-%ldt%.txt
where %ldt% has the date for eg: 20160403
but now I am not able to print the date. Any suggesstions ?
%date% is an internal windows variable. It is not wise to overwrite it. Use another variable name (for example %myDate%).
By the way: you can restore the original windows variable by (no joke) deleting it: set "date="
Related
I've created a timestamp variable in a batch script like so...
set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
There is an issue though when the HH is only a single digit I get...
YYYY-MM-DD- 2-MM-SS
instead of
YYYY-MM-DD-02-MM-SS
How do I consistently generate the timestamp without spaces?
set "timestamp=%timestamp: =0%"
replaces spaces with zeroes.
See set /? from the prompt for documentation.
Don't use locale/user settings dependent date time variables but wmic:
#Echo off
For /f "delims=." %%A in (
'wmic os get LocalDateTime^|findstr ^^20'
) Do Set DT=%%A
Set "TIMESTAMP=%DT:~0,4%-%DT:~4,2%-%DT:~6,2%-%DT:~8,2%-%DT:~10,2%-%DT:~12,2%"
Set TimeStamp
Sample output:
> SO_45465890.cmd
TIMESTAMP=2017-08-02-18-42-07
Or use PowerShell and let it do the formatting:
#Echo off
For /f %%A in ('powershell -NoP -C "get-date -f \"yyyy-MM-dd-HH-mm-ss\""') Do Set TimeStamp=%%A
Set TimeStamp
I want to create a batch file which when clicked will create a folder with the name 12012016.
I tried with the command
mkdir "E:\Meru\Work\Trace Reports\%date:~6,4%%date:~3,2%%date:~0,2%
But its creates with the name 20160112.
Please help
This question implies that you have not tried to understand what is going on with this command...
Split in part: mkdir will create a directory with the name you have given.
The name you have given is build together using a fixxed string, you decided to use E:\Meru\Work\Trace Reports\ and three substrings from the system variable %date%.
Substring in batch works like this: %variable_name:~last character NOT to use,number of characters you need%. In your case it takes the year first, then the month and at last the day. You would just simply change the parts from %date:~6,4%%date:~3,2%%date:~0,2% to %date:~3,2%%date:~0,2%%date:~6,4%.
Notice!
The variable %date% has a different value based on the system settings for the time format. An alternative is the command wmic os get localdatetime which I covered in another answer here that will always have the same output format no matter what the settings are.
If you have read this ==> Windows batch file redirect output to logfile with date/time
You can be able to do like this one :
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
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 stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
pause
echo datestamp: "%datestamp%"
pause
echo timestamp: "%timestamp%"
pause
set MyDateVar=%MM%%DD%%YYYY%
echo My desired Variable Date to use is : %MyDateVar%
pause
mkdir "E:\Meru\Work\Trace Reports\%MyDateVar%"
pause
Because several hours have now elapsed:
mkdir "E:\Meru\Work\Trace Reports\%date:~0,2%%date:~3,2%%date:~6,4%"
I am trying to write a windows batch script that allows me to duplicate a directory [foldername] with a new file name with date appended to it [foldername.date01]. But it should also check if a duplicate has already been made for current date and create a new one like [foldername.date02] and so on..
xcopy /s/e c:\source d:\target
allows me to copy but I dont know how to retrieve date and append it.
This will backup your directory structure to another drive with all copied folders named SourceFolderName_CurrentDate_BackupTime. Let me know, if any errors:
#ECHO OFF
SET "source=C:\Source" & SET "target=D:\Target"
FOR /F %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET DT=%DTS:~0,8%_%DTS:~8,6%
FOR /F "tokens=*" %%G in ('DIR /b /a:d "%source%"') DO (
XCOPY /s/e/y/b/q/r/k/i "%source%\%%G" "%target%\%%G_%DT%" )
EXIT /B
Note, wmic is supported starting from WinXP Pro, and the above wmic command outputs OS locale independent time stamp. You can adjust DT variable format in its SET statement as you wish.
I every month a get a folder named using the year and month (which am not allowed to change). For instance DB201401 (DB = database, 2014 = the year and 01 =the month). I was wondering if there was a way to make a batch file, which finds the correct folder of the same year and month based on windows OS Date?
or maybe just find the folder by the month, ignoring the "DB2014"?
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "curdt=%%a"
set "Year=%curdt:~0,4%"
set "Month=%curdt:~4,2%"
echo DB%YEAR%%MONTH%
Change the echo to whatever you want to do with the foldername.
This depends on WMI - so it won't work on very very old Windows versions without WMI.
Wouldn't that just be the latest "DB2*" directory date?
for /f "delims=" %%a in ('dir /b /ad DB2*') do set currdir=%%a&goto done
:done
echo %currdir%
but that assumes that the only DB2* directories are of format DBccyymm
I'm trying to setup 7zip for automated backups but I'm having trouble with output file names.
I tried using the %date% command but it just made 2 directories within my backup.
C:\Users\Desktop\Sun 11\07\2010.7z
How can I make it just log the day and month?
C:\Users\Desktop\Sun 11-07-2010.7z
Try
7z a %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.7z *.* for (YYYY-MM-DD)
or
7z a %DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%.7z *.* for (DD-MM-YYYY)
(*.* is the mask for the files to back up)
Are you using a bat-file? Look here http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/
You can use WMI to get your date details in a specific format. The problem with the output from date (and the %date% environment variable) is that it's very locale-specific.
If you execute:
wmic path win32_localtime get day^,month^,year^ /format:csv
you will see the output you need to process.
The following script will get you the yyyy-mm-dd format that you need (using the day of the week as the primary sort key is not a good idea):
#echo off
for /f "skip=2, tokens=2-4" delims=," %%a in ('wmic path win32_localtime get day^,month^,year^ /format:csv') do (
set /a ymd = 10000 * %%c + 100 * %%b + %%a
)
set ymd=%ymd:~0,4%-%ymd:~4,2%%ymd:~6,2%
echo %ymd%