2017-03-07 180 views
0

我試圖在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 
+4

您需要查看Delayed Expansion。 block中的變量應該使用'!'not'%'引用,即'!InPdfVer:〜0,-3!'等。 – Compo

+3

另外 - 你不能在代碼塊中使用像'loop'這樣的標籤括號內的一系列命令) – Magoo

+0

阿哈!已經查詢了子串操縱了很長時間,並沒有看到這個問題,事實證明,這是延遲的擴張,它搞砸了。我認爲它不會在被引用之前擴展%% f類型變量,因爲變量需要根據for循環的不同而不同。 – Xeotech

回答

0

感謝您的所有輸入,這裏是完成的腳本與一些更新。

使用pdftotext.exe作爲免費軟件xpdf套件的一部分(請捐贈,因爲它是一個很棒的工具),在這種情況下,一些查找文件可以幫助解析網站編號。在 001格式=我鎮

我完全沒有得到一個可行的方法來墊2位站點代碼與一家領先的0讓所有網站3個位數,但最終與另一個查找文件做同樣的事情!

我希望這對別人有用!

@echo off 
SETLOCAL ENABLEDELAYEDEXPANSION 
SET OutPref=373137# 
SET OutSuf1=#My Text 
SET OutSuf2=#1520755.pdf 
SET [email protected] 
SET BaseDir=C:\myfolder\ 
SET LogFile=C:\myfolder\FileNamer.log 
SET InFolder=C:\myfolder\Inbound\ 
SET OutFolder=C:\myfolder\Outbound\ 
SET StoreList=C:\myfolder\StoreList.inf 
SET StoreNoConv=C:\myfolder\StoreNoConv.inf 

echo Starting Run %TIME% %DATE% >> %LogFile% 
echo Starting Run %TIME% %DATE% 
cd %InFolder% 
for /f "tokens=*" %%f in ('dir /b *.pdf') do (
    %BaseDir%pdftotext.exe -q %%f 
    set "InTxtVer=%%~nf.txt" 
    for /f "tokens=*" %%x in ('findstr !MatchStr! !InTxtVer!') do set InAddLine=%%x 
    call:getpos 
    call set StoreNo=%%InAddLine:~!pos!,-25%% 
    echo Now Renaming Store No !StoreNo! 
    call:getvalue %StoreList% !StoreNo! StoreName 
    call:getvalue %StoreNoConv% !StoreNo! ThreeDigitNo 
    set OutFile=!OutPref!Store!ThreeDigitNo!!OutSuf1!!StoreName!!OutSuf2! 
    echo %%f moved to !OutFile! >> %LogFile% 
    move "%%f" "%OutFolder%!OutFile!" >> %LogFile% 
    del !InTxtVer! 
) 

cd %BaseDir% 
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-=4 
goto:eof 

:getvalue 
rem This function reads a value from a file and stores it in a variable 
rem %1 = name of file to search in 
rem %2 = search term to look for 
rem %3 = variable to set search result under 
FOR /F "tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~3=%%~j 
goto:eof