Windows CE 5 batch IF EXISTS - batch-file

I'm trying to run a batch file on a WINCE device that will ping another networked device and capture the results in a text file.
However, whenever I try to execute the section that checks to see if the log file already exists, I get this error:
IF: incorrect command syntax
Can anyone shed some light as to why?
Here is the bit that creates the text file to log the results. It may or may not be worth mentioning, but it works fine on a windows 7 PC:
SET DATETIME=%date:~0,2%\%date:~3,2%\%date:~6,4% 0%time:~1,1%:%time:~3,2%:%time:~6,2%
if NOT exist %DATEVARIABLE%-log.txt type "New Log" > %DATEVARIABLE%-log.txt
Thanks
Doug

Something looks like this :
#echo off
SET "DATETIME=%date:~0,2%-%date:~3,2%-%date:~6,4%_%time:~1,1%-%time:~3,2%-%time:~6,2%"
echo "%DATETIME%"
pause
if NOT exist "%DATETIME%-log.txt" echo "New Log" > "%DATETIME%-log.txt"
pause

Related

Logic to identify errors logged

How can I check for a particular text logged to a file in Win 10 ? There is a SAS daemon that fails occasionally and at which point logs a message to a log file (C:\SAS\logs\daemon_error.log). The message is logged (with timestamp) only when the error occurs. I want to write a program (and schedule to run every 15 mins) which would check the occurrence of any error-message since the last error recognized. For e.g. if the last error was logged on July 15th the file would have looked like :
07-15-2018 13:21:42 : Incorrect parameters
Now if some error gets logged again today, it would look like below, at which point the script should trigger and identify it and send an email to a list of intended recipients. I presume there needs to be a mechanism to store the last timestamp when the error was recognized so that it only identify errors logged after the last one.
07-15-2018 13:21:42 : Incorrect parameters
09-10-2018 04:32:09 : Temp directory full
How can I accomplish this thru a batch file or perhaps any windows event(s). I do not have much knowledge on Win systems as I primarily worked on Unix. Can anyone please help ?
Thanks
When I understood you correctly, you want to check for timestamp only to know, if there are new errors since the last check, but don't necessarily need to know, which errors. For that, I suggest using the "Archive attribute" (Windows sets this attribute at each write access to the file)
#echo off
:loop
timeout 900
dir /aa "C:\SAS\logs\daemon_error.log" >nul 2>&1 || goto :loop
echo The file was changed!
attrib -a "C:\SAS\logs\daemon_error.log"
goto :loop
The dir command will not find the file when the Archive attribute is not set (/aa) (or the file doesn't exist) and || goto :loop will then jump to the :loop label.
>nul 2>&1 will just discard any output of the dir command - we don't need it, just the errorlevel)
If the file was found (the Archive attribute is set, so we know, there was a write access), the code continues. Replace the echo command with the command to write an email (there are several questions/answers about that on SO).
attrib -a unsets the Archive attribute.
Note: Instead of the loop, I would set up a scheduled task, but keep with the loop for troubleshooting until you're sure, it works as intended.

Windows Batch: Executing command with FOR /F -- "command not found"

I have a problem with executing an other command within a FOR-command on Windows 7 Ultimate.
The for-command is part of a batch-file and should parse the outputs of another command.
Usually this:
for /f %%a IN ('tasklist') DO echo %%a
should execute the command "Tasklist" and output its results via echo.
But I always get "command not found".
I tried to execute the command outside of the forloop and it works.
I also tried do execute lots of outer commands within the for, but every command said "command not found".
I also tried the examples of this post:
Batch: Execute command with quotes in for loop with piping to find
I`m sure
that the commands exists
that I have read and execute rights to it
that my User is in Administrator Group
that I run the commands with "Run as Administrator"
But nothing is working in this pc.
The same commands work on another PC wich is also running a windows 7 ultimate.
So has anybody an idea would could be wrong on the pc where all commands are not found ?
Here is an example of my console outputs when I try it with the command "ls".
ls.exe is a executable file from the gun4win project, and its located in the same folder where my batch-file is running.
The windows is in german, so the error output is also in german.
C:\test>test_for.bat
C:\test>rem --- test a command stand-alone ---
C:\test>ls
ls.exe test_for.bat
C:\test>rem --- test same command in a FOR-Loop ---
C:\test>for /F "delims=" %a in ('ls') do echo FOR-OUTPUT: %a
Der Befehl "ls" ist entweder falsch geschrieben oder konnte nicht gefunden
werden.
FINAL EDIT:
The problem was as wrong value for the system envoirement variable ComSpec.
I changed ComSpec in Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe and the problem was solved.
Thanks to #foxidrive and #jeb
If you use FOR /F and get the error 'something' is not recognized as an internal or external command, operable program or batch file for every program, even internal commands, the most common cause is a wrong ComSpec variable.
You can check the variable with set ComSpec, it should be C:\Windows\System32\cmd.exe.
It will not help to change the variable on the command line, the cause is described at DosTips: ComSpec strange behaviour
If the variable contains a different value, you should correct this under
For a German system:
Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen
For an English system:
Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables
There exists a second possible cause for strange FOR /F behaviour
If the AutoRun feature can be enabled in the registry ...\Command Processor\AutoRun, for more details see cmd /?.
The AutoRun feature can start a batch file each time a new cmd.exe instance is started.
This can be useful for ex. showing some data on opening a new cmd window or always change to a choosen directory.
But this batch will be also executed inside the FOR /F and normally causes unexpected results.
Pipes also start new cmd instances, but suppress the AutoRun script

Batch chinese(?) symbols change in cmd

Related to this problem: Error code 259 when trying to pair using Bluetooth Command Line Tools
I made a simple batch that pairs my bluetooth gamepad with my PC, but unfortunately the gamepads screen name is in chinese(?) and when I run the batch the symbols are different in the cmd than in the batch.
I'm using Bluetooth Command Line Tools
Here's the batch:
rem #echo off
btpair -p -n"小米蓝牙手柄"
if errorlevel 1 goto error
exit
:error
pause
exit
...and here's what it looks like in the cmd window:
C:\Windows\system32>rem #echo off
C:\Windows\system32>btpair -p -n"Õ░Åþ▒│ÞôØþëÖµë﵃ä"
Remote device "ıª┼■ªªÌ¶Ï■ÙÍÁÙ´Áâõ" not found.
C:\Windows\system32>if errorlevel 1 goto error
C:\Windows\system32>pause
Press any key to continue . . .
I tested it manually (copy-pasted the name to cmd) and it worked, but not from batch.
As you can see I have a problem here...
Ideas?
You may be able to use the command line tool devcon.exe from Microsoft to disable and re-enable just that piece of hardware - it should re-pair the device as a side effect I am guessing.
Changing the codepage with the chcp command in the batch file may help with the chinese characters.
You must change your system locale to have chinese character input/output.
Control Panel -> Region -> Administrative -> Change system locale... -> Current system locale (change it here)

BAT file programming assistance : how to make a response in a bat file copy to another part OR to clipboard?

My apologies if the title was confusing. It should be fairly basic, but I cannot find a way to do this.
What i'm wanting to do is have a bat file prompt for an answer to a question and then surround that answer with another piece of code and either copy to the clipboard or after the connection.
Maybe it will make more sense if i give the code.
#echo off
set /p input ="what server would you like to connect to? (example srv02) :"
echo Myhome.%input%.com
pause
c:\program files\putty
the echo gives the correct response, but i would like to see if there is a way to paste this past the c:\program files\putty to connect to a server.
OR if there is a way to copy that response to the clipboard so the bat file would open putty (which it does now) and then you could just paste that response.
Or am i going about this the wrong way? thanks for the help!!
I'm not a PuTTY user, so I can't be sure - But I believe you are simply trying to pass the the server host that you want to connect to. I think the following will do what you asked:
"c:\program files\putty" %input%
or
"c:\program files\putty" Myhome.%input%.com
However, there are probably other command line options you need to really accomplish your goal.
You should learn the PuTTY command line arguments and options. I suspect a web search could find some tutorials or examples to get you started.
Perfect! That is what i ended up doing after a few more hours of research.
I came up with two different types of scripts as my company wants us to use a specific set of defaults for different servers.
#echo off
set /p input="what server would you like to connect to? : "
echo myhome.%input%.com| clip
echo ----
echo myhome.%input%.com has been copied to your clipboard,
echo you can now paste into the host name box in putty
echo -----
echo Press enter to open puTTY
echo -----
Pause
start c:\users\public\apps\putty.exe
Ended setting it to open putty instead of pasting because it should be defaulted settings, but not everyone kept it that way, of course.
Thanks for the assistance though!

Gracefully trap error on start cmd

On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.

Resources