Jump to content

Recommended Posts

I have an Autoit script that lists files from a folder into an array list. Is there a way to separate the filenames by an underscore and include the id, version, name and date into separate columns in Excel.

Example of filename:

12345_v1.0_TEST Name [12345]_01.01.2022.html

12345 would be in one column

v1.0 would be in another column

TEST Name [12345] would be in another column

01.01.2022 would be in another column

.html would be in another column

Note: filenames always change each day.

Here is my code that lists the files into column C and then writes the column Headers into Column D, E, F, G. Just need some help with separating them into columns by the _ delimiter

 

Edited by goku200
Link to post
Share on other sites

_FileListToArray returns an array. StringSplit is by default 1-based, and your are not using StringRexExp for the extension. Please take a moment to understand what you are doing.  Put some error handling in your code to trace why some statements are faulted.

Link to post
Share on other sites
On 2/4/2022 at 5:44 PM, Nine said:

_FileListToArray returns an array. StringSplit is by default 1-based, and your are not using StringRexExp for the extension. Please take a moment to understand what you are doing.  Put some error handling in your code to trace why some statements are faulted.

Thanks for the reply @Nine. I went ahead and went a different route with this. I created a vba macro in Excel that separates my filenames into columns and then used Autoit to do the rest:

Sub Test_Formula()
    Dim i, LastRow As Long
   
    'Get last populated row in column one
    LastRow = Cells(Rows.Count, 1).End(xlUp).row
   
    'Loop thru rows
    For i = 2 To LastRow
        Range("B2:F" & LastRow).Formula = "=IF(LEN($A2)-LEN(SUBSTITUTE($A2,""_"",""""))+1>4,TRIM(MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A2,"".""&TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE($A2,""."","" "",LEN($A2)-LEN(SUBSTITUTE($A2,""."",""""))),"" "",REPT("" "",200)),100)),""_""&"".""&TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE($A2,""."","" "",LEN($A2)-LEN(SUBSTITUTE($A2,""."",""""))),"" "", REPT("" "",200)),100))),""_"","" "",3),""_"",REPT("" "",100)),COLUMN(B2)*99-98,100)), TRIM(MID(SUBSTITUTE(SUBSTITUTE($A2,"".""&TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE($A2,""."","" "",LEN($A2)-LEN(SUBSTITUTE($A2,""."",""""))),"" "",REPT("" "",200)),100)),""_""&"".""&TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE($A2,""."","" "",LEN($A2)-LEN(SUBSTITUTE($A2,""."",""""))),"" "",REPT("" "",200)),100))),""_"",REPT("" "",100)),COLUMN(B2)*99-98,100)))"
    Next i
   
End Sub

 

Edited by goku200
Link to post
Share on other sites

You can replace your formula by making a vba function and set that function making it a little more readable

n=0 for whole string last dot replaced with a _
n=1 for first word  and so on for getting your column

formula in cell is then like
B1=splitword(A1;1)

' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",0)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",1)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",2)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",3)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",4)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",5)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",6)

Public Function splitWord(str As Variant, n As Variant) As String
    dotPos = InStrRev(str, ".")
    newstr = Left(str, dotPos - 1) & "_" & Mid(str, dotPos + 1)
    
    If n = 0 Then
        splitWord = newstr
    Else
        splitWord = Split(newstr, "_")(n - 1)
    End If
End Function

 

 

 

Link to post
Share on other sites
3 hours ago, junkew said:

You can replace your formula by making a vba function and set that function making it a little more readable

n=0 for whole string last dot replaced with a _
n=1 for first word  and so on for getting your column

formula in cell is then like
B1=splitword(A1;1)


' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",0)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",1)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",2)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",3)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",4)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",5)
' splitword("12345_v1.0_TEST Name [12345]_01.01.2022.html",6)

Public Function splitWord(str As Variant, n As Variant) As String
    dotPos = InStrRev(str, ".")
    newstr = Left(str, dotPos - 1) & "_" & Mid(str, dotPos + 1)
    
    If n = 0 Then
        splitWord = newstr
    Else
        splitWord = Split(newstr, "_")(n - 1)
    End If
End Function

 

 

 

Thank you @junkew I'm a little confused with this function. Its not displaying in the macro when I click on View > Macros in Excel. Am I missing something?

Link to post
Share on other sites

You should copy the vba function code to a module.

I am not sure but it could be only sub procedures are shown in macro overview.

When you are in vba editor you can enter debug.print splitword to test the function or in you spreadsheet use it as a formule.

Link to post
Share on other sites
On 2/8/2022 at 2:30 PM, junkew said:

You should copy the vba function code to a module.

I am not sure but it could be only sub procedures are shown in macro overview.

When you are in vba editor you can enter debug.print splitword to test the function or in you spreadsheet use it as a formule.

Got it to work finally. Thank you.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By TheAlienDoctor
      Hi, I was looking into creating a script that would detect if a file exists, then move it (and in some cases rename it, depending on the file) as well as write to a log file.
      The issue is, there is a lot of files that need to be moved, sometimes some files will exist and others won't depending on the use-case. However if a file does exist, it will always be going into the same directory with the same name.
      Currently I have an array nested inside of the array, with each array inside that array having both the old and new directory, and then a For loop to actually run through and do the file transferring. The issue I am having is how to call the Array inside of the array, because how do I specify  which the old directory is and which the new is?
      Global $FileTransfer[2000] = [Global $Dir1[2] = ["original dir 1", "new dir 1"], Global $Dir2[2] = ["original dir 2", "new dir 2"]] For $FileTransfer = [0] To [1] Step +1 If FileExists({original dir}) Then FileMove({original dir}, new dir, 1) FileOpen("log.latest.txt", 1) FileWrite("log.latest.txt", "{original dir} found, moved it to new dir." & @CRLF) FileClose("log.latest.txt") Else FileOpen("log_latest.txt", 1) FileWrite("log_latest.txt", "{original dir} not found, ignoreing it." & @CRLF) FileClose("log_latest.txt") EndIf Next I have put what I want the old and new directory to be for each array in {}, so hopefully its easier to tell which part is working and whats not.

      I am still reasonably new to AutoIT, any help is appreciated. Thankyou
    • By arunkw
      I have a spreadsheet - daily routine which has two columns: activity and time as shown here
      | Activity             | Time     | |----------------------|----------| | Sleep               |  6:00 am | | Toilet              |  6:15 am | | Get ready for gym  |  6:30 am | | Exercise            |  7:50 am | | ... more things      |  9:00 pm | | ... still more       | 10:45 pm | | Sleep               |  6:00 am |   I wanted to find out, say in C1 which activity is current for me using now() I.e., if it’s 6:45am on my watch, it should show me Exercise  in C1 Thanks to Adam D. PE, this formula works like magic to get the result =VLOOKUP(MOD(NOW(),1),{B2:B,A2:A},2,1)   Now, I want to reproduce same result in autoit, how to do that? To have easy solution say, I copy-paste spreadsheet data in array directly in code, right? Use for loop and run the above vlookup function and show the answer using tooltip. How to achieve this? please help.  
    • By SkysLastChance
      I have been trying to figure this out longer then I would like to admit. I am betting that I am missing something super simple. 
      #include <Excel.au3> Local $oExcel = _Excel_Open() Local $oWorkbook = _Excel_BookNew($oExcel) $vPath = "'J:\Temporary Files\FolderName\FileName.xlsx''!$A:$B,2,0))'" _Excel_RangeWrite($oWorkBook, Default, '=IF(D2=D1,"",VLOOKUP(D2,''J:\Temporary Files\FolderName\FileName.xlsx''!$A:$B,2,0))', "A1", False) ;Line 1 _Excel_RangeWrite($oWorkBook, Default, '=IF(D2=D1,"",VLOOKUP(D2,' & $vPath, "A2", False) ;Line 2 ConsoleWrite("'=IF(D2=D1,"",VLOOKUP(D2,''J:\Temporary Files\FolderName\FileName.xlsx''!$A:$B,2,0))'" & @CRLF) ConsoleWrite("'=IF(D2=D1,"",VLOOKUP(D2,'" & $vPath & @CRLF) Why is line 1 writing but line 2 is not? 
      Any suggestion to lead me in the right direction is appreciated. 
       
    • By SkysLastChance
      I am having a issue of whenever I try to _Excel_RangeWrite a formula that references another workbook I am getting an error @4 and @extended -2147352567

      #include <Excel.au3> #include <MsgBoxConstants.au3> Local $oExcel = _Excel_Open() Local $oWorkbook = _Excel_BookNew($oExcel) _Excel_RangeWrite($oWorkBook,Default,"=IF(D2=D1,"",VLOOKUP(D2,'J:\Temporary Files\FolderName\FileName.xlsx'!$A:$B,2,0))","W2",False) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 1", "Error writing to worksheet." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 1", "String successfully written.") I am not a 100% sure, but I am guessing this is because I have "" and '' in the formula. However, I am not sure how this can best be resolved. 
       
       
    • By SkysLastChance
      I am trying to autofill a range. I am getting stuck and I don't understand what I am doing wrong. 
      My goal is to auto fill some formulas that are next to a pivot table in columns A-C. 
      _Excel_RangeWrite($oNewWorkBook,Default,"2000","D3") _Excel_RangeWrite($oNewWorkBook,Default,"=(B3-D3)","E3") _Excel_RangeWrite($oNewWorkBook,Default,"100","F3") _Excel_RangeWrite($oNewWorkBook,Default,"=(C3-F3)","G3") $oNewWorkbook.ActiveSheet.Range("D3:G3").Select With $oNewWorkbook .Selection.AutoFill(.Range("D3:G77"),0) EndWith The data is not auto filling. 
      Hoping someone can point me in the right direction. 
×
×
  • Create New...