这可能超出了批处理文件的功能,但我正在尝试实现这一点:
http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting
调用需要填写用户名和密码提示的URL (我可以手动完成)。
响应看起来与此类似,对每个实例重复,但包含附加信息。我需要在“id”后找到一个特定的字符串,然后找到与之关联的“physicalId”。我对此有点不知所措,我怀疑这是否能用批处理文件完成,但我想我还是问一下以防万一。:
"audioEnabled":false,
"backupType":"CameraBackupDefault",
"controlEnabled":true,
"dewarpingParams":"{\"enabled\":false,\"fovRot\":0,\"hStretch\":1,\"radius\":0.5,\"viewMode\":\"1\",\"xCenter\":0.5,\"yCenter\":0.5}",
"disableDualStreaming":false,
"failoverPriority":"Medium",
"groupId":"",
"groupName":"",
"id":"{081c5328-ebc9-c717-2fad-6c219d4b265f}",
"licenseUsed":true,
"logicalId":"",
"mac":"00-0D-F1-2B-3A-E0",
"manuallyAdded":false,
"maxArchiveDays":-30,
"minArchiveDays":-1,
"model":"DWC-MF21M4TIR",
"motionMask":"5,0,0,44,32",
"motionType":"2",
"name":"Store Bathrooms",
"parentId":"{9336be48-010a-dfb4-1925-5a695352eaf8}",
"physicalId":"urn_uuid_32623361-6530-6665-6261-000df12b3ae0",
"preferredServerId":"{9336be48-010a-dfb4-1925-5a695352eaf8}",
"recordAfterMotionSec":5,
"recordBeforeMotionSec":5,
"scheduleEnabled":true,
"scheduleTasks":[
答案1
据我了解,您的目标是按顺序分离列表中的每个项目的 id 和物理 id,首先是 ID,然后是物理 ID,对于从 curl 命令的输出返回的每个摄像机。
@echo off
set "_tab= "
set "_login=YourUserNameHere:YourPasswordHere"
set "_cURL_cmd=curl -s -u %_login% http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
for /f tokens^=2-3delims^={}^,:^" %%i in ('%_cURL_cmd%^|findstr /l " id\"\:\"{ physicalId\":\""
')do if /i "%%~i" == "id" (echo\%_tab% %%i: %%j)else echo\%%i: %%j
1.使用for /f
带有多个分隔符的循环{},:"
,利用它们将从输出中删除,因此它们将部分用于删除输出,部分用作分隔符
2.使用findstr
分隔ID
和,其中仅列出包含和的physicalId
行,利用输出已经排序的事实,您可以按照相同的顺序输出。ID
physicalId
返回:
id: 081c5328-ebc9-c717-2fad-6c219d4b265f
physicalId: urn_uuid_32623361-6530-6665-6261-000df12b3ae0
id: 106562ad-06c6-ea42-7413-1e9b852812bc
physicalId: urn_uuid_333236313332-3636-2d33-3636-312d3330
id: 15e490d5-2b97-3bea-eba2-ad45a20a3c25
physicalId: urn_uuid_32616166-3836-6665-6130-000df12aaf86
id: 18e6bc2a-d7f2-d030-6cf0-1fd668124452
physicalId: urn_uuid_32623035-6332-6666-3064-000df12b05c2
id: 1d884232-c33c-922d-61c5-d0f568d0ffc7
physicalId: urn_uuid_32623036-3237-6666-6137-000df12b0627
id: 1e3b120b-0894-6631-b831-513a8dfc9bbe
physicalId: urn_uuid_32623361-3664-6666-3264-000df12b3a6d
id: 2b798b32-86f3-3c45-4a1c-daab08f5b138
physicalId: urn_uuid_32616166-3331-6665-6635-000df12aaf31
观察1:如果不需要输出字符串(ID
和physicalID
),则从2-
命令中删除token^=2-3
、和:%%j
echo\%%i %%j
for /f tokens^=3delims^={}^,:^" %%i in ('%_cURL_cmd%^|findstr /l " id\":\"{ physicalId\":\""')do echo\%%i
返回:
081c5328-ebc9-c717-2fad-6c219d4b265f
urn_uuid_32623361-6530-6665-6261-000df12b3ae0
106562ad-06c6-ea42-7413-1e9b852812bc
urn_uuid_333236313332-3636-2d33-3636-312d3330
15e490d5-2b97-3bea-eba2-ad45a20a3c25
urn_uuid_32616166-3836-6665-6130-000df12aaf86
18e6bc2a-d7f2-d030-6cf0-1fd668124452
urn_uuid_32623035-6332-6666-3064-000df12b05c2
1d884232-c33c-922d-61c5-d0f568d0ffc7
urn_uuid_32623036-3237-6666-6137-000df12b0627
1e3b120b-0894-6631-b831-513a8dfc9bbe
urn_uuid_32623361-3664-6666-3264-000df12b3a6d
2b798b32-86f3-3c45-4a1c-daab08f5b138
urn_uuid_32616166-3331-6665-6635-000df12aaf31
观察:2。从那里,您可以获得更多代码用途,例如,找到physicalID
特定相机的ID
:
@echo off
setlocal enabledelayedexpansion
set "_login=YourUserNameHere:YourPasswordHere"
set "_ID=081c5328-ebc9-c717-2fad-6c219d4b265f"
set "_cURL_cmd=curl -s -u %_login% http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
for /f tokens^=3delims^={}^,:^" %%i in ('!_cURL_cmd!^|findstr /l " id\":\"{%_ID% physicalId\":\""'
)do if not "!_CAM_loop!" == "%_ID%" (set "_CAM_loop=%%~i")else set "_physical_ID=%%~i" && goto %:^)
%:^)
echo\!_ID! & echo\!_physical_ID! & timeout -1 | endlocal
通过将 添加ID
到findstr
,您就有了列出的每个,并且在给定 之后ID
仅包含所需的,因此在每个循环中,将 设置为当前的,并且在下一个循环中,如果 匹配(在上一个循环中定义),则将您的 设置为当前循环并退出。physicalID
ID
_CAM_loop
ID/%%j
_CAM_ID
physicalID
%%j
for
观察:3。反过来,找到ID
某个physicalID
相机的:
@echo off
setlocal enabledelayedexpansion
set "_login=YourUserNameHere:YourPasswordHere"
set "_physicalId=urn_uuid_32623361-6530-6665-6261-000df12b3ae0"
set "_cURL_cmd=curl -s -u %_login% http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
for /f tokens^=3delims^={}^,:^" %%i in ('!_cURL_cmd!^|findstr /l " id\":\"{ physicalId\":\"%_physicalId%"'
)do if not "%%~i" == "%_physicalId%" (set "_ID=%%~i")else goto %:^)
%:^)
echo\!_ID! & echo\!_physicalId! & timeout -1 | endlocal & goto :eOf
通过将physicalID
字符串添加到findstr
,将列出所有出现的 ID,并在 中逐一定义_ID
,并可在下一个循环中进行比较,然后在与 匹配的循环中physicalID
,id 将已经被设置,因此,只需退出循环而不设置ID
为 中的当前字符串%%j
。
观察:4。要physicalID's
通过提供多个来获得多个相机,只需通过使用计数器调用函数/标签并在定义它们时将其作为变量递增来IDs
适应使用附加循环。for
@echo off
setlocal enabledelayedexpansion
set "_login=YourUserNameHere:YourPasswordHere"
set "_cURL_cmd=curl -s -u !_login! http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
set "_Multiples_IDs=cbea4225-934b-2344-ed37-13aa31295d58,081c5328-ebc9-c717-2fad-6c219d4b265f"
2>nul set /a "_cnt-=!_cnt!" & for /l %%L in (1 1 999)do set "_physical_ID_%%~L=" && set "_ID_%%~L="
for %%i in (%_Multiples_IDs%)do set /a "_cnt+=1+0" && call %:^] !_cnt! "%%~i" "!_cURL_cmd!" && call set "_ID_!_cnt!=%%~i"
%:^]
if not "%~1%~2" == "" for /f tokens^=3delims^={}^,:^" %%i in ('%~3^|findstr /l " id\":\"{%~2 physicalId\":\""'
)do if not "!_CAM_loop!" == "%~2" (set "_CAM_loop=%%~i")else set "_physical_ID_%~1=%%~i" && exit /b
%:^)
for /l %%L in (1 1 999)do if not "!_physical_ID_%%~L!" == "" (
echo\Variable name: _ID_%%~L == !_ID_%%~L!
echo\variable name: _physical_ID_%%~L == !_physical_ID_%%~L!
)
timeout -1 | endlocal & goto :eOf
为了进行乘法,并为出现的情况定义一个变量(可递增),只需在同一个变量中定义多个 ID,并使其适应计数器和函数的使用,其中计数器将用于按照与输出相同的顺序逐一存储字符串。
观察:5。相反,ID's
从您的相机中获取各种各样的东西,提供几个PhysicalID's
,只需使用额外的for
循环调用使用计数器的函数/标签并在定义它们时作为变量递增,类似的东西,Obs.3 与 Obs. 的交界处:4 d:)
@echo off
setlocal enabledelayedexpansion
set "_login=YourUserNameHere:YourPasswordHere"
set "_cURL_cmd=curl -s -u !_login! http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
set "_Multiples_physicalIDs=urn_uuid_32623361-6530-6665-6261-000df12b3ae0,00-1C-A6-02-97-1D"
2>nul set /a "_cnt-=!_cnt!" & for /l %%L in (1 1 999)do set "_physical_ID_%%~L=" && set "_ID_%%~L="
for %%i in (%_Multiples_physicalIDs%)do set /a "_cnt+=1+0" && call %:^] !_cnt! "%%~i" "!_cURL_cmd!" && call set "_physical_ID_!_cnt!=%%~i"
%:^]
if not "%~1%~2" == "" for /f tokens^=3delims^={}^,:^" %%i in ('%~3 ^|findstr /l " id\":\"{ physicalId\":\"%~2"'
)do if not "%%~i" == "%~2" (set "_ID_%~1=%%~i") else exit /b
%:^)
for /l %%L in (1 1 999)do if not "!_physical_ID_%%~L!" == "" (
echo\Variable name: _ID_%%~L == !_ID_%%~L!
echo\variable name: _physical_ID_%%~L == !_physical_ID_%%~L!
)
timeout -1 | endlocal & goto :eOf
观察:6。要使用 bat / cmd 中的用户输入获取多个 fysicalID,我可以建议您对我的答案的改编进行改编,该改编在观察项目 4 的改编中指出了我对您的问题的回答,这反过来又考虑了您在另一个答案 OP 中提到的评论,对我对您的问题的回答第 2 项的评论,或者类似令人困惑的内容,所以我的英语水平有限,无法让我发展出更清晰的想法 d;)
@echo off
setlocal enabledelayedexpansion
set "_curl_login=username:password"
for %%i in (Old,New)do set /p "_%%~i_Cam_ID=Enter %%~i Cam ID: "
set "_cURL_cmd=curl -s -u !_curl_login! http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
for %%i in (Old,New)do call %:^] "%%~i" !_%%~i_Cam_ID! "!_cURL_cmd!"
%:^]
if not "%~1%~2" == "" for /f tokens^=3delims^={}^,:^" %%i in ('%~3 ^|findstr /l " id\":\"{%~2 physicalId\":\""'
)do if not "!_CAM_loop!" == "%~2" (set "_CAM_loop=%%~i") else set "_%~1_physical_ID=%%~i" && exit /b
%:^)
for %%i in (Old,New)do echo\Variable name: _%%~i_Cam_ID == !_%%~i_Cam_ID! && (
echo\variable name: _%%i_physical_ID == !_%%i_physical_ID!
)
echo\ & timeout -1 | <con: echo\Press Any Key To Exit or F13 To Continue d;) & endlocal & goto :eOf
字符串输入输出:
Tested line 5101: d8e6a403-ccb7-4ded-cd9f-b67aa7e397e3
Tested line 5113: 00-1C-A6-02-97-1D
Tested line 4909: cbea4225-934b-2344-ed37-13aa31295d58
Tested line 4921: urn_uuid_32633161-6331-6665-6638-000df12c1ac1
输出:
Enter Old Cam ID: d8e6a403-ccb7-4ded-cd9f-b67aa7e397e3
Enter New Cam ID: cbea4225-934b-2344-ed37-13aa31295d58
Variable name: _Old_Cam_ID == d8e6a403-ccb7-4ded-cd9f-b67aa7e397e3
variable name: _Old_physical_ID == 00-1C-A6-02-97-1D
Variable name: _New_Cam_ID == cbea4225-934b-2344-ed37-13aa31295d58
variable name: _New_physical_ID == urn_uuid_32633161-6331-6665-6638-000df12c1ac1
Press Any Key To Exit or F13 To Continue d;)
观察:7。您还可以使用For /L
(有条件地执行针对一定范围的数字的命令):
@echo off
setlocal enabledelayedexpansion
set "_curl_login=username:password"
for /l %%L in (0 1 9)do set "_Cam_ID_#%%~L=" & set "_physical_ID_#%%~L="
for /l %%L in (0 1 9)do set /p "_Cam_ID_#%%~L= Enter Camera ID #%%~L: "
set "_cURL_cmd=curl -s -u !_curl_login! http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
for /l %%L in (0 1 9)do call %:^] "#%%~L" !_Cam_ID_#%%~L! "!_cURL_cmd!"
%:^]
if not "%~1%~2" == "" for /f tokens^=3delims^={}^,:^" %%i in ('%~3 ^|findstr /l " id\":\"{%~2 physicalId\":\""'
)do if not "!_CAM_loop!" == "%~2" (set "_CAM_loop=%%~i")else set "_physical_ID_%~1=%%~i" && exit /b
%:^)
for /l %%L in (0 1 9)do echo\Variable name: _Cam_ID_#%%~L == !_Cam_ID_#%%~L! && (
echo\variable name: _physical_ID_#%%~L == !_physical_ID_#%%~L!
)
echo\ & timeout -1 | <con: echo\Press Any Key To Exit or F13 To Continue d;) & endlocal & goto :eOf
返回:
Enter Cam ID #0: 1e3b120b-0894-6631-b831-513a8dfc9bbe
Enter Cam ID #1: 2f1c08ba-d44e-c96c-c9b9-d0172c8a44c8
Enter Cam ID #2: 638b66d6-683a-4fb3-31ea-90d8d0e44d10
Enter Cam ID #3: 760ed327-d2fe-802d-7574-83c37e3292ba
Enter Cam ID #4: 80da2ce7-ef02-1c38-7395-a786bac1b236
Enter Cam ID #5: aa3acc69-12b7-6045-f9b7-eaa7b54fc3f8
Enter Cam ID #6: cbea4225-934b-2344-ed37-13aa31295d58
Enter Cam ID #7: e5a72a3f-9bf1-6e6d-22ca-7084b91a228f
Enter Cam ID #8: e8290e6a-343e-2e14-8f37-0343aebbd797
Enter Cam ID #9: e8bc0e2c-97cb-94f4-493b-37f79122184e
Variable name: _Cam_ID_#0 == 1e3b120b-0894-6631-b831-513a8dfc9bbe
variable name: _physical_ID_#0 == urn_uuid_32623361-3664-6666-3264-000df12b3a6d
Variable name: _Cam_ID_#1 == 2f1c08ba-d44e-c96c-c9b9-d0172c8a44c8
variable name: _physical_ID_#1 == urn_uuid_32616166-3933-6665-3933-000df12aaf93
Variable name: _Cam_ID_#2 == 638b66d6-683a-4fb3-31ea-90d8d0e44d10
variable name: _physical_ID_#2 == urn_uuid_32616137-6233-6665-3762-000df12aa7b3
Variable name: _Cam_ID_#3 == 760ed327-d2fe-802d-7574-83c37e3292ba
variable name: _physical_ID_#3 == urn_uuid_32306138-3735-6665-6332-000df120a875
Variable name: _Cam_ID_#4 == 80da2ce7-ef02-1c38-7395-a786bac1b236
variable name: _physical_ID_#4 == urn_uuid_32616165-6439-6665-3465-000df12aaed9
Variable name: _Cam_ID_#5 == aa3acc69-12b7-6045-f9b7-eaa7b54fc3f8
variable name: _physical_ID_#5 == urn_uuid_32303837-3834-6665-6434-000df1208784
Variable name: _Cam_ID_#6 == cbea4225-934b-2344-ed37-13aa31295d58
variable name: _physical_ID_#6 == urn_uuid_32633161-6331-6665-6638-000df12c1ac1
Variable name: _Cam_ID_#7 == e5a72a3f-9bf1-6e6d-22ca-7084b91a228f
variable name: _physical_ID_#7 == 00-1C-A6-02-9A-EF
Variable name: _Cam_ID_#8 == e8290e6a-343e-2e14-8f37-0343aebbd797
variable name: _physical_ID_#8 == urn_uuid_32306137-3966-6665-3939-000df120a79f
Variable name: _Cam_ID_#9 == e8bc0e2c-97cb-94f4-493b-37f79122184e
variable name: _physical_ID_#9 == urn_uuid_333236313331-3633-2d33-3733-392d3330
Press Any Key To Exit or F13 To Continue d;)
进一步阅读:
答案2
请看看这是否符合您的需求:
@echo off
:: Fill this fields with your username and password:
set Username=
set Password=
::
for /f tokens^=^2^,^3^ delims^=^"^: %%a in ('curl -s -u %Username%:%Password% http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting') do (
if /i "%%~a"=="id" set ID=%%~b
if /i "%%~a"=="physicalId" set PhysicalID=%%~b
)
echo ID: %ID%
echo PhysicalID: %PhysicalID%
echo.
pause
答案3
结合我所做的一切,这是测试和工作。想对你的帮助说声谢谢,我永远无法独自解决这个问题。我能够剖析大部分代码并理解它,尽管仍有一些事情让我感到困惑……
@echo off
REM Last Edited 01/15/22 03:28PM
setlocal enabledelayedexpansion
set "_curl_login=username:Password"
for %%i in (Old,New)do set /p "_%%~i_Cam_ID=Enter %%~i Cam ID: "
set "_cURL_cmd=curl -s -u !_curl_login! http://127.0.0.1:7001/ec2/getCamerasEx?extraFormatting"
set "_cURL_rebuild_cmd=curl -s -u !_curl_login! http://127.0.0.1:7001/api/rebuildArchive?action=start&mainPool=1"
for %%i in (Old,New)do call %:^] "%%~i" !_%%~i_Cam_ID! "!_cURL_cmd!"
%:^]
if not "%~1%~2" == "" for /f tokens^=3delims^={}^,:^" %%i in ('!_cURL_cmd!^|findstr /l " id\":\"{%~2 physicalId\":\""'
)do if not "!_CAM_loop!" == "%~2" (set "_CAM_loop=%%~i") else set "_%~1_physical_ID=%%~i" && exit /b
%:^)
for %%i in (Old,New)do echo Cam ID !_%%~i_Cam_ID! matches Physical ID !_%%i_physical_ID!
for /F "skip=1" %%F in ('wmic LogicalDisk WHERE DriveType^=3 GET DeviceID ^| findstr [A-Z]') do (
echo Checking Drive %%F%...
set oldDirectory=%%F%\DW Spectrum Media\low_quality\!_1_physical_ID!
set newDirectory=%%F%\DW Spectrum Media\low_quality\!_2_physical_ID!
if exist "!oldDirectory!" (
echo Processing Drive %%F% ...
if exist "!oldDirectory!\info.txt" del /q "!oldDirectory!\info.txt" > null
if exist "!newDirectory!\info.txt" move "!newDirectory!\info.txt" "!oldDirectory!" > null
if exist "!newDirectory!" rmdir /S /Q "!newDirectory!" > null
if exist "!oldDirectory!" rename "!oldDirectory!" !_2_physical_ID! > null
)
set oldDirectory=%%F%\DW Spectrum Media\hi_quality\!_1_physical_ID!
set newDirectory=%%F%\DW Spectrum Media\hi_quality\!_2_physical_ID!
if exist "!oldDirectory!" (
if exist "!oldDirectory!\info.txt" del /q "!oldDirectory!\info.txt" > null
if exist "!newDirectory!\info.txt" move "!newDirectory!\info.txt" "!oldDirectory!" > null
if exist "!newDirectory!" rmdir /S /Q "!newDirectory!" > null
if exist "!oldDirectory!" rename "!oldDirectory!" !_2_physical_ID! > null
)
)
set /p Rebuild=Done, Rebuild Archive? ([Y]/N)
if /i not "%Rebuild%" == "N" (
echo Initiating Archive Rebuild...
!_cURL_rebuild_cmd! >null
) else (
echo Skipping Archive Rebuild...
)
echo\ & timeout -1 | <con: echo\Press Any Key To Exit
if exist null del null
endlocal
exit