我是批處理檔案的新手,我需要使用批處理從單行 txt 檔案中提取 URL 的幫助。
這是卷曲命令:
curl -X "GET" %runone% -H "accept: application/json" -o runone.txt
存盤在檔案 (runone.txt) 中的輸出是這樣的一行:
{"meta":{"terms-and-conditions":{"href":"https://apps.censored.int/datasets/licences/general/"},"license":"CC-BY-4.0","copyright":"censored"},"data":{"link":{"href":"https://apps.censored.int/webapps/opencharts/content/20220620211737-879c62d3db4f29947daaf8140a3f9261a35e4c5b.png","type":"image/png"},"attributes":{"description":"","name":"opencharts","title":"Chart"},"type":"graphical_product"},"tracker":"tracker-dd80b7a5299e46ef8aa8de4041b12aeb","uid":""}
我只想從 runone.txt 中選擇png 檔案的 URL(在此示例中:https ://apps.censored.int/webapps/opencharts/content/20220620211737-879c62d3db4f29947daaf8140a3f9261a35e4c5b.png )并將其存盤到變數批量。
謝謝你的幫助。
uj5u.com熱心網友回復:
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q72693254.txt"
SET "pngurl="&FOR /f "usebackqtokens=11,*delims={:}" %%b IN ("%filename1%") DO FOR %%e IN (%%c) DO IF NOT DEFINED pngurl SET "pngurl=%%~e"
ECHO pngurl=%pngurl%
GOTO :EOF
注意,如果檔案名中不包含空格等分隔符,則%filename1%不需要加引號,然后usebackq可以省略。
解決方案取決于所描述的輸出檔案的格式。
uj5u.com熱心網友回復:
您可以嘗試使用 Powershell 和 Batch 決議 JSON 檔案并從中提取鏈接,如下面的代碼:
@echo off
Title Parse JSON File And Exctract A Link From It Using Powershell And Batch
Set "JSON_File=%~dp0runone.txt"
set psCmd="&{(GC "%JSON_File%" | ConvertFrom-Json).data.link.href}"
Call :RunPS %psCmd% URL
Echo "%URL%"
pause & Exit
::-------------------------------------------------------------------
:RunPS <PassPSCMD> <RetValue>
@for /F "usebackq tokens=*" %%i in (`Powershell %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
::-------------------------------------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/494643.html
