2017-03-07 1 views
0

FOR 루프 내에서 하위 문자열 조작을 사용하려고하고 있으며 사랑이나 돈을 위해 작동하지 않습니다. 루프 변수 (%% f 등)에서 부분 문자열 조작을 사용할 수 없다는 말을 들었으므로 다른 변수를 루프와 동일하게 설정해야합니다 (MyVariable = %% f로 설정). 이 설정에 따라 작업하십시오 (MyOtherVar = % MyVariable 설정 : ~ 0, -3 %). 그러나 이러한 변수를 반향시킬 때만 루프 변수가 설정/비 null입니다.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 

희망이 있습니다. 시도해 볼 수 있습니다. 확실히 바닥 부분 doesnt 일 그러나 didnt는 그것을 멀리 얻는다!

모든 의견을 주셔서 감사합니다. 일반적인 개요로 스크립트는 들어오는 폴더에서 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

지연 확장을 검색해야합니다. 블록 내에서 설정되는 변수는'!'not '%', 즉'! InPdfVer : ~ 0, -3!'등을 사용하여 참조되어야합니다. – Compo

+3

또한 코드 블록 내에서'loop'와 같은 레이블을 사용할 수 없습니다 (명령의 괄호 안의 계열) – Magoo

+0

하하! 세월 동안 하위 문자열 조작을 조사해 보았지만 문제를 볼 수 없었고 지연된 확장으로 인해 엉망이었습니다. 나는 그것을 참조하기 전에 %% f 타입 변수를 확장하지 않는다. 주어진 일종의 변수로서, for 루프의 반복마다 변수가 다를 필요가있다. – Xeotech

답변

0

귀하의 의견을 모두 주셔서 감사합니다. 몇 가지 업데이트가 추가 된 완성 된 스크립트는 다음과 같습니다.

프리웨어 xpdf 제품군의 일부로 pdftotext.exe를 사용합니다 (훌륭한 유틸리티이므로 기부 해 주시기 바랍니다).이 경우 사이트 번호를 설명에 도움이되는 일부 조회 파일을 사용합니다. 001의 형식 = 내 타운에서

난 완전히 모든 사이트에 3 개 자리를 만들기 위해 선도적 0 패드 2 개 자리 사이트 코드를 실행 가능한 방법을 가져 오지 못했습니다하지만 다른 검색 파일과 같은 일을하고 결국!

다른 사람에게 유용 할 것입니다.

@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