我試圖在FOR循環中使用子字符串操作,我無法讓它爲愛或金錢工作。我被告知,我們不能在循環變量(%% f等)上使用子字符串操作,所以你必須設置另一個變量等於循環(set MyVariable = %% f),然後使用子字符串選項在這個派生物上工作(設置MyOtherVar =%MyVariable:〜0,-3%)。但是,當回顯這些變量時,只有循環變量設置爲/非空。FOR循環中的批處理文件字符串管理
這是我目前使用的代碼。
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET OutPref="373137#"
SET OutSuf1="#Window Clean POD "
SET OutSuf2="#1520755.pdf"
SET [email protected]
cd c:\Oscar\Scripts\FileNamer\Inbound\
for /f "tokens=*" %%f in ('dir /b *.pdf') do (
c:\Oscar\Scripts\FileNamer\pdftotext.exe -q %%f
set InPdfVer=%%f
set InTxtVer=%InPdfVer:~0,-3%txt
echo Loop Val= %%f
echo InPdfVer= %InPdfVer%
echo InTxtVer= %InTxtVer%
pause
set InAddLine=findstr %MatchStr% %InTxtVer%
set stemp=%InAddLine%
set pos=0
:loop
set /a pos+=1
echo %stemp%|findstr /b /c:"%MatchStr%" >NUL
if errorlevel 1 (
set stemp=%stemp:~1%
if defined stemp GOTO loop
set pos=0
)
set /a pos-=3
call set StoreNo=%InAddLine:~%pos%,-25%
call:getvalue C:\Oscar\Scripts\FileNamer\StoreList.inf %StoreNum% StoreName
set OutFile=%OutPerf%%StoreNo%%OutSuf1%%StoreName%%OutSuf2%
move %%f c:\Oscar\Scripts\FileNamer\Outbound\%OutFile%
)
cd c:\Oscar\Scripts\FileNamer\
exit 0
:getvalue
rem This function reads a value from a file and stored it in a variable
rem %1 = name of file to search in
rem %2 = search term to look for
rem %3 = variable to place search result
FOR /F "tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~3=%%~j
goto:eof
希望這是有道理的,可以嘗試和解釋它。很有可能底部不工作,但沒有得到那麼多!
感謝您的任何想法,作爲一般概述,腳本應該在傳入文件夾中將PDF文件轉換爲文本,在該文件中搜索電子郵件地址,在外部列表中查找該電子郵件地址然後移動PDF文件(在流程中以重要慣例重命名該文件),然後循環移至下一個文件,直到匹配文件結束。
親切的問候, 奧斯卡
行,所以它的休息似乎什麼什麼,現在應該,但我仍然不能得到這個子串來設置,我剛剛結束了在decendent可變整個字符串。這是新代碼(請原諒用於故障排除的暫停和回聲)。
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET OutPref=373137#
SET OutSuf1=#Window Clean POD
SET OutSuf2=#1520755.pdf
SET [email protected]
cd c:\Oscar\Scripts\FileNamer\Inbound\
for /f "tokens=*" %%f in ('dir /b *.pdf') do (
c:\Oscar\Scripts\FileNamer\pdftotext.exe -q %%f
set InPdfVer=%%f
call set InTxtVer=!InPdfVer:~0,-3!txt
for /f "tokens=*" %%x in ('findstr !MatchStr! !InTxtVer!') do set InAddLine=%%x
call:getpos
echo !pos!
pause
call set StoreNo=!InAddLine:~!pos!,-25!
call:getvalue C:\Oscar\Scripts\FileNamer\StoreList.inf !StoreNum! StoreName
echo OutPerf !OutPref!
echo StoreNo !StoreNo!
echo OutSuf1 !OutSuf1!
echo StoreName !StoreName!
echo Outsuf2 !OutSuf2!
set OutFile=!OutPerf!!StoreNo!!OutSuf1!!StoreName!!OutSuf2!
echo %%f !OutFile!
pause
REM move %%f c:\Oscar\Scripts\FileNamer\Outbound\!OutFile!
)
cd c:\Oscar\Scripts\FileNamer\
exit /b
:getpos
set stemp=!InAddLine!
set pos=0
:loop
set /a pos+=1
echo !stemp!|findstr /b /c:"!MatchStr!" >NUL
if errorlevel 1 (
set stemp=!stemp:~1!
if defined stemp GOTO loop
set pos=0
)
set /a pos-=3
goto:eof
:getvalue
rem This function reads a value from a file and stored it in a variable
rem %1 = name of file to search in
rem %2 = search term to look for
rem %3 = variable to place search result
FOR /F "tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~3=%%~j
goto:eof
您需要查看Delayed Expansion。 block中的變量應該使用'!'not'%'引用,即'!InPdfVer:〜0,-3!'等。 – Compo
另外 - 你不能在代碼塊中使用像'loop'這樣的標籤括號內的一系列命令) – Magoo
阿哈!已經查詢了子串操縱了很長時間,並沒有看到這個問題,事實證明,這是延遲的擴張,它搞砸了。我認爲它不會在被引用之前擴展%% f類型變量,因爲變量需要根據for循環的不同而不同。 – Xeotech