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

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"

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%

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)

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

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

syntax error on batch File to create a timestamp directory

I tried the script below to create a timestamp directory in one of my drives, for some reason is giving me a syntax error on the last string where it create the directory. See below.
:: Code begins....
pause
W:
pause
cd W:\VL2000_AMF\AMF_Archive
pause
for /F "tokens=1-4 delims=. " %%i in ('date /t') do (
set Day=%%i
set Month=%%j
set Year=%%k
)
pause
for /F "tokens=1-4 delims=: " %%i in ('time /t') do (
set Hour=%%i
set Minute=%%j
set Second=%%k
)
pause
md %1\%Year%-%Month%-%Day%
pause
:: Code ends....
I used the following on Windows 2000 and 2003 to get year, month, and day from the date command output. Unfortunately, I no longer have any XP systems to see if this will work there.
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do (
echo "Day=%%i"
echo "Month=%%j"
echo "Year=%%k"
)
What is the format of "date /t" and "time /t" output on XP?
Are you sure your date is coming back as mm.dd.yyyy (separated by dots, and with the fields in the order you expect)? If the process's locale is not what you expect, you might be ending up with %Day% as something like 8/17/2010, which would cause md to report a syntax error as it tries to interpret /17 and /2010 as options.