Print Batch results to a text file? - file

I created a batch file to lookup my external ip.
and it works well .
This is the code.
#echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML
for /f "tokens=7 delims=:<" %%a in ('cscript /nologo "%temp%\ip.vbs"') do set ip=%%a
echo %ip:~1%
pause
What i want is to Print the results to a text file named "IPlog.txt"
and every time i run the bat file it has to do the same thing and print the new results to the next line in the text file. So please can anyone help me with this.

... or change your
echo %ip:~1%
to
echo %ip:~1% >>IPlog.txt
to run your batch without the additional " >>IPlog.txt "

Please remove the pause command from your code and run the batch-file like this
mybatch.bat >> IPlog.txt
This will append the resulting ip address on to the log file IPLog.txt every time you run this batch file.

Related

Script Shell CMD not running

I running Windows 10 and I can't execute a script shell in .bat
So, my code is:
#echo off
echo .
echo MsgBox "TA MERE"
echo .
echo MsgBox "Salut ca va?"
> msgbox.vbs
cscript msgbox.vbs
loop
So, my script is running but the script do not create msgbox.vbs file on the desktop and the program close the Windows
This is a guess based upon what I think your script is supposed to do:
#echo off
( echo.
echo MsgBox "TA MERE"
echo.
echo MsgBox "Salut ca va?"
)> msgbox.vbs
cscript msgbox.vbs
I've missed out loop because I don't know where you want to loop to.
You can try something like that :
#echo off
REM We create our vbs file in temporary folder
REM If you want to create your vbs file on your desktop
REM Just Replace this line set "VBSFile=%Tmp%\%~n0.vbs" to
REM set "VBSFile=%userprofile%\desktop\%~n0.vbs"
set "VBSFile=%Tmp%\%~n0.vbs"
(
echo MsgBox "TA MERE TOI MEME LOL !",vbExclamation,"TA MERE TOI MEME LOL !"
echo MsgBox "Salut Comment ca va ?",VbQuestion,"TA MERE TOI MEME LOL !"
)> "%VBSFile%"
REM We execute our vbs file
cscript /nologo "%VBSFile%"
REM We Clean our vbs file
Del "%VBSFile%"

redirecting .exe output within batch script to txt file

i am trying to redirect output from an exe(commanline exe) which is being called in from batch file to log file. In this script IP addresses of hostnames provided in input.txt are being redirected to result.txt. i am trying to run .exe within same batch script to place those IPs in maintenance mode in my monitoring tool. Script runs fine and performs the action as expected but it fails to capture the output from .exe. please help.
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (input.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo !SERVER_ADDRESS::=!>>%OUTPUT_FILE%
)
start c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"
Output from .exe if i run it directly from command prompt:
PS C:\> .\MaintenanceMode.exe ON C:\result.txt username
Not an IP!!
Reading IPs from File: C:\result.txt
Valid Arguments
System put into MM Host# 10.*.*.* Status# Success
System put into MM Host# 10.*.*.* Status# Success
You are redirecting the output of the START command, but not the exe.
If you want to use START and redirect the output, then you most execute a new CMD.EXE session and escape the redirection so it occurs within the new session:
start cmd /c c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% ^>^> "c:\result2.txt"
But why are you using START? It would be so much simpler if you simply execute your exe directly:
c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"

scheduling of batch script through task scheduler

I have below script which have output as well as log redirection along with date and tiem written into the log and erro files. i am saving this script with .cmd extension. Manually when i try to run this script through the command prompt it runs perfectly and first write current date and time in output and error log and then start recording the logs. but when schedueled through task schedule it only writes current date and time int othe logs but not hte actual logs. Can someone please let me know how i can schedule this script in such a way it will first record the current date and time and then start recording the logs.
Echo Date:%date% Time:%time% >> error.txt
#echo off
(
Echo Date:%date% Time:%time%
start "" /wait /b "D:\ITSMaaS\BTscripts\capgemini\BESExtract\bin\BES_EXTRACT.exe" "-f D:\ITSMaaS\BTscripts\capgemini\BESExtract\conf\BES_EXTRACT.CONF"
:loop
for /f "tokens=2 delims=: " %%a in ('tasklist ^| find "BES_EXTRACT.exe"' ) do (
if "%ERRORLEVEL%"=="0" (
ping -n 10 localhost > nul 2>nul
goto loop
)
)
start "" /wait /b "D:\ITSMaaS\BTscripts\capgemini\BESExtract\bin\BES_DATA_MAP.exe" "-f D:\ITSMaaS\BTscripts\capgemini\BESExtract\conf\BES_DATA_MAP.conf"
) >> Output.txt 2>> error.txt
Use redirection on the batch file. In order to output date and time to both files, you could use this technique:
ECHO Date:%date% Time:%time%
1>&2 ECHO Date:%date% Time:%time%
The first ECHO will write to standard output which would be caught with >> Output.txt applied to the batch file. The second ECHO will write to standard error, that output would be redirected by 2 >> error.txt.
If you are worried that the values might differ slightly (by hundredth of a second, perhaps), you could first store the output string to a variable:
SET "datetime=Date:%date% Time:%time%"
ECHO %datetime%
1>&2 ECHO %datetime%
Set 'program/script' : 'yourfile.bat' WITHOUT PATH
Set 'Start in' the rest of path

taking and writing variables for a batch file

Basically here's what I want: A batch file that prompts the user to set a variable,
set /p x=
then the batch file writes the variable to a file of some sort (abc.txt) Then later on, in a different batch file, the program retrieves the variable from the text document, and sets it as %x% again for whatever use. If there are any questions, or if I'm not clear enough, please comment, and I will revise. thanks.
In Batch Files, you can redirect input and ouput using < and > respectively.
Input.bat
#echo off
:: Take input and set value to x
set /p "x=: "
:: Print out the value of x to the screen, but redirect this to a text file
Echo %x% >> abc.txt
Echo EOF & Pause & Exit
Read.bat
#echo off
:: Set x to the first line in abc.txt
set /p x=< abc.txt
Echo First Line of abc.txt: %x%
Echo.
:: Set x to last line in abc.txt, incase it is multi-line file
for /f "delims=" %%a in (abc.txt) do (set x=%%a)
Echo Last Line of abc.txt: %x%
Echo.
Echo EOF & Pause & Exit
That should help you understand.
Mona.
I figured out a way that works best for me.
So I have the variable %x%, right? I got it from this:
set /p x=
then I write a mini-batch file.
echo set y=%x% >> abc.bat
then later on in a different script, I can use
call abc.bat
the variable y will be the value that I had in the origional batch script.

Command for getting the file size

Could anybody please tell me a shell command for Windows 7 which would take a file path as argument and return the size of that file - Something like:
fileSize.cmd file.txt
...which will give me 1KB.
One question in SO noted the command echo %~z1, but for this, I have to write a separate batch file and use this command in it. I was thinking of modifying my existing bat file and incorporate this command somehow. My batch file looks like this:
p4 diff //sources/j2cs/output.txt >> diff_out.txt
I have to add above command in the existing bat file to find the file size of diff_out.txt.
You don't need an extra batch file, you could move your filename into %1 with a call to a function or you can use a FOR loop.
call :getFilesize diff_out.txt
echo %fileSize%
exit /b
:getFilesize
set filesize=%~z1
exit /b
Or
for %%A in (diff_out.txt) do set fileSize=%%~zA
another variant:
#echo off
set file=c:\bookmarks.html
%1 %0 :: %file%
set len=%~z2
echo %len%
pause
or with wmic:
D:\>set wql="drive='g:' and filename='function2' and extension='txt'"
D:\>wmic path cim_datafile where %wql% get name,filesize
FileSize Name
621 g:\function2.txt
D:\>
or:
set file=G:\function2.txt
echo set len=%%~z1 >_tmp.bat
call _tmp.bat %file% && del _tmp.bat
echo %len%

Resources