When I run following script batch file I getting error ( was unexpected this time . Kindly suggest how to rectify this syntax error.
@echo off
setlocal enabledelayedexpansion
set "pad=000000000000000"
set "NeededSpace=%pad%32212254720"
set LOGFILE=%cd%\serverspace.txt
call :LOG > %LOGFILE%
exit /B
:LOG
for /F "delims=" %%i in (servers.txt) do (
for /f "delims== tokens=2" %%x in (
'wmic /user:"demoadmin" /password:"Admin8221" /node:"%%i" logicaldisk where "DeviceID='D:'" get FreeSpace /format:value'
) do for %%y in (%%x) do set "FreeSpace=%pad%%%y"
if "%FreeSpace:~-15%" geq "%NeededSpace:~-15%" (
echo Drive has at least 4 GB free space.
echo.
echo =========================================================
echo.
) else (
echo Not enough free space in D drive for %%i . Kindly contact support.
echo.
echo =========================================================
echo.
)
)
You've enabled delayed expansion at the top of the script. However, you're not using when you need to.
Take a look at this line:
if "%FreeSpace:~-15%" geq "%NeededSpace:~-15%" (
The problem is that FreeSpace
isn't given a value until the loop is running but %FreeSpace%
is evaluated/substituted when the FOR
loop is parsed . That means, that the command that gets evaluated when the loop is run has nothing for the first operand (I guess the quotes get eaten by the parser) and becomes:
if "~-15NeededSpace:~-15" (
which is clearly a malformed statement. Had you turned off @echo off
and not done the stdout redirection, call:LOG > %LOGFILE%
, you would have seen the line that cmd was complaining about.
Switching to using delayed expansion (using !
instead of %
), the script behaves correctly.
if "!FreeSpace:~-15!" geq "%NeededSpace:~-15%" (
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.