Command line - "x was unexpected at this time" - batch-file

I was creating something with command line and came across this error - 'x was unexpected at this time', I tried switching the variable to x, %%x and %x% but none of them worked. Here are the code in the part:
echo Please enter the memory value [Recommended: 512M Xms 1024M Xmx]
set /p rams=Xms[MB]=
set /p ramx=Xmx[MB]=
echo %rams%|findstr /R "^[1-9][0-9]*$">nul
if NOT %errorlevel% EQU 0 (
echo Error: Please enter a valid number [Xms].
)
echo %ramx%|findstr /R "^[1-9][0-9]*$">nul
if NOT %errorlevel% EQU 0 (
echo Error: Please enter a valid number [Xmx].
)
if NOT %errorlevel% EQU 0 (
echo Execution failure: Invalid args
pause
goto start
)
set count=0
for %x in (*.jar) do set /a count+=1
if %count% == 1 (
java -Xms%rams%M -Xmx%ramx%M -jar BCU.jar
echo BCU.jar was executed.
pause
goto start
)
if %count% GEQ 1 (
echo [= = = = Multiple file found = = = =]
dir *.jar /B
echo [= = = = = = = = = = = = = = = = = =]
echo Please input file by the whole name with extension.
set /p file=Select File:
if exist %file% (
java -Xms%rams%M -Xmx%ramx%M -jar %file%
echo %file% was executed.
pause
) else (
echo File was not found.
pause
)
goto start
)
For some reason, the for command works normally when I execute it from the command prompt, but not when it was ran.
I ran the program as administrator
Only the for part is faulty, anything above it works normal
It is expected to have the count value defined and able to be used for the IF statement below
It is known that the java line works just fine without problem
For the full file it is located at: https://drive.google.com/file/d/1SgdoyCL5qaZGIWGJQDUKEJyEyUI2Y2u8/view?usp=sharing

Your immediate error is that you need to use %% within batch files, a single % works only on the command line. In other words, it should be:
for %%x in (*.jar) do set /a count+=1
(I'm not convinced you did try %%x as you state, since that actually works, but it may be that you just formatted it incorrectly).
But you'll strike another problem (at least) with using things like this (pared back and formatted for explanation):
if %count% GEQ 1 (
set /p file=Select File:
if exist %file% (
java -Xms%rams%M -Xmx%ramx%M -jar %file%
)
)
The entire block above is evaluated before it executes, meaning that %file% will be turned into whatever it was before the block started, despite the fact you're setting it within the block.
What you need to do is setlocal enabledelayedexpansion at the top of your file, and use the !file! delayed-expansion variant, something like:
setlocal enableextensions enabledelayedexpansion
:
if %count% GEQ 1 (
set /p file=Select File:
if exist !file%! (
java -Xms%rams%M -Xmx%ramx%M -jar !file!
)
)
:
endlocal
It's only needed for variables that can change within the loop, so not for the %rams% and %ramx% in this particular case - they're set before the if block starts.

Related

What is the reason for "( was unexpected at this time." on an IF command line with %var%?

When I start the batch file as posted below, I got this error
( was unexpected at this time.
I think this happens on the IF command line if %ad%==60 ( but I am not sure.
( was unexpected at this time.
#echo off
color 0f
title TITLE
mode con cols=50 lines=25
set ad = 0
set s = 0
set m = 0
set h = 0
set d = 0
if exist start.txt (
del start.txt
goto :1
) else (
exit
)
:1
if %ad%==60 (
:: Something here
set ad = 0
)
:: MINUTES
if %s%==60 (
set /a m=m+1
set s = 0
)
:: HOURS
if %m%==60 (
set /a h=h+1
set m = 0
)
:: DAYS
if %h%==24 (
set /a d=d+1
set h = 0
)
cls
echo Something here...
timeout 1 > nul
set /a ad=ad+1
set /a s=s+1
goto :1
What could be the reason for this error message on execution of the batch file?
For a start,
set ad = 60
will not set ad to 60. It will set the variable adspace to space60, leaving ad to whatever it was before the script started.
In your case, it's almost certainly an empty string because the command that would result from that would be the same as that in the following transcript (note the error generated):
d:\pax> if ==60 (
( was unexpected at this time.
If you want nicely spaced out expressions, you already know how to do that, since you used it to increment h. In other words:
set /a "ad = 0"
This isn't technically an answer, (that has already adequately been provided). It is just an example to show you how you may optionally shorten your script a little:
#Echo Off
Color 0F
Title TITLE
Mode 50,25
Set /A ad=s=m=h=d=0
If Not Exist "start.txt" Exit /B
Del "start.txt"
:1
If %ad% Equ 60 (
Rem Something here
Set "ad=0"
)
Rem Minutes
If %s% Equ 60 Set /A m+=1,s-=60
Rem Hours
If %m% Equ 60 Set /A h+=1,m-=60
Rem Days
If %h% Equ 24 Set /A d+=1,h-=24
ClS
Echo Something here...
Timeout 1 >Nul
Set /A ad+=1,s+=1
GoTo :1
Notes: 1. Using Timeout 1 will not increment clocklike (in accurate seconds), it would be approximately one second plus the time taken to run through :1 again. 2. Be careful not to fall into Set /A's octal trap, by ensuring that you don't have leading 0's in your variables.
I have IF command without spaces, and also batched with command in quotes; still got error ( was unexpected at this time. when called batch file from command line. If launched from explorer, the batch file crashed.
Here's the crash code:
if %file%==local (
set miss=n
set /p miss=Any missing?
set miss=%miss:~0,1%
if /I %miss%==y (
Goto check2
)
)
When I echo the value of miss before the IF statement, I get %miss:~0,1%
TO fix this, I enabled delayed expansion, to force values to be updated at runtime, and works ok now.
if %file%==local (
set miss=n
set /p miss=Any missing?
set miss=!miss:~0,1!
if /I !miss!==y (
Goto check2
)
)

Batch -"access is denied" when accessing a text file using batch

I have been having problems with making a batch script that can write into a old text file. When i try to run it with the old text file it says that "access is denied" (so when i try to put any text into the existing file)
Anyways here is the code:
#echo off
title file editor/creator
set lineNR=0
:start
set /p ANS= Do you want to access a 1:"old file" or 2"create a new" [1/2]
if %ANS% EQU 1 ( goto old
) ELSE if %ANS% EQU 2 ( goto new
) ElSE ( echo invalid input & goto start)
:old
set /p name = what is the name of the file
set /p ending = what type of file is it
goto loop
:new
set /p name= what do you want the name of the file to be
set /p ending= what type of file do you want the file to be
echo %name%.%ending%
:Q1
set /p echo_off= do you want echo to be off? [y/n]
if %echo_off% EQU y (
echo #echo off >%name%.%ending%
goto loop
) ELSE IF %echo_off% EQU n (
goto loop
) ELSE (
goto Q1
)
:loop
echo press CTRL+LSHIF+C to end loop
goto loop1
:loop1
set /a lineNR=%lineNR% + 1
set /p line= %lineNR%:
echo %line% >>%name%.%ending%
// this is where it says that access is denied
goto loop1
It is just a simple, but a common issue.
set a = b
Creates a variable named a (with the space) and a value of b(with the space).
Remove the spaces and it will work.

Batch file to update a csv file on condition if file exists

Below is the Report.csv file, where I need to update the Status column based on the condition, I file in Lockbox exist. I have written the code for if file exist or not, but don't know how to update the Status column.
Lockbox Location Status Reason(N)
3080 Minot Y
1780 Minot N Not Scheduled
2280 Minot Y
3015 Windsor Y
2215 Windsor Y
1515 Windsor Y
29011 Windsor Y
Below is the code I have written. Please help..
#echo off
setlocal
set count=0
echo %time%
echo %date%
set y=%Date:~10,5%
set m=%Date:~4,2%
set d=%Date:~7,2%
if "%time:~0,1%"==" " (set tym=0%time:~1,1%) ELSE set tym=%time:~0,2%
set tm=%tym%%time:~2,0%
echo %tm%
pause
set pattern1=INGFINS.IMAGE.image1.29011.%y%%m%%d%%tm%
set pattern2=INGFINS.IMAGE.image2.2215.%y%%m%%d%%tm%
set pattern3=INGFINS.IMAGE.image3.1515.%y%%m%%d%%tm%
set pattern4=INGFINS.IMAGE.image4.3015.%y%%m%%d%%tm%
set pattern5=INGFINS.IMAGE.image5.1780.%y%%m%%d%%tm%
set pattern6=INGFINS.IMAGE.image6.2280.%y%%m%%d%%tm%
set pattern7=INGFINS.IMAGE.image7.3080.%y%%m%%d%%tm%
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern1%*.<" (
ECHO "%pattern1% exist"
) ELSE (
ECHO "%pattern1% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern2%*.<" (
ECHO "%pattern2% exist"
) ELSE (
ECHO "%pattern2% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern3%*.<" (
ECHO "%pattern3% exist"
) ELSE (
ECHO "%pattern3% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern4%*.<" (
ECHO "%pattern4% exist"
) ELSE (
ECHO "%pattern4% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern5%*.<" (
ECHO "%pattern5% exist"
) ELSE (
ECHO "%pattern5% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern6%*.<" (
ECHO "%pattern6% exist"
) ELSE (
ECHO "%pattern6% not exist"
)
IF EXIST "C:\Users\Desktop\zipLocation\*%pattern7%*.<" (
ECHO "%pattern7% exist") ELSE (
ECHO "%pattern7% not exist"
)
rem call "statusReport.vbs"
endlocal
pause
Although some other users tend to close your question, I give next solution hoping that it could help. Commented .bat script:
#ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion
rem set input and output files: change to meet your circumstances
set "_fileIn=d:\bat\so\files\39587196_Report.TSV" tab-separated values
set "_fileOut=d:\bat\so\files\39587196_ReportOut.csv" comma-separated values
rem empty output file
>"%_fileOut%" type NUL
rem show input file
type "%_fileIn%"
echo(
rem obtain %_datetime% variable in locale independent yyyymmddHHMMSS format
for /f "tokens=2 delims==" %%G in (
'wmic OS get localdatetime /value') do set "_datetime=%%G"
set "_datetime=%_datetime:~0,14%"
rem reduce %_datetime% variable to yyyymmddHH format as the OQ does
set "_datetime=%_datetime:~0,10%"
set count=0
echo %date% %time%
echo %_datetime%
set "_pattern1=INGFINS.IMAGE.image1.29011.%_datetime%"
set "_pattern2=INGFINS.IMAGE.image2.2215.%_datetime%"
set "_pattern3=INGFINS.IMAGE.image3.1515.%_datetime%"
set "_pattern4=INGFINS.IMAGE.image4.3015.%_datetime%"
set "_pattern5=INGFINS.IMAGE.image5.1780.%_datetime%"
set "_pattern6=INGFINS.IMAGE.image6.2280.%_datetime%"
set "_pattern7=INGFINS.IMAGE.image7.3080.%_datetime%"
set "_pattern8=INGFINS.IMAGE.image7.8888.%_datetime%" for testing purposes
set "_ForGDelims= delims=," CSV input: delimiter is Comma (0x2C Comma)
set "_ForGDelims=" TSV input: delimiter is Tab (0x09 Character Tabulation)
if /I "%_fileIn:~-4%"==".tsv" set "_ForGDelims=" soft-code instead of above line
set "_csvHeader=" CSV header not processed yet
rem iterate input file, line by line, tokenized
for /F "usebackq tokens=1,2,3,*%_ForGDelims%" %%G in ("%_fileIn%") do (
set "_Lockbox=%%~G"
set "_Location=%%~H"
set "_Status=%%~I"
set "_Reason=%%~J"
if defined _csvHeader (
rem show checked Lockbox value (no NewLine)
<NUL set /P "=checking %%~G"
set "_LockboxFoundInVariable="
rem iterate all _pattern* variables
for /F "tokens=1,2 delims==" %%g in ('set _pattern') do (
rem take 4th token in %_pattern*% value
for /F "tokens=4 delims=." %%# in ("%%~h") do (
if "%%#"=="%%~G" (
echo - found %%G in %%g:%%h
set "_LockboxFoundInVariable=%%g"
rem ??? I don't comprehend what this ↓ LessThan should mean ???
IF EXIST "C:\Users\Desktop\zipLocation\*%%h*.<" (
rem ??? I don't comprehend what this ↑ LessThan should mean ???
set "_Status=y" pattern exists
) ELSE (
set "_Status=n" pattern does not exist
)
)
)
)
rem echo NewLine if necessary i.e. if checked Lockbox value not in any _pattern*
if not defined _LockboxFoundInVariable (echo()
) else (
set "_csvHeader=anything" CSV header processed already
)
call :csvLineOut
)
rem show output file
echo(
type "%_fileOut%"
ENDLOCAL
goto :eof
:csvLineOut
rem you could use <TAB>s ↓ ↓ ↓ instead of commas
>>"%_fileOut%" echo %_Lockbox%,%_Location%,%_Status%,"%_Reason%"
goto :eof
Please read Delimiter-separated values about .csv vs. .tsv difference. Note that I don't keep using quotation marks to surround every field strictly…
Output:
==> D:\bat\SO\39587196.bat
Lockbox Located Status Reason(N)
3080 Minot Y
1111 Test Y
1780 Minot N Not Scheduled
2280 Minot Y
2215 Windsor Y
29011 Windsor Y
9999 Test Y
20.09.2016 17:41:53,87
2016092017
checking 3080 - found 3080 in _pattern7:INGFINS.IMAGE.image7.3080.2016092017
checking 1111
checking 1780 - found 1780 in _pattern5:INGFINS.IMAGE.image5.1780.2016092017
checking 2280 - found 2280 in _pattern6:INGFINS.IMAGE.image6.2280.2016092017
checking 2215 - found 2215 in _pattern2:INGFINS.IMAGE.image2.2215.2016092017
checking 29011 - found 29011 in _pattern1:INGFINS.IMAGE.image1.29011.2016092017
checking 9999
Lockbox,Located,Status,"Reason(N)"
3080,Minot,n,""
1111,Test,Y,""
1780,Minot,n,"Not Scheduled"
2280,Minot,n,""
2215,Windsor,n,""
29011,Windsor,n,""
9999,Test,Y,""
==>
Further resources (required reading, incomplete):
(command reference) An A-Z Index of the Windows CMD command line
(helpful particularities) Windows CMD Shell Command Line Syntax
(%~G, %~H etc. special page) Command Line arguments (Parameters)
(special page) EnableDelayedExpansion
(>, >> etc. special page) Redirection
(%_fileIn:~-4% etc.) Extract part of a variable (substring)
(%% doubled percent sign) Escape Characters, Delimiters and Quotes

How to check a string does not start with a number in Batch?

How can I check that the first character of a string is a letter and so that it is not a number, or rather a cipher? There are no spaces or special characters in this string.
#ECHO OFF
SETLOCAL
SET /a num=5678
CALL :initnum
SET "num=hello"
CALL :initnum
SET "num=4ello"
CALL :initnum
SET "num=hell0"
CALL :initnum
SET "num=he8lo"
CALL :initnum
SET "num="
CALL :initnum
ECHO(==============
SET /a nam=7654
SET "nem=hello"
SET "nim=4ello"
SET "nom=hell0"
SET "num=he8lo"
SET "nzm="
CALL :initnum2 nam
CALL :initnum2 nem
CALL :initnum2 nim
CALL :initnum2 nom
CALL :initnum2 num
CALL :initnum2 nzm
GOTO :EOF
:initnum
IF NOT DEFINED num ECHO NUM is empty, so it doesn't begin with a numeric&GOTO :EOF
FOR /l %%a IN (0,1,9) DO IF %num:~0,1%==%%a ECHO %num% Begins with numeric&GOTO :EOF
ECHO %num% Does NOT begin with a numeric
GOTO :eof
:initnum2
IF NOT DEFINED %1 ECHO %1 is empty, so it doesn't begin with a numeric&GOTO :EOF
CALL SET "$1=%%%1%%"
FOR /l %%a IN (0,1,9) DO IF %$1:~0,1%==%%a ECHO %1 (%$1%) Begins with numeric&GOTO :EOF
ECHO %1 (%$1%) Does NOT begin with a numeric
GOTO :eof
You should be able to get what you want from this demo.
#echo off
setlocal enableextensions disabledelayedexpansion
set "var=1hello"
for /f "tokens=* delims=0123456789" %%a in ("%var%") do (
if not "%%a"=="%var%" echo var starts with a number
)
If the var contents starts with a number, the token/delim management in the for command will remove it.
edited just to include the usual (included the previous code) and some less used options just in case someone is interested
#echo off
setlocal enableextensions disabledelayedexpansion
set "var=1hello"
echo(%var%
rem Option 1 - Use the for command to tokenize the string
rem A dot is added to handle empty vars
for /f "tokens=* delims=0123456789" %%a in ("%var%.") do (
if not "%%a"=="%var%." (
echo var starts with a number
) else (
echo var does not start with a number
)
)
rem Option 2 - Use set arithmetic and detect errors
rem This will fail if the string starts with + or -
set "%var%_=0"
set /a "test=%var%_" 2>nul
if not errorlevel 1 (
echo var does not start with a number
) else (
echo var starts with a number
)
rem Option 3 - Use substring operations and logic operators
set "test=%var%."
if "%test:~0,1%" GEQ "0" if "%test:~0,1%" LEQ "9" set "test="
if defined test (
echo var does not start with a number
) else (
echo var starts with a number
)
rem Option 4 - Use findstr
rem This is SLOW as findstr needs to be executed
echo(%var%|findstr /b /r /c:"[0-9]" >nul && (
echo var starts with a number
) || (
echo var does not start with a number
)
I think this is the simplest way:
#echo off
setlocal EnableDelayedExpansion
set digits=0123456789
set var=1something
if "!digits:%var:~0,1%=!" neq "%digits%" (
echo First char is digit
) else (
echo First char is not digit
)
The first character of var is tried to be removed from digits string. If such a char was a digit, digits string change; otherwise, digits string remains the same.
#echo off
setlocal
set "the_string=a23something"
for /l %%a in (%the_string% ; 1 ; %the_string%) do set "cl_string=%%~a"
if %the_string:~0,1% neq 0 if "%cl_string%" equ "0" (
echo does not start with number
) else (
echo starts with number
)
endlocal
Another approach is with FINDSTR which eventually will be slower as it is an external for cmd.exe command.
#echo off
set "the_string=something"
echo %the_string%|findstr /b /r "[0-9]" >nul 2>&1 && (
echo starts with number
) || (
echo does not start with number
)
This will work in your situation:
echo %variable%|findstr "^[a-zA-Z]" >nul && echo it starts with an alpha character
Using findstr with regexp :
#echo off
set "$string=2toto"
echo %$string:~0,1%|findstr /i "^-*0*x*[0-9][0-9]*$">nul && echo is NUM || echo Is not NUM
in place of echo is NUM or echo is not NUM you can use a goto to redirect your script the way you want it.
#echo off
set "$string=2toto"
echo %$string:~0,1%|findstr /i "^-*0*x*[0-9][0-9]*$">nul && goto:isnum || goto:isnotnum
:isnum
echo is NUM
exit/b
:isnotnum
echo is not NUM
You have to set the string as a variable; in this way you are able to extract substrings from a main string. Here is an example:
#echo off
set EXAMPLESTRING=12345abcde
set FIRSTCHARACTERSTRING=%EXAMPLESTRING:~0,1%
The result of this short script should be 1 in this case.
Then, you can set a series of conditions to verify whether the first character is a number or not:
if %FIRSTCHARACTERSTRING%==0 goto NUMBER
if %FIRSTCHARACTERSTRING%==1 goto NUMBER
if %FIRSTCHARACTERSTRING%==2 goto NUMBER
if %FIRSTCHARACTERSTRING%==3 goto NUMBER
if %FIRSTCHARACTERSTRING%==4 goto NUMBER
if %FIRSTCHARACTERSTRING%==5 goto NUMBER
if %FIRSTCHARACTERSTRING%==6 goto NUMBER
if %FIRSTCHARACTERSTRING%==7 goto NUMBER
if %FIRSTCHARACTERSTRING%==8 goto NUMBER
if %FIRSTCHARACTERSTRING%==9 goto NUMBER
goto LETTER
:NUMBER
echo The first character is a number!
goto EOF
:LETTER
echo The first character is a letter!
goto EOF
Maybe this is not the most efficient solution but it works fine and it is easier to understand.

FOR /F issue running from a text file + counting up value

Here is something I'm struggeling with:
Title Scanning online computers: && set /a title_count=0
call :next
:next
FOR /F "tokens=1" %%a IN (workstation.txt) do (
title Scanning for online computers: %title_count% / %workstation%
ping -n 1 %%a | find "bytes=" >nul
set /a title_count=title_count+=1
if NOT ERRORLEVEL 1 (
set color=%%a && call includes\what.bat %color_pass% && echo %%a >> logs\reachable.txt
) else (
set color=%%a && call includes\what.bat %color_fail% && echo %%a >> logs\unreachable.txt && echo %%a, offline, % date%, %time% >> logs\offline.txt
)
)
The problem I have here is that the function TITLE is not getting updated while the variable %count_title% is counting up through the script.
set /a title_count+=1
doesn't work either
It's displayed as:
Scanning for online computers 0 / 5
Can sombody tell me what I'm doing wrong here?
Thanks in advance.
Illusion
Hi,
I've tried it the way as suggested:
It finishes the rest of the script when using the last GOTO :EOF
IT doesn't make sense to me, if I remove the last GOTO :eof, only the first row in workstation.txt is getting processed/parsed.
Scanning online computers: && set /a title_count+=1`
call :next
::added as possibly missing
GOTO :EOF
:next
FOR /F "tokens=1" %%a IN (workstation.txt) DO CALL :pingstation %%a
GOTO :EOF
:pingstation
title Scanning for online computers: %title_count% / %workstation%
ping -n 1 %1 | find "bytes=" >nul
set /a title_count+=1
if NOT ERRORLEVEL 1 (
set color=%1 && call includes\what.bat %color_pass% && echo %1 >> logs\reachable.txt
) else (
set color=%1 && call includes\what.bat %color_fail% && echo %1 >> logs\unreachable.txt && echo %1, offline, %date%, %time% >> logs\offline.txt
)
goto :eof
)
Read this: Environment variable expansion occurs when the command is read.
Salient points:
Your variables are expanded right when for command (and its entire body enclosed in parentheses) is parsed.
Use !VARNAME! instead of %VARNAME% to avoid it.
For better portability across OS versions/setups, it's a good idea to stick a setlocal EnableExtensions EnableDelayedExpansion at the beginning of your batch file.
Also, make sure there is a goto (e.g., goto :EOF) after call :next, because the code as posted will run through next one extra time.
You can go with setlocal EnableDelayedExpansion and changing the % syntax to the ! one when addressing vars inside the loop that are initialised within that very loop, just as atzz has suggested.
But there's a different approach. You can simply move the body of the loop to a(nother) subroutine. That way the variables would expand as expected.
Title Scanning online computers: && set /a title_count=0
call :next
::added as possibly missing
GOTO :EOF
:next
FOR /F "tokens=1" %%a IN (workstation.txt) DO CALL :pingstation %%a
GOTO :EOF
:pingstation
title Scanning for online computers: %title_count% / %workstation%
ping -n 1 %1 | find "bytes=" >nul
set /a title_count+=1
if NOT ERRORLEVEL 1 (
set color=%1 && call includes\what.bat %color_pass% && echo %1 >> logs\reachable.txt
) else (
set color=%1 && call includes\what.bat %color_fail% && echo %1 >> logs\unreachable.txt && echo %1, offline, %date%, %time% >> logs\offline.txt
)

Resources