+ Reply to Thread
Results 1 to 16 of 16

How to loop through filenames and perform an action based on filename criteria

  1. #1
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    How to loop through filenames and perform an action based on filename criteria

    Hi

    I am reading in text files and i am incrementing a value in each file that is read in. However i need now to check the filenames each time to see if the value should be incremented or not.

    Filenames are of the type xyx123-0.01, xyx123-0.02, xyx123-0.03, xyx123-0.04 and then the first part of the filename changes to abc345-0.01 etc.

    I need to ensure the values written to each set of 4 are the same. There is also a way to read a value from the file to do this as well but the filename currently looks to be the simplest.

    Just had a thought, I suppose the simplest way might be to say if the last chr of the filename is >1 then do not increment. Is that a safe way?

    Thanks

    Neil

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to loop through filenames and perform an action based on filename criteria

    I'm confused. Are you modifying file names or data within the file or both? Also, it is helpful if you post any code you are using so we don't spend a lot of time doing what you have already done.
    Last edited by JLGWhiz; 10-30-2018 at 03:23 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    I'm modifying a value within a set of files. However my logic i have realised is flawed and i am trying to figure out how to describe what i am trying to do accurately with some example code.

    I will come back once I am more able to describe the problem and hopefully show my possible solution.

    thanks

    neil

  4. #4
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    Further to my earlier post I have now figured out what I need to achieve.
    I have a folder with multiple files, each has the filename form “AUDCAD-0.01”
    The first 6 characters show the currency pair and then the number after the – gives the file variant
    There are 8 distinct files with 4 variants, e.g.“AUDCAD-0.01” to “AUDCAD-0.04” and “GBPUSD-0.01” to “GBPUSD-0.04” and so on.
    What I am trying to do is to modify a line in each text file in the following manner.
    A start time (let’s say 01:00) and increment value (let’s say 5 mins) is chosen by inputbox method.
    Then the first set of four currency pair files is read in sequence and the start time set to the chosen start time of 01:00 (i.e. without increment)
    Then the next set of four is read in sequence and the start time chosen is incremented by the chosen value if 5 mins then 01:05).
    Then the next set of four is read in sequence and the start time chosen is incremented by the chosen value and so on.
    I want to end up with 8 sets of four files with the first set with a start time of say 01:00 and the 8th set (if incremented by 5 mins per set) with a start time of 01:40.
    I have a means in code to read the files and do the increment and write the changes. What I cannot figure out is the logic to do this easily.
    A crude method might be to set a counter (number of the file read so 1 to 4 would be the first set of 4 files etc.) and a case statement to set the code to increment based on the counter.
    Select Case Counter
    Case 1 to 4
    Do not increment ‘set time to 01:00
    Select Case Counter
    Case 5 to 8
    Increment by value chosen ’set time to 01:05
    Case 9 to 12
    Increment by value chosen ’set time to 01:10
    Etc.

    Currently the logic is starting like this but i got stuck.

    Please Login or Register  to view this content.
    Then i get stuck
    ' GetTemplateVersion (StrFile) returns the number of the version -0.01 returns 1 if that can be used.

    Thanks

    Neil

  5. #5
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to loop through filenames and perform an action based on filename criteria

    Put your target file names in an array, then use the array in a loop to call up the files you want to modify. You can then use a Select case to assign the increments so they will apply in the order you desire.

  6. #6
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    Ok thanks i found a bit of code that does the reading in and i can see how to use them but i do not know how to make it so that i can keep the array "static" so i can use it in my subroutine.
    I have looked it up but fail to understand quite how it works.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    I have found out how to make the array public and have managed to loop through the first set of 4 files with the same currency pair eg. "AUDUSD" in the filename. These four have the associated files modified to the start time chosen.

    Now I need to figure out logic to proceed through the remainder of files and for each set of the remaining currency pairs to increment each to the same incremented time.

    So for the remainder of files are likely to be in sets of four (but not definitely) so rather than counting i need to check the pair in the filename and loop through the ones with the same pair and update them to the same value.

    Trouble is how do you do this easily?

    If as part of the loop i read the pair from the filename and store it in a variable (to compare with the next variable read) then it will be overwritten each time?

    So far I have looped through all the files with AUSUSD in the filename and updated them. The next bit i have not figured out. The next file name part is say GBPUSD and i need to update all the ones with this name part until i hit the next change in name part.

    Can i somehow use a do while?

    Thanks Neil

  8. #8
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to loop through filenames and perform an action based on filename criteria

    It can get pretty dicey when you are working with several layers of data, ie. files, sheets and names, that all have multiple values associated with them and you want to organize them in a specific order of combinations for a result. The logic for such an algorithm will likely require nested loops of some sort. Finding the right combination of types of loops with their associated algorithms is the key to success. As I previously stated, Select Case might be one way to handle the lower level algorithms for sequencing the increments. You could probably use the Do While loop to handle the different currency types, but then sequencing the currency types and maintaining the increment chain is really the big issue.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: How to loop through filenames and perform an action based on filename criteria

    Neil

    Do you have a list/lists of the currencies/currency pairs and the values to be updated?
    If posting code please use code tags, see here.

  10. #10
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    I Cannot post for some reason today as they detect an SQL injection!!!!!!??????

  11. #11
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    The reply is in the attached text file as i could not post it?
    Attached Files Attached Files

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: How to loop through filenames and perform an action based on filename criteria

    Do you actually have a list of currency pairs?

  13. #13
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    Trying again to post these but a sample was in the attached file as i could not post before.

    AUDCAD-0.01.tpl
    AUDCAD-0.02.tpl
    AUDCAD-0.03.tpl
    AUDCAD-0.04.tpl
    AUDJPY-0.01.tpl
    AUDJPY-0.02.tpl
    AUDJPY-0.03.tpl
    AUDJPY-0.04.tpl
    AUDUSD-0.01.tpl
    AUDUSD-0.02.tpl
    AUDUSD-0.03.tpl
    AUDUSD-0.04.tpl
    EURJPY-0.01.tpl
    EURJPY-0.02.tpl
    EURJPY-0.03.tpl
    EURJPY-0.04.tpl
    GBPUSD-0.01.tpl
    GBPUSD-0.02.tpl
    GBPUSD-0.03.tpl
    GBPUSD-0.04.tpl
    NZDJPY-0.01.tpl
    NZDJPY-0.02.tpl
    NZDJPY-0.03.tpl
    NZDJPY-0.04.tpl
    NZDUSD-0.01.tpl
    NZDUSD-0.02.tpl
    NZDUSD-0.03.tpl
    NZDUSD-0.04.tpl
    USDCHF-0.01.tpl
    USDCHF-0.02.tpl
    USDCHF-0.03.tpl
    USDCHF-0.04.tpl

  14. #14
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    Trying again to post these but a sample was in the attached file as i could not post before. If i try and post the code i get accused of an SQL injection!

    AUDCAD-0.01.tpl
    AUDCAD-0.02.tpl
    AUDCAD-0.03.tpl
    AUDCAD-0.04.tpl
    AUDJPY-0.01.tpl
    AUDJPY-0.02.tpl
    AUDJPY-0.03.tpl
    AUDJPY-0.04.tpl
    AUDUSD-0.01.tpl
    AUDUSD-0.02.tpl
    AUDUSD-0.03.tpl
    AUDUSD-0.04.tpl
    EURJPY-0.01.tpl
    EURJPY-0.02.tpl
    EURJPY-0.03.tpl
    EURJPY-0.04.tpl
    GBPUSD-0.01.tpl
    GBPUSD-0.02.tpl
    GBPUSD-0.03.tpl
    GBPUSD-0.04.tpl
    NZDJPY-0.01.tpl
    NZDJPY-0.02.tpl
    NZDJPY-0.03.tpl
    NZDJPY-0.04.tpl
    NZDUSD-0.01.tpl
    NZDUSD-0.02.tpl
    NZDUSD-0.03.tpl
    NZDUSD-0.04.tpl
    USDCHF-0.01.tpl
    USDCHF-0.02.tpl
    USDCHF-0.03.tpl
    USDCHF-0.04.tpl

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: How to loop through filenames and perform an action based on filename criteria

    Why not add an extra column to this list with the value that needs to be updated in each file?

    Then all you would need to do is loop through the list, open the file, update it with the corresponding value from the 'value' column and then close the file.

  16. #16
    Forum Contributor
    Join Date
    02-03-2018
    Location
    Buckinghamshire, England
    MS-Off Ver
    2010
    Posts
    130

    Re: How to loop through filenames and perform an action based on filename criteria

    I suppose having chosen the increment value from the input box i could add this somehow to the array of pairs and then retrieve them to do as you suggest.

    I will have to look that up as have only just played with arrays here in current code.

    BTW i did get current code almost there but it is messy and currently it jumps from 5 min increment to 10 on first loop and then it works in 5 min increments forwards. slight logic problem!!
    thanks

    Neil

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help with vba code to look at array and perform action when cell meets criteria
    By rv02 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-20-2018, 02:40 PM
  2. [SOLVED] Loop down col A and find 2 criteria in a cell to perform an action on the data in between
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-20-2016, 08:32 PM
  3. [SOLVED] Loop through all worksheets in workbook to perform action fix code
    By AWITCHER in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-08-2016, 02:26 PM
  4. Comparing two rows of data and perform action based on result
    By Mhvenegaard in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-26-2015, 05:02 PM
  5. [SOLVED] Edit macro to perform action in Sheet1 and Sheet2 based on the value in sheet3
    By bmbalamurali in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2013, 04:17 AM
  6. Loop through Autofilter Multiple Criterias and perform action
    By tahatkhan in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 03-22-2012, 04:07 PM
  7. Perform action based on choice from validation list
    By kjetiltb in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-05-2009, 04:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1