• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Search for multiple files in a directory - automation

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to automate the task - looking for files in a folder daily. There are 14 files with different names. Example: ACH_ACC_20210607.csv, PRP_ACC_20210607.csv, ACR_PREM_20210607.csv etc. My activity is to check daily if above 14 files are present or not in a folder.

The date in the filename for all the files will be current date minus 1 i.e. yesterday date. When we are checking on monday, we will check for friday so minus 3 days.

Can anyone help me to automate this activity? below is the code which works for only one file. But i am having difficulty writing code to check multiple files.

 
Saloon Keeper
Posts: 6969
164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I wouldn't loop over the files that are there (which is what the code does now), I would loop over the files that should be there. So you need an array of all the filename prefixes you want to check, and then loop through them, constructing the full filename from the prefix and check whether it's present.
 
Marshal
Posts: 7982
560
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai pranav wrote:


You mentioned what you wanted to do on Monday, to get Friday as a yesterday, however, why are you having that second line of code and calling it every single day? As if it would get you Friday every time - it won't.

What you want to do, is to figure out whether today is Monday or not, and then as for a Friday to subtract 3 days. Check documentation what methods LocalDate has, I'm sure there is something useful.
 
Saloon Keeper
Posts: 23689
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Liutauras Vilda
Marshal
Posts: 7982
560
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given some examples are already out...

I'd go with this approach:

I personally find it semantically more correct. And the reason for this isn't obvious right away, unless I improve variable name from yesterday to previousWorkday, so I'd finally go with this:

Now I find this code not only semantically correct, but also logically correct.

If you'd take an example of having variable name as yesterday, then it is a bit wonky to say that having a Monday currently, the yesterday is Friday.
 
Tim Holloway
Saloon Keeper
Posts: 23689
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My only complaint with that approach is that the trinary operator is considered to be somewhat dangerous.

The example I gave is an anti-bugging approach in that the day will at worst be in the ballpark with no danger of it slipping through uninitialized (technically, that SHOULDN'T happen for the trinary either, but like I said, it's not universally loved).

The one downside is that it's subtracting calendar days, which might possibly be more overhead than an equivalent case where the affected variable was a simple integer or float. Then again, premature optimization is the worst kind of operation.
 
Marshal
Posts: 72913
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with the ?: operator? Its behaviour is strictly defined. I would suggest both expressions after ? be the same type, because it seems to cast types silently if they aren't exactly the same. It also often has to be wrapped in (...) if it is every used in conjunction with other operators because of its low precedence. It is possible to nest ?:s, but it takes some getting used to its right‑associativity.
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic