2013-05-21 3 views
3

У меня есть код для выполнения процесса в одном файле, может ли кто-нибудь изменить этот скрипт, чтобы он перебирал все файлы в каталоге «H: \ Letter Display \ Letters» с тип файла «.LTR» и сохраняет их все:VBScript для прокрутки всех файлов в папке

Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.LTR", ForReading) 


str1000 = "1000" 
str1100 = "1100" 
str1200 = "1200" 
str9990 = "9990" 

arrCommas1 = Array(14,31,41,59,70,81,101,111,124,138) 
arrCommas2 = Array(14,31,41,55,79,144,209,274,409,563,589,608,623) 
arrCommas3 = ArraY (14,32,41,73,83,97,106,156,167,184,188,195,207,260,273,332,368,431,461,472,593,617,666,772,810,834,848,894,898) 
arrCommas4 = Array(14,31,41) 

Do Until objFile.AtEndOfStream 
    strLine = objFile.ReadLine 

    If Left(strLine, 4) = str1000 then 
    intLength = Len(strLine) 
    For Each strComma in arrCommas1 
     strLine = Left(strLine, strComma - 1) + "," _ 
     + Mid(strLine, strComma, intLength) 
    Next 
    End If 

    If Left(strLine, 4) = str1100 then 
    intLength = Len(strLine) 
    For Each strComma in arrCommas2 
     strLine = Left(strLine, strComma - 1) + "," _ 
     + Mid(strLine, strComma, intLength) 
    Next 
    End If 

    If Left(strLine, 4) = str1200 then 
    intLength = Len(strLine) 
    For Each strComma in arrCommas3 
     strLine = Left(strLine, strComma - 1) + "," _ 
     + Mid(strLine, strComma, intLength) 
    Next 
    End If 

    If Left(strLine, 4) = str9990 then 
    intLength = Len(strLine) 
    For Each strComma in arrCommas4 
     strLine = Left(strLine, strComma - 1) + "," _ 
     + Mid(strLine, strComma, intLength) 
    Next 
    End If 

    strText = strText & strLine & vbCrLf 
Loop 


objFile.Close 

Set objFile = objFSO.OpenTextFile("H:\Letter Display\Letters\LTRPRT__00000008720000000001NI-K-RMND.LTR", ForWriting) 
objFile.Write strText 
objFile.Close 

Любая помощь будет высоко ценится!

Благодаря

+0

Я уже ответил на этот вопрос [здесь] (http://stackoverflow.com/a/16648253/1630171). –

+0

Да, но я немного запутался в том, как реализовать его в остальной части кода, –

ответ

7

Возможно, это прояснит ситуацию. (Или запутать вас более,)

Const ForReading = 1 
Const ForWriting = 2 

sFolder = "H:\Letter Display\Letters\" 
Set oFSO = CreateObject("Scripting.FileSystemObject") 

For Each oFile In oFSO.GetFolder(sFolder).Files 
    If UCase(oFSO.GetExtensionName(oFile.Name)) = "LTR" Then 
    ProcessFiles oFSO, oFile 
    End if 
Next 

Set oFSO = Nothing 


Sub ProcessFiles(FSO, File) 

Set oFile2 = FSO.OpenTextFile(File.path, ForReading) 

str1000 = "1000" 
str1100 = "1100" 
str1200 = "1200" 
str9990 = "9990" 

arrCommas1 = Array(14,31,41,59,70,81,101,111,124,138) 
arrCommas2 = Array(14,31,41,55,79,144,209,274,409,563,589,608,623) 
arrCommas3 = ArraY (14,32,41,73,83,97,106,156,167,184,188,195,207,260,273,332,368,431,461,472,593,617,666,772,810,834,848,894,898) 
arrCommas4 = Array(14,31,41) 

    Do Until oFile2.AtEndOfStream 
     strLine = oFile2.ReadLine 

     If Left(strLine, 4) = str1000 then 
     intLength = Len(strLine) 
     For Each strComma in arrCommas1 
      strLine = Left(strLine, strComma - 1) + "," _ 
      + Mid(strLine, strComma, intLength) 
     Next 
     End If 

     If Left(strLine, 4) = str1100 then 
     intLength = Len(strLine) 
     For Each strComma in arrCommas2 
      strLine = Left(strLine, strComma - 1) + "," _ 
      + Mid(strLine, strComma, intLength) 
     Next 
     End If 

     If Left(strLine, 4) = str1200 then 
     intLength = Len(strLine) 
     For Each strComma in arrCommas3 
      strLine = Left(strLine, strComma - 1) + "," _ 
      + Mid(strLine, strComma, intLength) 
     Next 
     End If 

     If Left(strLine, 4) = str9990 then 
     intLength = Len(strLine) 
     For Each strComma in arrCommas4 
      strLine = Left(strLine, strComma - 1) + "," _ 
      + Mid(strLine, strComma, intLength) 
     Next 
     End If 

     strText = strText & strLine & vbCrLf 
    Loop 

    sFile = File.path 
    oFile2.close 
    set oFile2 = Nothing 

    Set File = FSO.OpenTextFile(sFile , ForWriting) 
    File.Write strText 
    File.Close 
    Set File = Nothing 

end sub 
+0

Благодарим за помощь, она не похожа на эту строку: sFile = File.path, поскольку он не поддерживает это свойство или метод 'File.path' –

+1

Здесь вы попробуете его сейчас. –

+0

Спасибо большое! Действительно помог! –

2

Ваш текущий скрипт в основном выполняет следующие действия:

Set objFile = objFSO.OpenTextFile("...", ForReading) 
Do Until objFile.AtEndOfStream 
    strLine = objFile.ReadLine 
    'do stuff with strLine and append to strText 
Loop 
objFile.Close 

Set objFile = objFSO.OpenTextFile("...", ForWriting) 
objFile.Write strText 
objFile.Close 

Для обработки всех файлов в данной папке, просто нужно добавить внешнюю петлю вокруг этого, и настроить некоторые соответствующие инструкции:

For Each f In objFSO.GetFolder("C:\some\folder").Files 
    Set objFile = f.OpenAsTextStream 
    Do Until objFile.AtEndOfStream 
    strLine = objFile.ReadLine 
    'do stuff with strLine and append to strText 
    Loop 
    objFile.Close 

    Set objFile = f.OpenAsTextStream(ForWriting) 
    objFile.Write strText 
    objFile.Close 
Next
+0

@AnsgerWiechers - +1 для использования .OpenAsTextStream. –

+0

+1 плюс для OpenAsTextStream. –

0

Что бы еще лучше сделать рекурсивную функцию, чтобы идти во все папки, которые находятся ниже основной папке и поиск тех, а .. Просто и идея :)

Смежные вопросы