所以我创建了一个程序,将数字和字母分成2个不同的变量,因此它将“word1234”变成包含“word”的变量和包含“1234”的变量,我所做的是制作一个贯穿于变量“信息”逐字母。 “这是一个数字吗?” “不”,“这是一封信吗?” “是”。当变量“toggle”仍然在值0时,它将继续将字母附加到“weatherd”变量,并将其附加到“temperature”变量。 注意:我知道我的变量和标签名称都不好,我在大约半秒内拿出它们:) 因此它检查它是否是一个数字,如果它是“toggle”变量变为1并且它开始将其余的文本放入一个新变量(“temperature”)。我想知道,一旦它击中数字它会给我一个错误和崩溃,为什么?还是有比我正在做的更简单的方法? 对于大量的帖子感到抱歉,我添加了一些暂停和调试的东西,代码在这里:
REM PREPARE FOR A MASSIVE AMOUNT OF CODE, all this stuff just gets the current weather, ignore it
@echo off
set "wherestay=%cd%"
cd /d C:
cd\
cd "C:/Users/%USERNAME%/Downloads"
del weather.txt
start chrome.exe teamhaxor.netau.net/getWeather.php
set times=0
:loading
set /a times=%times% + 1
if %times% == 2000 goto failed
if not exist weather.txt goto loading
cd /d C:
cd\
cd "C:/Users/%USERNAME%/Downloads"
< weather.txt (
set /p info=
)
REM PROBLEM STARTS HERE
setlocal enabledelayedexpansion
@echo on
set tempery=0
set "weatherd="
set "temperat="
set toggle=0
:loopy
set "char=!info:~%tempery%, 1!"
if "%char%" EQU "0" set toggle=1
if "%char%" EQU "1" set toggle=1
if "%char%" EQU "2" set toggle=1
if "%char%" EQU "3" set toggle=1
if "%char%" EQU "4" set toggle=1
if "%char%" EQU "5" set toggle=1
if "%char%" EQU "6" set toggle=1
if "%char%" EQU "7" set toggle=1
if "%char%" EQU "8" set toggle=1
if "%char%" EQU "9" set toggle=1
pause
if %toggle% EQU 0 (set weatherd=%weatherd%%char%) ELSE (set temperat=%temperat%%char%)
pause
set /a tempery=%tempery% + 1
pause
if %tempery% EQU 15 (goto out) ELSE (goto loopy)
:out
echo %weatherd%
echo %temperat%
setlocal disabledelayedexpansion
REM PROBLEM STOPS HERE
goto afterafterlol
:failed
set weatherd=Failed
set temperat=Failed
:afterafterlol
cd /d C:
cd\
cd %wherestay%
echo %weatherd%
echo %temperat%
pause >Nul
答案 0 :(得分:0)
要直接回答您的问题,您可以更轻松地分割您的值,以便最终在一个变量中使用alpha,在另一个变量中使用数字。
@echo off & setlocal
set "var=Thunderstorms87"
for /f "delims=0123456789" %%I in ("%var%") do set "condition=%%~I"
call set "temperature=%%var:%condition%=%%"
echo %condition%
echo %temperature%
从更广泛的意义上讲,您的项目似乎是一种非常繁琐的方法,可以抓取大量有用的数据。最好查询提供API的天气服务,该API具有不需要拆分字符串或其他平面文本黑客的结构化响应。
这是一个使用Weather Underground's API,检索和解析其JSON响应的示例脚本。您需要创建一个帐户并注册一个免费的API密钥,但是您可以获得更多数据,并且解析起来更加容易。而且无需按照这种方式生成Chrome窗口来获取数据。
@if (@CodeSection == @Batch) @then
@echo off & setlocal
rem // weather.bat
rem // https://stackoverflow.com/a/44015647/1683264
rem // go to https://www.wunderground.com/weather/api/ for an API key
set "APIkey=pasteyourAPIkeyhere"
if "%~1"=="" ( set "location=autoip" ) else set "location=%~1"
cscript /nologo /e:JScript "%~f0" "%APIkey%" "%location%"
rem // end main runtime
goto :EOF
@end // end batch / begin JScript chimera
var x = WSH.CreateObject("Microsoft.XMLHTTP"),
htmlfile = WSH.CreateObject('htmlfile'),
APIkey = WSH.Arguments(0),
loc = WSH.Arguments(1),
URL = 'http://api.wunderground.com/api/' + APIkey + '/conditions/q/' + loc + '.json',
JSON = obj = {}, pad = ' ';
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
htmlfile.close(JSON = htmlfile.parentWindow.JSON);
x.open("GET", URL, true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState!=4) {WSH.Sleep(50)};
response = JSON.parse(x.responseText).current_observation;
obj = {
"station": response.observation_location.full,
"timestamp": response.observation_time,
"conditions": response.weather,
"temperature": response.temperature_string,
"humidity": response.relative_humidity,
"wind": response.wind_string,
"heat index": response.heat_index_string,
"wind chill": response.windchill_string,
"forecast URL": response.forecast_url
};
for (var i in obj) while (i.length > pad.length) pad += ' '
for (var i in obj) {
var key = (i + pad).substring(0, pad.length);
WSH.Echo(key + ' : ' + obj[i]);
}
输出看起来像这样:
您可以使用以下内容:
WSH.Echo(x.responseText);
...查看原始JSON并查看Feed中包含的所有其他数据。您还可以修改网址,并将conditions
替换为forecast
,hourly
和bunch of other options(以及替换current_observation
和response
适当的对象属性。)