bat 擷取ini檔內的字串

config.ini
--------------------------------
#Settings
OUTDIR=E:\Videos\
VIDENC=-c:v libx264 -profile:v high -preset veryslow -x264opts crf=18:ref=4:bframes=5
AUDENC=-c:a libvo_aacenc -b:a 128k -ac 2 -strict -2
--------------------------------

請各前輩指導一下哪邊有誤
找到 "VIDENC" 後,擷取 最左邊 "=" 右邊 的完整字串
"-c:v libx264 -profile:v high -preset veryslow -x264opts crf=18:ref=4:bframes=5"


cd/d "%~dp0"
:readconfig
set keyword=VIDENC
for /f "eol=# delims=" %%a in (config.ini) do (
call :split %%a title params
if "%title%"=="%keyword%" (
echo title: %title%
echo params: %params%
)
)
pause
goto :exit

:split
set str=%~1
set str1=
set str2=
set /a length=0
:loop
if defined str (
set str=%str:~1%
set str1=%str1%%str:~0,1%
set str2=%str:~2%
set /a length+=1
if not "%str:~1,1%"=="=" goto loop
)
set %2=%str1%
set %3=%str2%
goto :eof
:exit
2013-12-05 3:45 發佈
文章關鍵字 bat ini檔 字串
試試看

@echo off
set keyword=VIDENC
for /f "delims=" %%a in ('type config.ini^|findstr / "%keyword%"') do (
set za=%%a
set znew=%za:~7%
set z
pause
)
John wrote:
試試看@echo o...(恕刪)

非常感謝
去查了一下 findstr 用法,總算解決了
但還是有兩個問題

1.

FOR %variable IN (set) DO 命令 [command-parameters]
在 do (...) 裡面寫 if、set 好像很容易錯誤,這是BUG嗎? 改成下面這樣就沒問題了。

@echo off
set keyword=VIDENC
for /f "delims=" %%a in ('type config.ini^|findstr / "\<%keyword%="') do (
set za=%%a
)
set znew=%za:~7%
set z
pause



2.
config.ini
--------------------------------
#Settings
ITEM1_NAME=ITEM1_VALUE
ITEM2_NAME=ITEM2_VALUE
ITEM3_NAME=ITEM3_VALUE
--------------------------------

ITEM_NAME不一定都是6個字元,ITEM_VALUE裡面可能也包含 "="
下面的寫法好像太複雜,有沒有比較簡單的方式可以分割字串

@echo off & cd/d "%~dp0"
:readconfig
set keyword=VIDENC
for /f "eol=# delims=" %%a in ('type config.ini^|findstr / "\<%keyword%="') do (
call :split "%%a" title params
)
:break
echo title: %title%
echo params: %params%
pause

:split
set str=%~1
set str1=
set str2=
set /a length=0
:loop
if defined str (
set str=%str:~1%
set str1=%str1%%str:~0,1%
set str2=%str:~2%
set /a length+=1
if not "%str:~1,1%"=="=" goto loop
)
set %2=%str1%
set %3=%str2%
goto :eof



kingdragon wrote:
在 do (...) 裡面寫 if、set 好像很容易錯誤,這是 BUG 嗎? 改成下面這樣就沒問題了。

首先,先說聲抱歉,這是一個 BUG,一個人的 BUG,其實是我漏打了一個 I ,這是 findstr 的參數,用於尋找時,不區分大小寫
for /f "delims=" %%a in ('type config.ini^|findstr /i "%keyword%"') do (

kingdragon wrote:
ITEM_NAME 不一定都是 6 個字元,ITEM_VALUE 裡面可能也包含 "="
下面的寫法好像太複雜,有沒有比較簡單的方式可以分割字串

試試看

@echo off
setlocal enabledelayedexpansion
set keyword=testtesttest
for /f "delims=" %%a in ('type config.ini^|findstr /i "%keyword%"') do (
set za=%%a
set znew=!za:%keyword%=!
set znew2=!znew:~1!
@echo =============== Result ===============
set z
)

config.ini
--------------------------------
#Settings
OUTDIR=E:\Videos\
WIDTH=720
VIDENC=-c:v libx264 -profile:v high -preset veryslow -x264opts crf=18:ref=4:bframes=5
AUDENC=-c:a libvo_aacenc -b:a 128k -ac 2 -strict -2
--------------------------------

@echo off & cd/d "%~dp0"
setlocal enabledelayedexpansion
call readconfig.bat AUDENC ACODEC
call readconfig.bat VIDENC VCODEC
call readconfig.bat WIDTH RWIDTH
set VFILTERS=-vf scale='%RWIDTH%:trunc((%RWIDTH%*1/1/dar)/16+0.5)*16'
echo FFmpeg -i video.mkv %ACODEC% %VFILTERS% %VCODEC% video.mp4
echo.
call readconfig2.bat AUDENC ACODEC
call readconfig2.bat VIDENC VCODEC
call readconfig2.bat WIDTH RWIDTH
set VFILTERS=-vf scale='%RWIDTH%:trunc((%RWIDTH%*1/1/dar)/16+0.5)*16'
echo FFmpeg -i video.mkv %ACODEC% %VFILTERS% %VCODEC% video.mp4
pause

kingdragon wrote:
在 do (...) 裡面寫 if、set 好像很容易錯誤,這是 BUG 嗎?



:readconfig
set keyword=%~1
for /f "eol=# delims=" %%a in ('type config.ini^|findstr /i "\<%keyword%="') do (
set za=%%a
)
set znew=!za:%keyword%=!
set znew2=!znew:~1!
set %2=%znew2%
goto :eof

:readconfig2
set keyword=%~1
for /f "eol=# delims=" %%a in ('type config.ini^|findstr /i "\<%keyword%="') do (
set za=%%a
set znew=!za:%keyword%=!
set znew2=!znew:~1!
set %2=%znew2%
)
goto :eof

readconfig2 看不出哪裡有問題,這是鬼打牆?
kingdragon wrote:
readconfig2 看不出哪裡有問題,這是鬼打牆?


kingdragon wrote:
:readconfig2
set keyword=%~1
for /f "eol=# delims=" %%a in ('type config.ini^|findstr /i "\<%keyword%="') do (
set za=%%a
set znew=!za:%keyword%=!
set znew2=!znew:~1!
set %2=%znew2%

改成這樣看看
set %2=!znew2!
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?