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

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%

Related

Batch - How to Echo %%Variable%%

I have a small problem with a batch file I'm working on.
Here's a simple sample:
I would like to get the string "THERE" as my result.
But the result I get is just "HELLO"
set hello=there
set a=h
set b=ello
set result=%a%%b%
echo %result%
I already tried something like this:
Echo %%result%%
And Sadly, it just gets me the result %HELLO%
Any help would be great. Thanks!
The reason that %%result%% gives you %result% on the output is that the %% tokens are interpreted first, and further interpretation is not done.
However, you can use that to your advantage, doing a second level of indirection with the following trick:
#echo off
set result=hello
echo %result%
call :iset second %%result%%
echo %second%
goto :eof
:iset
for /F "usebackq tokens=*" %%i in (`echo %%%2%%`) do set %1=%%i
goto :eof
The secret lies in passing the thing you want interpreted (in this case, %%result%% passes in the literal %result% as per the rules stated in the first paragraph, not the interpretation of it).
The for loop then echos the interpretation of that (hello) surrounded by %...% (again, the double %% reduces to %), so that it is also interpreted, and it uses that to set the target variable you also passed in.
The upshot is that it effectively gives you:
%(%result%)%
which is what you're after.
Might I suggest, however, that you start looking into Powershell, so that you don't have to perform these batch-gymnastics in future :-)

Batch for loop - Variable issue

I have to do a few things in bash and batch. I'm stuck on the batch for-loop part. The instructions for part of my assignment are as follows:
::Doing a for loop from 1 to 100,
:: - Find the results of calculating each number from 1 to 100 mod 5.
:: - Append each number to the results.txt file
:: - After the for loop ends calculate and display the average of the results
:: - Append the average of the numbers to the results.txt file
::Code
:forLoop
echo.
::set /A temp-0
for /L %%x in (1, 1, 100) do (
set /A result="%%x %% 5"
call echo %%result%% >> results.txt
::%%temp+=%%result%%
)
::average=temp/100
::append average
GOTO exit
Other users helped me with result variable and the mod 5. However, I'm currently having trouble with temp. I think once I get temp to work I should be able to get the average part working without too much issue. My professor also mentioned that there are 3 different kinds of for-loops in batch, so I'm not even sure if I'm using the right one. Can anyone please help me figure this out.
echo.
set /A temp=0
for /L %%x in (1, 1, 100) do (
set /A result="%%x %% 5"
call echo %%result%%
CALL SET /a temp+=%%result%%
)
SET /a average=temp/100
ECHO %average% %temp%
This is quite straight-forward. Don't use :: comment-style within a block (parenthesised series of statements) as it's actually a broken-label which breaks the loop.
Beyond that, you need to call the set because you are not using delayedexpansion - hence the requirement to double the customary number of %s - same as call echo.
I've taken out the redirection so that the result simply appears on screen.

Batch - set variable error; variable(%)(string)(%%a)(%)?

On the batch script I'm currently working on, I've encountered a bit of a problem.
What I want to happen is to set a variable as itself, but after it to be followed by a percentage mark, a letter, a number from a for /l (#,#,#) loop, then another percentage sign.
My code is currently as follows;
set value1=1
set value2=10
for /l %%a in (%value1%,1,%value2%) do (
set variable1=%variable1%%b%%a%
)
This doesn't give me a value for %variable1% at all, even after looping this 10 times. I've tried adding 'variable1= ' (without apostrophes) to the top, however that gave me the same result. After searching around, I figured that I should try and cancel out the %'s which are before the 'b' and after the 'a' - using %'s in front of them -,and my code ended up like this;
set value1=1
set value2=10
set variable1=
for /l %%a in (%value1%,1,%value2%) do (
set variable1=%variable1%%b%%a%
)
This is the closest I got, however %variable1% would change the value to '%b1%', then '%b2%', then '%b3%', etc. Instead of tiling them up next to each other.
My desired result would be for, by the end of the loop, %variable1% to have a value of; %b1%%b2%%b3%%b4%%b5%%b6%%b7%%b8%%b9%%b10%
Where is my code going wrong? It seems that it should give me my desired output, however quite clearly, it's not.
Thanks in advanced,
Try this:
#echo off
setlocal EnableDelayedExpansion
set value1=1
set value2=10
set variable1=
for /l %%a in (!value1!,1,!value2!) do (
set percent=%%
set variable1=!variable1!!percent!b%%a!percent!
)
echo !variable1!
You will have to add setlocal EnableDelayedExpansion as this will helps to expand the variable at execution time rather than at parse time.

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)