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!
Related
I am new to this and have created a batch file that creates a current dated folder with a user entered title.
Unfortunately it does not create the year for the 20th of the month.
e.g. On the 19th it will create "140119 - Test" but on the 20th "0120 - Test"
Any ideas what is causing it?
It must be the Delims that is causing the issue but I don't know how to get around it.
#echo off
#REM Setups %date variable
#REM First parses day, month, and year into dd , mm, yyyy formats and then combines to be YYMMDD by removing the 20
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET dd=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/20 " %%A IN ('echo %CDATE%') DO SET yy=%%B
SET date=%yy%%mm%%dd%
call :inputbox "Please enter the folder name (Excl. Date):" "Standard Date folder"
exit /b
:InputBox
set input=
set heading=%~2
set message=%~1
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set input=%%a
if "%input%"=="" (exit /b)
mkdir "%date% - %input%"
exit /b
Your fundamental issue is that the delims= phrase estalishes a set of delimiters, not a delimiter string. Any character between the = and " becomes a delimiter.
#ECHO OFF
SETLOCAL
REM Setups %date variable
REM First parses day, month, and year into dd , mm, yyyy formats and then combines to be YYMMDD by removing the 20
FOR /F "TOKENS=1-4 DELIMS=/ " %%A IN ("%date%") DO ECHO A=+%%A+ B=+%%B+ C=+%%C+ D=+%%D+
FOR /F "TOKENS=1-4 DELIMS=/ " %%A IN ("%date%") DO SET dd=%%B&SET mm=%%C&SET yyyy=%%D
SET yy=%yyyy:~-2%
SET xdate=%yy%%mm%%dd%
ECHO xdate=%xdate%
GOTO :EOF
#echo off turns off command-echoing. Having executed that instruction, there is no requirement for further #instruction commands. # simply turns off instruction-echoing for that one instruction.
You don't tell us what your date-format is, but I conclude that it's dayname dd/mm/yyyy. This is fundamental - personally, I use dd/mm/yyyy but many Overflowers would use dayname mm-dd-yyyy. Always a problem area - don't let's get started on time.
%date% is a magic variable, set by the system to potatoes according to the user's selected format-settings. Similarly, %time% is set to carrots. If you set a user-variable of the same name, then the user-variable overrides the system-set variable.
In the above, I've used SET yy=%yyyy:~-2%. To substring in batch, you need SET var=%bigstringname:~start,length% where length is optional and if missing means the remainder of the string afterstart.Startmay be 0 or positive meaningthis number of characters after the start of the string (or character-position, starting a position '0'). If negative, it means this number of characters from the end of the string.
the result depends on your systems locale:
C:\Users\User\TEST>date /t
20/01/2014
C:\Users\User\TEST>for /f "tokens=1-3delims=/ " %a in ('date /t') do #echo(%c%b%a
20140120
C:\Users\User\TEST>for /f "tokens=1-3delims=/ " %a in ('date /t') do #set "mydate=%c%b%a"
C:\Users\User\TEST>echo %mydate%
20140120
C:\Users\User\TEST>set "mydate=%mydate:~2%"
C:\Users\User\TEST>echo %mydate%
140120
Please note the trailing space : 20/01/2014_. You should not use system variables for local variables, ex date.
if this is an option for you then this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher, using WMIC.
#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 "datestamp=%YY%%MM%%DD%"
I'm trying to figure this out need some help. I need to make a program that copy's my Log file and rename with date and time to different directory. Prior to loading the program not sure how to do it if any help would be great.
Here's a batch file that will copy all log files to an archive folder appending the date to each:
#Echo Off
#For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do #(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A
)
#For %%a in ("*.log") do copy %%a "archive\%%~na_%All%.LOG"
I use the following code to create a new folder that is named with todays date:
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
mkdir c:\%date:/=%
Now the format is as follows:
20130619
How do I change the format to?:
2013_06_19
Thank you
%date% depends on your computer settings, and locale. Here is a reliable way to get a date and time stamp. Win XP pro and above.
If you need to use your batch file on unknown machines then this is worth using.
:: time and date stamp YYYYMMDD, HHMMSS and YYYY-MM-DD_HH-MM-SS
#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%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"
Do this:
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1="%%k"_"%%i"_"%%j"
mkdir %yyyymmdd1%
or simply
SET Today=%Date:~10,4%_%Date:~7,2%_%Date:~4,2%
echo %today%
outputs
2013_06_19
Press any key to continue . . .
Then you could easily use the variable today for directory creation e.g.:
mkdir %today%
EDIT: YYYY_MM_DD format
I can't seem to understand how batch files add yyyy/mo/dd/hh/mm/ss to the beginning of filenames. (Using Windows 7) Accuracy to the second is important.
It doesn't actually have to be a batch file, it just has to be a small program which can be executed by Directory Monitor whenever I add files to a folder: http://brutaldev.com/page/Directory-Monitor.aspx
I only imagine that a batch file would be the simplest and most efficient approach, but any other suggestions are welcome.
I work with many sequentially numbered files with overlapping filenames and I need a quick way to rename them whenever I add them to a folder such that there will never be any file with the same name yet they will still remain in sequential order. This is how I thought of adding the current date and time to the beginning of the filename and why seconds are important, since I can easily add multiple sets to a folder in under a minute but certainly not under a second. It would be ideal if the batch file could ignore file extensions and simply add the current date/time to the beginning of any file added to the folder.
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
#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 "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%"
:: this line will rename the files in the current folder which haven't already
:: been renamed by checking for the fullstamp format at the start of the line
:: but it will skip this batch file
for /f "delims=" %%a in ('dir /b /a-d ^|findstr /v "^[0-9]*-[0-9]*-[0-9]*_[0-9]*-[0-9]*-[0-9]*" ') do if /i not "%%a"=="%~nx0" ren "%%a" "%fullstamp% - %%a"
pause
#ECHO off
SETLOCAL
IF [%1] NEQ [] goto s_start
:: Author - Simon Sheppard, July 2003
:: Tested for Windows NT, 2K, XP
ECHO STAMPME.cmd
ECHO REName a file with the DATE/Time
ECHO.
ECHO SYNTAX
ECHO STAMPME TestFile.txt
ECHO.
ECHO STAMPME "Test File.txt"
ECHO.
ECHO STAMPME "c:\docs\Test File.txt"
ECHO.
ECHO In a batch file use CALL STAMPME ...
:: To change the filename format just change around the last line below
GOTO :eof
:s_start
SET _file=%~n1%
SET _pathname=%~f1%
SET _ext=%~x1%
::Get the date
:: note ISO 8601 date format would require 4 digit YYYY Year)
FOR /f "tokens=6-8 delims=/ " %%G IN ('NET TIME \\%computername%') DO (
SET _mm=%%G
SET _dd=%%H
SET _yy=%%I
)
:: Get the time
FOR /f "tokens=2-4 delims=:." %%G IN ('cmd /c "time<nul"') DO (
SET _hr=%%G
SET _min=%%H
SET _sec=%%I
GOTO :done
)
:done
ECHO Today is Year: [%_yy%] Month: [%_mm%] Day: [%_dd%]
ECHO The time is: [%_hr%]:[%_min%]:[%_sec%]
REN "%_pathname%" "%_hr%-%_min%-%_sec%#%_file%%_ext%"
This seems to work for me
I'd prefer solutions, that are not dependent to local settings (wmic gives always the same format):
#echo off
setlocal EnableDelayedExpansion
for %%a in (a.*) do (
for /f %%i in ( 'wmic os get localdatetime /value ^|find "Local"' ) do set %%i
set ldt=!LocalDateTime:~0,4!-!LocalDateTime:~4,2!-!LocalDateTime:~6,2!-!LocalDateTime:~8,2!-!LocalDateTime:~10,2!-!LocalDateTime:~12,2!-!LocalDateTime:~15,3!
echo seconds ### ren %%a !LocalDateTime:~0,14!%%a
echo milliseconds ### ren %%a !LocalDateTime:~0,18!%%a
echo with separator ### ren %%a !ldt!-%%a
)
I've been tasked with updating an existing windows scheduled task. The task simply calls a windows console based FTP command using an existing script text file using the -s:ftpScript.txt syntax.
The problem is that the filename has changed and will now be based on the current date such as filename20110101.txt.
How can I get the -s:ftpScript.txt to understand that there is a dynamic filename now required? Do I have to recreated the "ftpScript.txt" file dynamically each time the task fires to then include a new static file containing the current date based filename?
I've now since followed my initial suggestion and it works perfectly. I've posted the following below in case it helps anyone else;
echo Generating New FTP Request
set request=Request.txt
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET month=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET day=%%B
FOR /F "TOKENS=3,4 DELIMS=/ " %%A IN ('DATE /T') DO SET year=%%B
set /A day=%day%-1
set yesterday=%year%%month%%day%
set file=<filename>
(
echo open <server>
echo <pass>
echo get %file%%yesterday% %file%%yesterday%
) > %request%
ftp -i -s:%request%