D:\> set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%"
D:\> mkdir SVN_BACKUP_DUMP_TEST\%today%
D:\> CD SVN_BACKUP_DUMP_TEST\%today%
when we are make bat file this command not work
Format of date of referenced environment variable DATE depends on region setting, i.e. which country and derived from country which date format is configured for the current account. So which string is assigned to environment variable today and if that string is valid for a directory or file name depends on account specific date format.
On my computer with German set as country for my account output of echo %DATE% is 13.11.2017. So the date format is dd.MM.yyyy with point as separator, no weekday and always two digits for date and month even for a date or month less than 10.
The command line
echo %DATE:~10,4%-%DATE:~7,2%-%DATE:~4,2%
produces on my machine with my account and my region settings the output:
-01-1.
That is definitely not the current date in format yyyy-MM-dd.
The solution is using following batch code:
#echo off
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "Today=%%I"
set "Today=%Today:~0,4%-%Today:~4,2%-%Today:~6,2%"
mkdir "SVN_BACKUP_DUMP_TEST\%Today%" 2>nul
cd "SVN_BACKUP_DUMP_TEST\%Today%"
This is a region independent solution. The batch code is completely explained in answer on
Why does %date% produce a different result in batch file executed as scheduled task?
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean? is one more question on which answer should be read to understand the string substitutions. And run in a command prompt window set /? to get displayed the help for command SET explaining string substitution, too.
Related
I have a batch file that looks like the following:
For /f "tokens=2-4 delims=/ " %a in ('date /t') do (set newdate=%c%a%b)
blat my_file_%newdate% -to test#email.com -f test_email.com
When I enter this two commands separately in a cmd window, it seems to work perfectly fine, but when placed into a batch file and ran manually, it does not work.
Open a command prompt window and run for /?. Output is the help for this command containing at top the information:
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different from %I.
Next I suggest to run set /? and read at least last page of output help listing the environment variable DATE.
If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up
in the list of variables displayed by SET. These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:
%CD% - expands to the current directory string.
%DATE% - expands to current date using same format as DATE command.
%TIME% - expands to current time using same format as TIME command.
%RANDOM% - expands to a random decimal number between 0 and 32767.
%ERRORLEVEL% - expands to the current ERRORLEVEL value
%CMDEXTVERSION% - expands to the current Command Processor Extensions
version number.
%CMDCMDLINE% - expands to the original command line that invoked the
Command Processor.
%HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number
on this machine.
So there is perhaps no need to run in a separate command process in background with cmd.exe /C the command line date /T as done by FOR with the posted command line, capture output of this command process, and process it line by line by FOR.
Well, the format of date output by date /T or on using %DATE% depends on Windows region setting. And it was not posted what is the date format on used machine with used account. But I suppose that following works also a very little bit faster.
for /F "tokens=2-4 delims=/ " %%a in ("%DATE%") do set "newdate=%%c%%a%%b"
I suppose using only string substitution works also on your machine for your account with a date format MM/dd/yyyy or dddd, MM/dd/yyyy:
set "newdate=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%"
This last solution is some microseconds faster than the others.
There is also a region independent solution as explained in detail for example by the answer on Why does %date% produce a different result in batch file executed as scheduled task? But the region independent solution using WMIC is much slower in comparison to the usage of the dynamic environment variable DATE.
Batch variable need to have %% instead of only one
It appears you are looking for the output to be YYMMDD, if so try this:
For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set newdate=%%c%%a%%b)
I run a command script that saves date to text file.
echo %date% > date.txt
Only problem this command saves the day in text file like so
Sun 13/08/2017
When it starts the service, I have a command to change date back
date < date.txt
I get an error
The system cannot accept the date entered.
It needs to show
13/08/2017
For this command to work
date < date.txt
I have someone gave me this command
for /F "tokens=2" %i in ('date /t') do echo %i > date.txt
it removes the day, it runs by itself.
but to run with a script it closes after it executes,
it doesnt seem to run with script.
I have other computers , I use this script and after windows update
It fixes issue; the windows update on other computer removes the day.
This computer had all windows update , but the day still displays
When you type <date> in cmd
The simple solution is using string substitution and write into the text file only the last 10 characters of the region dependent date string of built-in environment variable DATE by using this code:
>date.txt echo %DATE:~-10%
Avoid a space character before redirection operator > on writing a text with echo into a file as this space character is written as trailing space also into the file.
Sometimes it is unsafe to use echo %VariableText%>OutputFile.txt because of the variable text could end with a number in range 1 to 9 which would result in execution of the command line with 1> to 9> which means redirect handle 1 to 9 to the file instead of writing the number 1 to 9 to the file. In such cases it is advisable to specify the redirection to file at beginning of the command line before command echo as done on the command line above.
It would be also possible to use this code to save the date in format DD/MM/YYYY into the ANSI encoded text file date.txt.
#echo off
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /Value') do set "LocalDateTime=%%I"
>date.txt echo %LocalDateTime:~6,2%/%LocalDateTime:~4,2%/%LocalDateTime:~0,4%
This code has the advantage of being independent on which region is set for the current user because the date and time string output by WMIC has always the same format. Run in a command prompt window wmic OS GET LocalDateTime /Value to see the UTF-16 encoded output of WMIC processed by command FOR line be line. The disadvantage is that this code is slower.
On setting the date back with using command DATE the region dependent date format must be known.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
set /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?
Read also the Microsoft article about Using Command Redirection Operators.
Is there any possibility to create a new directory with current datetime when I use the Windows Command-line FTP?
for more clarification if I have scheduler to run this:
C:\windows\system32\ftp -s:ftpMyFiles.txt
And inside ftpMyFiles.txt I have:
open 127.0.0.1
user
pass
mkdir %DATE%
bye
here is the question Can I create A new directory with a datetime here (mkdir %DATE%)?
You have to generate the ftpMyFiles.txt dynamically like:
(
echo open 127.0.0.1
echo user
echo pass
echo mkdir %DATE%
echo bye
) > ftpMyFiles.txt
C:\windows\system32\ftp -s:ftpMyFiles.txt
Note that the value of %DATE% is locale-specific. So make sure you test your batch file on the same locale you are going to actually run it. Otherwise you may get unexpected results. For example on my (Czech) locale the DATE=po 13. 04. 2015
You can achieve the same easier and more reliably using WinSCP scripting. It supports:
Specifying the commands directly on its command-line
%TIMESTAMP% syntax.
winscp.com /ini=nul /command ^
"open ftp://user:pass#127.0.0.1/" ^
"mkdir %%TIMESTAMP#yyyymmdd%%" ^
"exit"
(I'm the author of WinSCP)
I prefer using ISO 8601 date formats (sometimes minus hyphens), as that makes ordering files by date easier (simply order alphabetically by folder name). Generally, I find ISO 8601 dates with hyphens easier to read than those without, but your requirements may vary.
Also, ISO 8601 avoids the silliness from across the pond where days and months are switched around - this confuses those of us who use international standards no end.
The ISO 8601 format used most often is YYYY-MM-DD or YYYYMMDD (e.g., 2016-02-24 or 20150224 for today's date).
Using wmic os get LocalDateTime, you can get non-locale-specific datetime from the operating system, and then convert it to ISO 8601 format, as demonstrated here:
#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 ldtime=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
set ldate=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
set ldatestraight=%ldt:~0,8%
echo Local datetime is [%ldtime%]
echo Local date is [%ldate%]
echo Local date no hyphens is [%ldatestraight%]
echo .
echo Raw datetime is [%ldt%]
pause
So, taking Martin Prikryl's sample code a step further, you could do something like this (for ISO 8601 dates with hyphens):
(
echo open 127.0.0.1
echo user
echo pass
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldate=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%
echo mkdir %ldate%
echo bye
) > ftpMyFiles.txt
C:\windows\system32\ftp -s:ftpMyFiles.txt
Kudos to Anerty and Jay for their answer to a similar question that helped me a little while back, here.
I want to run a simple batch file show one row message only, in the Valentine day 14-02-2014.
I need to write the day date (14-02-2014) inside the code, not do it as Task scheduler in windows. So, the batch will work every day with windows start up doing nothing, till that date it shows the message.
I just need the code for checking the date.
Thank you for your time.
The below code run in a .bat file will get the date in a local-agnostic way (the output format of %date% can change depending on a user's settings), and then use substrings to set today in the format you want (dd-mm-yyyy). Then the IF statement checks to see if the date is today, and echos Hello! if it is.
#ECHO OFF
FOR /F "skip=1" %%x IN ('wmic os get localdatetime') DO IF NOT DEFINED dmy_date SET dmy_date=%%x
SET today=%dmy_date:~6,2%-%dmy_date:~4,2%-%dmy_date:~0,4%
IF %today% == 14-02-2014 ECHO Hello!
:::For NT
echo off
set $Valentine=14-02-2014
set $TodayDate=%date:~0,2%-%date:~3,2%-%date:~6,4%
if %$TodayDate%==%$Valentine% echo "Happy Valentine Day My Love"
:::For XP
echo off
set $Valentine=14-02-2014
set $TodayDate=%date:~4,2%-%date:~7,2%-%date:~10,4%
if %$TodayDate%==%$Valentine% echo "Happy Valentine Day My Love"
I am need to log systems that do not have a specific file in a specific folder and have created the below batch which works fine. It will be called by the domain logon script (Clients are Windows XP in a 2003 AD domain):
IF EXIST "C:\Documents and Settings\%username%\Application Data\Microsoft\Outlook\test.OTM" (
goto END
) ELSE (
echo %DATE%_%TIME%_%COMPUTERNAME% >> %LOG1%
)
In addition to this, however, if the file is present I need to check that it has a specific modified date and if not then output it to a log file. So far I am stumped and would greatly appreciate any feedback/help on this. Thanks.
You can obtain information about the file's modification date and time in a batch script, but you'll need to remember these things:
it comes as a combination of date and time;
it's locale specific;
it's a string.
That means that before comparing you'll need to cut off the time part, for which you'll need to take into account the display format as specified in the system's regional settings. And because it's a string, you'll probably be only able to check whether it is a specific date, but not whether it belongs to a specific period.
And here's how you can have it implemented:
SET filename="C:\Documents and Settings\%username%\Application Data\Microsoft\Outlook\test.OTM"
IF NOT EXIST %filename% GOTO log
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
IF "%filedatetime:~0,-6%" == "%checkdate%" GOTO END
:log
ECHO %DATE%_%TIME%_%COMPUTERNAME% >> %LOG1%
On my system %%~tf would return the date and time formatted as dd.MM.yyyy hh:mm. So the %filedatetime:~0,-6% part follows that format and cuts off the time part accordingly. You may need to change the expression slightly to fit your case.
One last thing is, there's a predefined variable called USERPROFILE. It points to the active user's 'home' folder, C:\Documents and Settings\username. So you can reduce the path string to this: "%USERPROFILE%\Application Data\Microsoft\Outlook\test.OTM".
If the date is relative to today (e.g. file updated within the last 7 days) you can use the "forfiles" command which has date calculations built in.
For example: to list all files that have been modified in the last 2 days:
forfiles /D -2 /C "cmd /c ECHO file selected...#path, dated #fdate"