bat file that gets current date in MMDDYY format and saves it in a variable for later use in the file - variables

i am making a bat file that does a few tasks for me but i need to get the current date in MMDDYY format.
I know this will return the 4 digit year but how can i make it show 12 instead of 2012?
for /f "tokens=2-4 delims=/ " %%a in ('date /T') do set year=%%c

set MMDDYY=%DATE:~4,2%%DATE:~7,2%%DATE:~12,2%
check the return from echo %DATE% to see if you need to make adjustment for locale

Related

Assigning a variable to the output of a findstr/find combination

I have a log file, that prefixes all lines in it with the date and time. For instance:
2015-02-04 16:11 Error Message: Important Bit Useless Information.
I wish to write a batchfile that when run picks out all the lines from the last hour and then counts how many of these lines contain "Important Bit" and then output this count if it's greater than a given value.
The part I am having difficulty with is basically the main part. I have set up the batchfile, it finds the date, finds the last ten minute period. I cannot then get the file to do the above.
My current command for this is as below, where date and now are assigned to the current date and time:
set count="findstr /G:“%date% %now%” “C:\Documents\File.txt” | find /C "Important Bit""
If %count% geq 5 echo Found %count% lines >> C:\Documents\Output.txt
When I run this, as far as I can tell, nothing happens. I've tried using a FOR command as well, but this still wouldn't work.
Any suggestions are greatly appreciated!
Thanks
Use something like the following:
set /a COUNT=0
for /f %%i in ('findstr /i /c:"Important Bit" test.txt') do (
set /a COUNT=COUNT + 1
)
echo %COUNT%

Windows - Batch script- How to Append Current Date and Time to .txt name

I know this question was duplicated many time and I tried almost all of it but I still unable to append date and time to a .txt . Sorry if this question make you mad or anything.
Here is my code:
cd C:\Users\310152922\Desktop\Mohit Task\Lumi_FTP
java Lumi_FTP "C:\Users\310152922\Desktop\Mohit Task\Lumi_FTP\lumi_ftp_settings.ini" >"C:\test\Scriptlogs\log_DP.txt"
exit;
The question is is how can I append date and time to a "name" of a .txt
Example (expecting output):
log_DP_DATE_ TIME.txt (log_DP_03112014_1146.txt)
Thank you very much for viewing, comment and answer.
p.s I m newbie for programming languages
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
The variable is in the log file name below:
#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%"
cd /d "C:\Users\310152922\Desktop\Mohit Task\Lumi_FTP"
java Lumi_FTP "C:\Users\310152922\Desktop\Mohit Task\Lumi_FTP\lumi_ftp_settings.ini" >"C:\test\Scriptlogs\log_DP_%fullstamp%.txt"

Batch file - date variables and moving files with variable date file name from one folder to another

I need some assistance, please. I'm trying to create a batch file to move files from one folder to another. The file name will have variable yyyy-mm format plus additional data before or after the date. The batch will need to move the file to a server directory with the same mmmm-yy folder name.
I've come up with the code below, but it doesn't quite work.
A "Missing Operand" error is returned.
The new directory is created but the files are not moving from the old folder to the new one.
My code:
#echo off
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
SET /A MONTH=%%D
SET /A YEAR=%%F
)
:: Set month to last month
set /a MONTH=%MONTH%-1
:: If month equals zero, reset to 12
if %MONTH%==0 set MONTH=12
:: If month < 10, fill with zero
if %MONTH% LSS 10 set MONTH=0%MONTH%
:: If month = 12, subtract one year
if %MONTH%==12 set /a YEAR=%YEAR%-1
SET FILEDATE=%YEAR%-%MONTH%
SET FOLDER2=E:\ARCHIVE\%FILEDATE%
MKDIR %FOLDER2%
:: trying to recreate the format MOVE C:\FOLDER1\\*2013-07*.* E:\FOLDER2\2013-07 which does work
MOVE C:\FOLDER1\\*%FILEDATE%*.* %FOLDER2%
:END
EXIT
EDIT: Both responders below really helped. I tried to vote them up, but I guess my reputation is not good. Mother was right - guard your repuation! It will get you far. :)
You should better use %date%, not wmic, but you can try:
for /f "skip=1 delims=" %%a in ('WMIC Path Win32_LocalTime Get Month^,Year /Format:table') do for /f "tokens=1,2" %%b in ("%%a") DO SET "month=%%b" &SET "year=%%c"
Just a minor edit - it looks like it should work - as your explanation says the folder is being created just fine.
MOVE "C:\FOLDER1\*%FILEDATE%*" "%FOLDER2%"

windows command line giving error on calculation

I found this nice little tidbit of code here: https://stackoverflow.com/a/5262637/2128987
#echo off
set starttime=%TIME%
set startcsec=%STARTTIME:~9,2%
set startsecs=%STARTTIME:~6,2%
set startmins=%STARTTIME:~3,2%
set starthour=%STARTTIME:~0,2%
set /a starttime=(%starthour%*60*60*100)+(%startmins%*60*100)+(%startsecs%*100)+(%startcsec%)
:TimeThis
robocopy /e /NFL /NDL /NJH /NJS /nc /ns /np folder%rndfolder% %drvltr%:\f%dirnew%\
set endtime=%time%
set endcsec=%endTIME:~9,2%
set endsecs=%endTIME:~6,2%
set endmins=%endTIME:~3,2%
set endhour=%endTIME:~0,2%
if %endhour% LSS %starthour% set /a endhour+=24
set /a endtime=(%endhour%*60*60*100)+(%endmins%*60*100)+(%endsecs%*100)+(%endcsec%)
set /a timetaken= ( %endtime% - %starttime% )
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%
echo.
echo Took: %timetaken% sec.
As a standalone program it works great. I am using it with a robocopy command basically to determine how long it takes to write a file.
I add one extra variable in it because I want to keep the raw seconds for calculation purposes. So I add the extra line set timeraw=%timetaken%:
set /a timetaken= ( %endtime% - %starttime% )
***set timeraw=%timetaken%***
set /a timetakens= %timetaken% / 100
set timetaken=%timetakens%.%timetaken:~-2%
My batch file also uses setlocal enabledelayedexpansion
Well sometimes it does not properly calculate the "starttime" or "endtime". It's keeps it as the raw time in 08:30:22.35 type format and results in the error:
Invalid number. Numeric constants are either decimal (17),hexadecima (0x11), or octal (021)
Well obviously because it contains non-numeric characters like the : character.
My batch file goes in a continuous loop forever as I am using it to read, write, delete files and folders for a specific torture test condition.
Any idea why it would intermittently not calculate the starttime or endtime variables?
edit:
I made some changes to my overall script. I no longer need enabledelayedexpansion, cleaned up some if then statements, and simplified code a little. But I still occasionally get it where the starttime or endtime variables remain as the raw time format of HH:MM:SS.CS and causes error in calculation.
Old question, but there are probably blocks of parentheses and when you change a variable within parentheses then you need to use delayed expansion.
Run this and examine the differences.
#echo off
set a=nothing
if z==z (
set a=b
echo %a%
)
pause
setlocal enabledelayedexpansion
set a=nothing
if z==z (
set a=b
echo !a!
)
pause
Gee - a question nearly a year old, with no answer.
I'll assume that the problem has now been solved, so as a matter of record, I'd conclude that the
"sometimes it does not properly calculate" is because the hour/minute/second/hundredths will contain "08" or "09" which are not octal numbers.
The solution is
set /a startcsec=1%STARTTIME:~9,2% - 100
and repeat with each of the other 3 start time-segments; then repeat again with the end parts.
In addition, it could be that the hour is being presented with 0s suppressed. In this case, I'd suggest
set starttime=0%TIME: =%
set starttime=%startTIME:~-11%
set /a startcsec=1%STARTTIME:~9,2% - 100
where the first line prefixes the time with a '0', and replaces Space with [nothing]
the second selects just the last 11 characters of the result
and the last is the familiar form, using the resultant hh:mm:ss.cc format.
(obviously, the remainder of the substring-and-calculate method needs also to be implemented)

Get a filename bat

I am new to BAT writing.
I am trying to write a batch file which will check to see if a file is above or below a certain size and then send an email accordingly. I have written something that can do this with a static file name
#echo off
setlocal
set file="ssoff.bat"
set maxbytesize=1000
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
if %size% LSS %maxbytesize% (
echo.File is ^< %maxbytesize% bytes
blat -server mail.omers.com -f checker#omers.com -t rplomp#omers.com -s "filesize less than" -body testbody
) ELSE (
echo.File is ^>= %maxbytesize% bytes
blat -server mail.omers.com -f checker#omers.com -t rplomp#omers.com -s "filesize greater than" -body testbody
)
In this case, the filename being checked is ssoff.bat. However, I need to have the bat check a filename which changes daily according to the date. The mask for the filename uses the date string IE: deployEAR_restartWAS_03132013.log ; deployEAR_restartWAS_03142013.log ... and so on - with the last 8 chars reflecting the date generated. The bat needs to check the latest file in that directory. For today it would be deployEAR_restartWAS_03152013.log
This logfile would not be in the parent dir either.
I thought maybe of having the bat copying over the latest file from that dir to the parent dir and then checking its size? Or using the static part of the filename 'deployEAR_restartWAS_' and then passing the last part of it through a date variable?
But I'm not sure what approach would be best, and I'm sure there are others that I have not thought of.
The bat file run time would be the same day as the date variable at the end of the filename.
for /f %%i in ('dir /b /a-d /od deployEAR_restartWAS_*.log') do set name=%%i&set size=%%~zi
echo latest file is %name% size %size%
Can't work out in which directory this set of .log files resides from your description. You tell us it won't be in the parent directory and your only reference appears to be that directory.
If the .log files are not in the CURRENT directory, all you need do is add the directory name to the deploy... thus: Q:\wherever\it\maybe\deployEAR_restartWAS_*.log or, if the file's path contains spaces, quote the name thus: "Q:\where ever\it\may be\deployEAR_restartWAS_*.log"