- Joined
- Aug 7, 2013
- Messages
- 1,338
Hi,
Is there anyway to force a programming script to always find files in the same order?
I notice that if I walk down my directory, the script I am using will collect the files by how they are sorted in the file system, e.g.
dir1
x.txt
y.txt
z.txt
It will collect x, then y, then z.
But let's suppose I added a new file to dir1 called "a"
dir1
x.txt
y.txt
z.txt
a.txt
Now all of the sudden, the script first takes a, then x, then y, then z.
So how could I force it to do x, y, z, a instead?
One (brute force) solution is simply to prefix every file with a number to force the order
dir1
0_x.txt
1_y.txt
2_z.txt
3_a.txt
But this has the downside, that everytime I need to make a new file in dir1, I have to prefix it with an integer. It also ruins the readability in a way.
A second solution would be to access how long ago the file was created, and thus sort the collected files by that before doing I/O.
Then again, if I have to read my files in a certain order, is that just a bad sign of organization?
For my problem, I basically have a directory that I occasionally add a new file to. When my script reads that dir, it assigns each file name a unique integer value. This value corresponds to a position in an auxiliary data structure. But a problem is, if I add a new file, it ruins the order of the data structure. This is because the data structure doesn't have an ability to overwrite previous data. So for the previous example here is my data structure, D.
D[0] = Attributes(x.txt)
D[1] = Attributes(y.txt)
D[2] = Attributes(z.txt)
But now, I added a.txt in, so here is what the structure looks like now
D[0] = Attributes(a.txt)
D[1] = Attributes(x.txt)
D[2] = Attributes(y.txt)
D[3] = Attributes(z.txt)
So the problem is, my data structure cannot overwrite previous values. This means everything is jumbled up now, e.g. D[0] will have attributes from both a.txt and x.txt. Likewise for every entry except D[3], which is correctly just the attributes of z.txt.
Before solutions are proposed, note that it is impossible to change the auxiliary data structure D in any way that would allow sweeping away previous entries.
Is there anyway to force a programming script to always find files in the same order?
I notice that if I walk down my directory, the script I am using will collect the files by how they are sorted in the file system, e.g.
dir1
x.txt
y.txt
z.txt
It will collect x, then y, then z.
But let's suppose I added a new file to dir1 called "a"
dir1
x.txt
y.txt
z.txt
a.txt
Now all of the sudden, the script first takes a, then x, then y, then z.
So how could I force it to do x, y, z, a instead?
One (brute force) solution is simply to prefix every file with a number to force the order
dir1
0_x.txt
1_y.txt
2_z.txt
3_a.txt
But this has the downside, that everytime I need to make a new file in dir1, I have to prefix it with an integer. It also ruins the readability in a way.
A second solution would be to access how long ago the file was created, and thus sort the collected files by that before doing I/O.
Then again, if I have to read my files in a certain order, is that just a bad sign of organization?
For my problem, I basically have a directory that I occasionally add a new file to. When my script reads that dir, it assigns each file name a unique integer value. This value corresponds to a position in an auxiliary data structure. But a problem is, if I add a new file, it ruins the order of the data structure. This is because the data structure doesn't have an ability to overwrite previous data. So for the previous example here is my data structure, D.
D[0] = Attributes(x.txt)
D[1] = Attributes(y.txt)
D[2] = Attributes(z.txt)
But now, I added a.txt in, so here is what the structure looks like now
D[0] = Attributes(a.txt)
D[1] = Attributes(x.txt)
D[2] = Attributes(y.txt)
D[3] = Attributes(z.txt)
So the problem is, my data structure cannot overwrite previous values. This means everything is jumbled up now, e.g. D[0] will have attributes from both a.txt and x.txt. Likewise for every entry except D[3], which is correctly just the attributes of z.txt.
Before solutions are proposed, note that it is impossible to change the auxiliary data structure D in any way that would allow sweeping away previous entries.
Last edited: