Input Error: There is no file extension - batch-file

If this is a rookie mistake I apologize for wasting your time.
As part of a larger batch file to create a directory with today's date, copy and paste files to be backed up, change permissions and delete files older then X days I've run into a problem. Here's where it happens:
echo var D = new Date() > tmp.js
echo D = (D.getFullYear()*100+D.getMonth()+1)*100+D.getDate() >> tmp.js
echo WScript.Echo( 'set YYYYMMDD='+D ) >> tmp.js
echo #echo off > tmp.bat
cscript //nologo tmp.js >> tmp.bat
call tmp.bat
The command prompt spits back this error:
Input Error: There is no file extension in "C:\Users\name\Desktop\Error:".
So obviously the rest of the batch file fails but the weird thing is this works on one machine on the network but not the other.
Any help is greatly appreciated.
EDIT: there is one more line to that code that is returning the error, my mistake.
After I call tmp.bat:
mkdir "\\network\file\%YYYYMMDD%"

This code, as is, will not generate the indicated error
BUT, if the path to the batch file contains spaces, and the code is something like
cscript //nologo %~dp0\tmp.js
you will get the indicated error as the argument to cscript is incorrectly parsed because the space in the path to the js file. If this is the case (or something similar), quotes in the filename should solve it
cscript //nologo "%~dp0\tmp.js"
Also, the temporary batch file is not needed. Output from cscript can be directly read with the help of for command
set "tmpJS=%~dp0\tmp.js"
>"%tmpJS%" echo with(new Date()){WScript.StdOut.WriteLine(getFullYear()*10000+(getMonth()+1)*100+getDate())};
for /f %%D in ('cscript //nologo //B "%tmpJS%"') do set "YYYYMMDD=%%D"

Wow - that is a very convoluted way to get the current date in a variable.
This is not a direct answer to your question, but I think I can make your life much easier :-)
There really is no need for a temporary batch file. FOR /F could process the output of your temporary JS script directly.
But I have an even easier solution - GetTimeStamp.bat is a hybrid JScript/batch utility that can do nearly any date and time computation on a Windows machine.
The utility is pure script that will run on any modern Windows machine from XP forward - no 3rd party executable required.
Assuming getTimestamp.bat is in your current directory, or better yet, somewhere within your PATH, then the following simple call will define a YYYYMMDD variable containing today's date in YYYYMMDD format:
call getTimeStamp -f {yyyy}{mm}{dd} -r YYYYMMDD
There are a great many options for specifying the base date and time, many options for adding positive or negative offsets to the date and time, many options for formatting the result, and an option to capture the result in a variable. Both input and output can be directly expressed as local time, UTC, or any time zone of your choosing. Full documentation is embedded within the script.

Figured it out:
running cscript on tmp.js led to an error begin put into tmp.bat
call tmp.bat then returned the weird input error because of the error message inside of tmp.bat.
tmp.bat looked like this when editing
Cscript Error: Can't find script engine "JScript" for script "C:\Users\name\Desktop\tmp.js".
That's why calling tmp.bat returned this error message:
Input Error: There is no file extension in "C:\Users\name\Desktop\error:".
The problem ended up being with jscript.dll. It needed to be (re)registered.
regsvr32 %systemroot%\system32\jscript.dll
^Fixed my problem. Thank you all for the help.

Related

Use content of a file in a string

I have a file (let's call it version.txt) that contains a version number and some text:
v5.02
Some text explaining
where and how this
number is used
Based on this answer, I use
set /p version=<version.txt
to store the first line of the file in the version variable. Now I'm trying to write a batch script that operates on folders that contain this version number in their name. However, I get unexpected results because something seems to go wrong when I insert the variable in a path. For example, this script
#set /p version=<version.txt
#echo C:\some\folder\%version%\some\file.exe
prints
C:\some\folder\v5.02
instead of
C:\some\folder\v5.02\some\file.exe
What's going on? I have a feeling there are hidden characters of some sort at the end of the text in the variable, because setting the variable by hand to a constant in the script works.
Edit: I'm using Windows 10 with Notepad++ as my editor, if it helps.
I can only replicate your issue, when version.txt uses Unix line endings (LF) instead of Windows (CRLF). for /f is immune to this issue:
for /f "delims=" %%a in (version.txt) do set "verion=%%a" & goto :skip
:skip
echo C:\some\folder\%version%\some\file.exe
goto :skip breaks the loop after reading the first line.
Since everything I tried didn't seem to work, the solution I found in the end is to call the batch script from a Python script. The Python script reads the first line of the version file and passes it as an argument to the batch script. Out of context, it is a bit of an inelegant solution, but in my case the batch script was already called by a Python script, so it's not that terrible.
Here is a minimal example:
version.txt
v5.02
Some text explaining
where and how this
number is used
script.bat
#echo C:\some\folder\release\%1\some\file.exe
script.py
import os
with open("version.txt") as f:
version = f.readline().rstrip()
os.system("cmd /c script.bat %s" % version)
Edit: Following Stephan's comment, I tried to change the line ending in the text file from LF to CRLF and it indeed solves the problem. However, since I don't really have control over everything that writes in that file, the solution above remains the most feasible in my case.
Edit 2: Stephan's answer (with the for loop) is actually a better solution than this one since it avoids having to transfer part of the work to the calling Python script.

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.

Batch File Problems - Batch Game Code Error in Running, but runs fine Outside in CMD.exe

Geo here, I have a command (Kinda Confuzzling) that isn't working. It works outside of the batch file fine though! All the Parameters are set, and i would prefer it on one line (Which it should work on). It doesn't work in a batch file, but in CMD works perfectly
Here is the command:
IF %Move%==North IF %0x-1Seen%==1 (set LevelXY=0x1 & GOTO :Level1~0x1) ELSE (Resources\Sound\SWPlayer.exe "Locked.wav")
This is a ONE LINE code. Thanks - Geo Skionfinschii
EDIT: It is a script to test if you have gone to that area - It does work. BUT not in the game: ' The Error Is: ELSE IS NOT EXPECTED AT THIS TIME
IF %0x-1Seen%
within a batch, %0 will be replaced by the executable-name; the 0x-1Seen will not be seen as a variable (same for %1..%9 - parameters 1..9 to the batch process)
Tip 1 : Don't use initial numerics in variables
Tip2: "quote" user-input when testing for keystrings. if /i "%move%"=="north" for instance (assuming that move is a user-input using set/p) - the /i makes the match case-insensitive.

trying to pass variables adds extra characters

Ive seen others address this several years back but they didnt seem to give a working answer.
When I try to pass these two commands in a batch file (they work when done by hand) :
nltest /dsgetsite>c:\windows\temp\site.txt
set /p CurrentADSite<c:\windows\temp\site.txt
but when I attempt to issue the commands via a batch file I get this:
C:\working>nltest /dsgetsite 1>c:\windows\temp\site.txt
C:\working>set /p CurrentADSite 0<c:\windows\temp\site.txt
The syntax of the command is incorrect.
How on earth do I actually get this to work? Is there an easier way to pass the dsgetsite results straight into a variable?
You can use for /f to avoid the temp file (also you [usually] can't write to the Windows directory so that will blow up anyway):
for /f %%x in ('nltest /dsgetsite') do if not defined CurrentADSite set CurrentADSite=%%x
According to http://ss64.com/nt/set.html you may have missed out an =
"To place the first line of a file into a variable:
Set /P _MyVar=<MyFilename.txt "
If all else fails you could always fall back on:
:: start the temp batch with start of set command
echo set CurrentADSite=>c:\windows\temp\site.bat
:: add to the temp bat : the variable assignment
nltest /dsgetsite>>c:\windows\temp\site.bat
:: run the temp batch and return here
call c:\windows\temp\site.bat

Why won't a batch file run when being called from within another batch file?

I have a batch file that first creates another batch file containing a ClearCase cleartool command and second, runs it:
ECHO cleartool lsactivity -long "%ACTIVITY%"^>"%OUTPUTFILE%">FILETORUN.bat
CALL FILETORUN.bat
When running the batch, FILETORUN.bat is generated with the correct format, but the CALL to it is completely ignored.
If I ECHO output after the CALL to a log file, I can see that the script just skips over it.
What could it be?
I have tried removing CALL but it makes no difference.
EDIT: SOLUTION
Thank you all for the input. I found the problem. Before the write to batch and batch call in the script there was a command that read information into a variable from a file:
SET /p FILETODELETE=<rmname_%CLEARCASE_USER%.tmp
It reads only the first line. For some reason this created a conflict with temporary batch file, and I have no idea why. I used a different solution for reading the first line from a file and the conflict doesn't happen anymore:
(set FILETODELETE=)
for /f "delims=" %%q in (rmname_%CLEARCASE_USER%.tmp) do if not defined FILETODELETE set FILETODELETE=%%q
If anyone can shed some light it would be great!
SET /P waits for user input, so it actually will finish the command with what you are trying to execute after that and consume the input buffer, which might produce different results on each machine.
See set command reference for more details

Resources