views:

77

answers:

3

I have written a script that works. What it does now is it looks through a directory to a given file and returns what is on the second row fourth tab (RXC193) and renames the file to that of which it found from a file like this:

@Program @RxBIN @RXPCN @RxGroup @MemberID @WebsiteE @WebsiteS @VerticalLogo @TextLogo
RXCUT 013824 RXCUT RXC193 RXC5FHXF9 www.rxcut.com/HBG www.rxcut.com/HBG/es P:\RxCut\In Design Implementation\RXC193

What I need this script to be able to do is loop through the directory and rename all files by this RXC#####. Here is the script:

Call TwoDimensionArrayTest

Sub TwoDimensionArrayTest
' Version 1.0
' Writtem by Krystian Kara
' Dated 25-Jan-2009


    Dim fso
    Dim oFile
    Dim arrline
    Dim arrItem
    Dim objFolder
    Dim i
    Dim arrMain()
    Dim sFileLocation, strResults

    Const forReading = 1

' The file contains on each line:
    ' Text1 (tab) Text2 (tab) Text3 (tab) Text4
    ' Text5 (tab) Text6 (tab) Text7 (tab) Text8
'etc etc


    Set fso = CreateObject("Scripting.FileSystemObject")
        sFileLocation = "file 2.txt"

        Set oFile = fso.OpenTextFile(sFileLocation, forReading, False)

    Do While oFile.AtEndOfStream <> True
        strResults = oFile.ReadAll
    Loop

' Close the file
    oFile.Close

' Release the object from memory
    Set oFile = Nothing

' Return the contents of the file if not Empty
    If Trim(strResults) <> "" Then

        ' Create an Array of the Text File
        arrline = Split(strResults, vbNewLine)
    End If

    For i = 0 To UBound(arrline)
        If arrline(i) = "" Then
            ' checks for a blank line at the end of stream
            Exit For
        End If 

        ReDim Preserve arrMain(i)

            arrMain(i) = Split(arrline(i), vbTab)

    Next

    fso.MoveFile "file 2.txt", arrMain(1)(3) & ".txt"

End Sub ' TwoDimensionArrayTest

Thanks in advance, Joe

A: 

One approach is to parameterize the file name in your sub-procedure so it can be called multiple times for different files, like this:

Sub TwoDimensionArrayTest(fileName) 'you may want a more descriptive name

    ' ...
    sFileLocation = fileName
    ' ...

End Sub

Then, write a loop that goes through your directory, calling your sub each time around:

Dim fso, folder

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("Your Folder Name")
For Each file In folder.Files
    TwoDimensionArrayTest file.Path
Next
Jacob
I keep getting invalid call procedure
jmituzas
You should also remove your `Call TwoDimensionArrayTest` line since after my suggested change, `TwoDimensionArrayTest` requires a parameter.
Jacob
A: 

ok I have re-writen some things but am still having problems here is what I have so far...

All is well until line 33 where it gives the error Object required: 'objFolder' thats why I have it as a comment. I don't know if there are any other errors but I have corrected half a dozen and got to line 33 almost there please help.

Thanks in advance, Joe

Here is my re-written code:

strFolder = "C:\Documents and Settings\jmituzas.NMCLLC\Desktop\desktop2\New Folder (2)\datafiles\" Set objFSO = CreateObject("Scripting.FileSystemObject") For Each objFile In objFSO.GetFolder(strFolder).Files If Right(LCase(objFile.Name), 4) = LCase(".txt") Then TwoDimensionArrayTest objFile.Path End If Next

Call TwoDimensionArrayTest(iName) 

Sub TwoDimensionArrayTest(iName) Dim fso Dim oFile Dim oFiles Dim arrline Dim arrItem Dim i Dim arrMain() Dim strResults

  Const forReading = 1

   iName = 1

' The file contains on each line: ' Text1 (tab) Text2 (tab) Text3 (tab) Text4 ' Text5 (tab) Text6 (tab) Text7 (tab) Text8 'etc etc

    Set fso = CreateObject("Scripting.FileSystemObject")

'Error receiving: Object required: 'objFolder'

      oFiles = objFolder.Files 

Do While iName <= oFiles.Count
    Set oFile = oFiles(iName) 
    iName = iName + 1 & ".txt"
    Response.Write 

Loop ' Close the file oFile.Close

' Release the object from memory Set oFile = Nothing

' Return the contents of the file if not Empty If Trim(strResults) <> "" Then

        ' Create an Array of the Text File
        arrline = Split(strResults, vbNewLine)
  End If

  For i = 0 To UBound(arrline)
        If arrline(i) = "" Then
              ' checks for a blank line at the end of stream
              Exit For
        End If 

        ReDim Preserve arrMain(i)

              arrMain(i) = Split(arrline(i), vbTab)

  Next

  oFile.Name arrMain(1)(3) & ".txt"

End Sub ' TwoDimensionArrayTest(iName)

jmituzas
A: 

Here is the Final Error free code! Finally have it searching through my directory of Tab-delimited.txt files and grabbing from the second row third tab (group number) then renaming the files to its corrisponding group number! YAY!

heres final error free code!:

Call TwoDimensionArrayTest

Sub TwoDimensionArrayTest

Dim fso Dim oFile Dim arrline Dim arrItem Dim i Dim arrMain() Dim sFileLocation, strResults

Const forReading = 1

strFolder = "C:\Documents and Settings\jmituzas.NMCLLC\Desktop\desktop2\New Folder (2)\datafiles" Set objFSO = CreateObject("Scripting.FileSystemObject") For Each objFile In objFSO.GetFolder(strFolder).Files If Right(LCase(objFile.Name), 4) = LCase(".txt") Then

    ' The file contains on each line:
' Text1 (tab) Text2 (tab) Text3 (tab) Text4
' Text5 (tab) Text6 (tab) Text7 (tab) Text8

'etc etc

Set fso = CreateObject("Scripting.FileSystemObject") sFileLocation = objFile.Name

    Set oFile = fso.OpenTextFile(objFile.Name, forReading, False)

Do While oFile.AtEndOfStream <> True
    strResults = oFile.ReadAll
Loop

' Close the file
oFile.Close

' Release the object from memory Set oFile = Nothing

' Return the contents of the file if not Empty If Trim(strResults) <> "" Then

    ' Create an Array of the Text File
    arrline = Split(strResults, vbNewLine)
End If

For i = 0 To UBound(arrline)
    If arrline(i) = "" Then
        ' checks for a blank line at the end of stream
        Exit For
    End If 

    ReDim Preserve arrMain(i)

        arrMain(i) = Split(arrline(i), vbTab)

Next

  fso.MoveFile sFileLocation, arrMain(1)(3) & ".txt"

End If Next End Sub ' TwoDimensionArrayTest

jmituzas