2012-01-11 3 views
1

는 불행하게도 내 VB 지식은 매우 최소,하지만 난 다음을 수행하는 스크립트를 필요
License 1
License 2
License 3VB에서 파일의 문자열을 읽고 바꾸는 방법은 무엇입니까?

I : 내가 한 줄에 하나의 라이센스를 포함하는 파일 Licenses.txt을 가지고 Licenses.txt 전나무에서
읽기 : 나는 다음을 수행 할 ProductKey=XXXXX-XXXXXX-XXXXXX-XXXX

:이 형식에 값 ProductKey이있는 다른 파일 response.ini을 가지고 세인트 라이센스. 무료 (라이센스가 Used을 유지하지 않으면 라이센스가 무료 인 경우), response.ini의 XXXXX-XXXXXX-XXXXXX-XXXX 값을 바꿔야하고 Used을 첫 번째 라이센스 이후에 입력해야합니다. 따라서 License.txt에는 License 1 - Used으로 유지되어야합니다. 다시 실행하면 두 번째 라이센스를 사용해야하며 그 후에 Used을 입력해야합니다. 무료 라이센스가 없으면 ErrorMessage를 받아야합니다.

바라는 바에는 내가하고 싶은 것을 이해할 수 있기를 바랍니다. 누군가 나를 도울 수 있습니까?

감사합니다.

답변

1

다음은 원하는대로 정확하게 수행 할 수있는 스크립트입니다.

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject") 

'Read the licence.txt file 
Dim f : Set f = oFSO.OpenTextFile("Licence.txt", 1) 
Dim sData : sData = f.ReadAll 
f.close 

'Find next available licence 
licenceKey = "" 
Dim sLine : for each sLine in split(sData, vbNewLine) 
    if (lcase(right(sLine,7)) <> " - used") then 
     licenceKey = sLine 
     exit for 
    end if 
Next 

if (licenceKey = "") then 
    'Show error if no licences are available 
    msgbox "No available licences! ", vbOkOnly + vbexclamation, "Licence" 
else 
    'Write a Response.ini file 
    Set f = oFSO.OpenTextFile("Response.ini", 2, true) 
    f.Write licenceKey 
    f.close 

    'Mark the licence as used 
    sData = replace(sData, licenceKey, licenceKey & " - Used") 

    'ReWrite the licence.txt file 
    Set f = oFSO.OpenTextFile("Licence.txt", 2) 
    f.Write sData 
    f.close 
end if 
관련 문제