![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Sorting files Hi all, I wrote a script that looped through all files in a folder, however, it seems that the files created in the collection are not appearing in alphabetical order, which is causing my routine to fail. Basically, I want to loop through all files until I find the last one created, and then perform an action on this file. Does anyone know how to do this? Can they provide an example etc? Below is listed an example the files that I want to process. I don't know the name of the files in advance, apart from a portion of them. As you can see, the first portion before the underscore does not change, so I want to find the last file written that starts "File123_" and the process this file. Once done, I need to move onto the next group of files that start "File456_" and process the last one written. File123_abc.txt File123_def.txt File123_ghi.txt File456_ghi.txt File456_abc.txt File456_def.txt Hopefully this makes sense :-S Thanks in advance |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Sorting files "Hugh Janus" <my-junk-account@xxxxxx> schrieb im Newsbeitrag news:2a1d5342-1549-4982-9420-9273923562c1@xxxxxx Quote: > Hi all, > > I wrote a script that looped through all files in a folder, however, > it seems that the files created in the collection are not appearing in > alphabetical order, which is causing my routine to fail. Basically, I > want to loop through all files until I find the last one created, and > then perform an action on this file. > > Does anyone know how to do this? Can they provide an example etc? > Below is listed an example the files that I want to process. I don't > know the name of the files in advance, apart from a portion of them. > As you can see, the first portion before the underscore does not > change, so I want to find the last file written that starts "File123_" > and the process this file. Once done, I need to move onto the next > group of files that start "File456_" and process the last one written. > > File123_abc.txt > File123_def.txt > File123_ghi.txt > File456_ghi.txt > File456_abc.txt > File456_def.txt > > Hopefully this makes sense :-S > > > Thanks in advance Hi, Do you want to find the last file that was written to a folder? Maybe you can loop though the folder and check the file attribute "file creation date" ... ? To get the file's attribute you can use something like this: FileAccessInfo ("c:\test.txt") Sub FileAccessInfo(aFile) Dim s, f, fs Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFile(aFile) s = UCase(afile) & vbCrLf s = s & "file was created: " & f.DateCreated & vbCrLf s = s & "file was last accessed: " & f.DateLastAccessed & vbCrLf s = s & "fiel was last modified: " & f.DateLastModified wscript.echo s End Sub |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Sorting files On Jun 1, 3:02*pm, "Heinz" <Spacewalker4711(noSpam)@hotmail.com> wrote: Quote: > "Hugh Janus" <my-junk-acco...@xxxxxx> schrieb im Newsbeitragnews:2a1d5342-1549-4982-9420-9273923562c1@xxxxxx > > > > > Quote: > > Hi all, Quote: > > I wrote a script that looped through all files in a folder, however, > > it seems that the files created in the collection are not appearing in > > alphabetical order, which is causing my routine to fail. *Basically, I > > want to loop through all files until I find the last one created, and > > then perform an action on this file. Quote: > > Does anyone know how to do this? *Can they provide an example etc? > > Below is listed an example the files that I want to process. *I don't > > know the name of the files in advance, apart from a portion of them. > > As you can see, the first portion before the underscore does not > > change, so I want to find the last file written that starts "File123_" > > and the process this file. *Once done, I need to move onto the next > > group of files that start "File456_" and process the last one written. Quote: > > File123_abc.txt > > File123_def.txt > > File123_ghi.txt > > File456_ghi.txt > > File456_abc.txt > > File456_def.txt Quote: > > Hopefully this makes sense :-S Quote: > > Thanks in advance > Hi, > > Do you want to find the last file that was written to a folder? > Maybe you can loop though the folder and check the file attribute "file > creation date" ... ? > > To get the file's *attribute you can use something like this: > > FileAccessInfo ("c:\test.txt") > > Sub FileAccessInfo(aFile) > > Dim s, f, fs > > Set fs = CreateObject("Scripting.FileSystemObject") > Set f = fs.GetFile(aFile) > > s = *UCase(afile) & vbCrLf > s = s & "file was created: " & f.DateCreated & vbCrLf > s = s & "file was last accessed: " & f.DateLastAccessed & vbCrLf > s = s & "fiel was last modified: " & f.DateLastModified > > wscript.echo s > > End Sub- Hide quoted text - > > - Show quoted text - What I actually need is the last file written to the folder, for each 'group' of files. So, as you can see in my example, I need the last written file starting with File123, and then the last written file starting with File456. This is where I am lost. Any ideas? |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Sorting files On Jun 1, 8:32 am, Hugh Janus <my-junk-acco...@xxxxxx> wrote: Quote: > Hi all, > > I wrote a script that looped through all files in a folder, however, > it seems that the files created in the collection are not appearing in > alphabetical order, which is causing my routine to fail. Basically, I > want to loop through all files until I find the last one created, and > then perform an action on this file. > > Does anyone know how to do this? Can they provide an example etc? > Below is listed an example the files that I want to process. I don't > know the name of the files in advance, apart from a portion of them. > As you can see, the first portion before the underscore does not > change, so I want to find the last file written that starts "File123_" > and the process this file. Once done, I need to move onto the next > group of files that start "File456_" and process the last one written. > > File123_abc.txt > File123_def.txt > File123_ghi.txt > File456_ghi.txt > File456_abc.txt > File456_def.txt > > Hopefully this makes sense :-S > > Thanks in advance I am assuming that the underscore "_" is the only delimiter in the file name, and that from your response to Heinz, that the last modified date is not always the last file? Instead the 2nd half of the file name identifies that order of the files? The script uses a dictionary object to store the file groups as the dictionary keys. As it loops through the folder, it splits the file name by the underscore character. The first half (arr_temp(0)) is the group name (i.e. File123). If it is the first "group" to be found it adds it to the dictionary, along with the second half of the file name. If a file from the same group has been encountered it checks the 2nd half of the name to see if it is bigger. (Text comparison). If it's bigger, it gets replaced. Once it's through with the folder, the last few lines just iterate through all the dictionary items and echo back the group and last file in that group. Set fso = CreateObject("Scripting.FileSystemObject") Set d = CreateObject("Scripting.Dictionary") Set fldr = fso.GetFolder("C:\Temp") For each file in fldr.Files If InStr(1, file.name, "_") Then arr_temp = Split(file.name, "_") If d.Exists(arr_temp(0)) Then If arr_temp(1) > d.Item(arr_temp(0)) Then d.Item(arr_temp(0)) = arr_temp(1) End If Else d.Add arr_temp(0), arr_temp(1) End If End If Next d_keys = d.keys For i = 0 to d.Count-1 WScript.echo d_keys(i) & vbTab & d.Item(d_keys(i)) Next |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Sorting files On Jun 1, 3:52*pm, Jason <flaja...@xxxxxx> wrote: Quote: > On Jun 1, 8:32 am, Hugh Janus <my-junk-acco...@xxxxxx> wrote: > > > > > Quote: > > Hi all, Quote: > > I wrote a script that looped through all files in a folder, however, > > it seems that the files created in the collection are not appearing in > > alphabetical order, which is causing my routine to fail. *Basically, I > > want to loop through all files until I find the last one created, and > > then perform an action on this file. Quote: > > Does anyone know how to do this? *Can they provide an example etc? > > Below is listed an example the files that I want to process. *I don't > > know the name of the files in advance, apart from a portion of them. > > As you can see, the first portion before the underscore does not > > change, so I want to find the last file written that starts "File123_" > > and the process this file. *Once done, I need to move onto the next > > group of files that start "File456_" and process the last one written. Quote: > > File123_abc.txt > > File123_def.txt > > File123_ghi.txt > > File456_ghi.txt > > File456_abc.txt > > File456_def.txt Quote: > > Hopefully this makes sense :-S Quote: > > Thanks in advance > Here you go. (Watch for line wraps and indentation) > > I am assuming that the underscore "_" is the only delimiter in the > file name, and that from your response to Heinz, that the last > modified date is not always the last file? Instead the 2nd half of the > file name identifies that order of the files? > > The script uses a dictionary object to store the file groups as the > dictionary keys. As it loops through the folder, it splits the file > name by the underscore character. The first half (arr_temp(0)) is the > group name (i.e. File123). If it is the first "group" to be found it > adds it to the dictionary, along with the second half of the file > name. If a file from the same group has been encountered it checks the > 2nd half of the name to see if it is bigger. (Text comparison). If > it's bigger, it gets replaced. > > Once it's through with the folder, the last few lines just iterate > through all the dictionary items and echo back the group and last file > in that group. > > Set fso = CreateObject("Scripting.FileSystemObject") > Set d = CreateObject("Scripting.Dictionary") > > Set fldr = fso.GetFolder("C:\Temp") > For each file in fldr.Files > * * * * If InStr(1, file.name, "_") Then > * * * * * * * * arr_temp = Split(file.name, "_") > * * * * * * * * If d.Exists(arr_temp(0)) Then > * * * * * * * * * * * * If arr_temp(1) > d.Item(arr_temp(0)) Then > * * * * * * * * * * * * * * * * d.Item(arr_temp(0)) = arr_temp(1) > * * * * * * * * * * * * End If > * * * * * * * * Else > * * * * * * * * * * * * d.Add arr_temp(0), arr_temp(1) > * * * * * * * * End If > * * * * End If > Next > > d_keys = d.keys > > For i = 0 to d.Count-1 > * * * * WScript.echo d_keys(i) & vbTab & d.Item(d_keys(i)) > Next- Hide quoted text - > > - Show quoted text - didn't explain myself perfectly. the part of the file name after the underscore is random, so i need to find the last file created/modified in the folder per file group. is it hard to modify your code to do this? |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Sorting files On Jun 1, 9:08*am, Hugh Janus <my-junk-acco...@xxxxxx> wrote: Quote: > On Jun 1, 3:02*pm, "Heinz" <Spacewalker4711(noSpam)@hotmail.com> > wrote: > > > Quote: > > "Hugh Janus" <my-junk-acco...@xxxxxx> schrieb im Newsbeitragnews:2a1d5342-1549-4982-9420-9273923562c1@xxxxxx Quote: Quote: > > > Hi all, Quote: Quote: > > > I wrote a script that looped through all files in a folder, however, > > > it seems that the files created in the collection are not appearing in > > > alphabetical order, which is causing my routine to fail. *Basically, I > > > want to loop through all files until I find the last one created, and > > > then perform an action on this file. Quote: Quote: > > > Does anyone know how to do this? *Can they provide an example etc? > > > Below is listed an example the files that I want to process. *I don't > > > know the name of the files in advance, apart from a portion of them. > > > As you can see, the first portion before the underscore does not > > > change, so I want to find the last file written that starts "File123_" > > > and the process this file. *Once done, I need to move onto the next > > > group of files that start "File456_" and process the last one written.. Quote: Quote: > > > File123_abc.txt > > > File123_def.txt > > > File123_ghi.txt > > > File456_ghi.txt > > > File456_abc.txt > > > File456_def.txt Quote: Quote: > > > Hopefully this makes sense :-S Quote: Quote: > > > Thanks in advance Quote: > > Hi, Quote: > > Do you want to find the last file that was written to a folder? > > Maybe you can loop though the folder and check the file attribute "file > > creation date" ... ? Quote: > > To get the file's *attribute you can use something like this: Quote: > > FileAccessInfo ("c:\test.txt") Quote: > > Sub FileAccessInfo(aFile) Quote: > > Dim s, f, fs Quote: > > Set fs = CreateObject("Scripting.FileSystemObject") > > Set f = fs.GetFile(aFile) Quote: > > s = *UCase(afile) & vbCrLf > > s = s & "file was created: " & f.DateCreated & vbCrLf > > s = s & "file was last accessed: " & f.DateLastAccessed & vbCrLf > > s = s & "fiel was last modified: " & f.DateLastModified Quote: > > wscript.echo s Quote: > > End Sub- Hide quoted text - Quote: > > - Show quoted text - > Thanks Heinz, > What I actually need is the last file written to the folder, for each > 'group' of files. *So, as you can see in my example, I need the last > written file starting with File123, and then the last written file > starting with File456. *This is where I am lost. *Any ideas? and the other using the WSH Dictionary class. Using the ArrayList the files are sorted in name order with the dates associated with each, something like this ... sPath = "C:\Documents and Settings\username\someplace" Set DataList = CreateObject("System.Collections.ArrayList") with Createobject("Scripting.FileSystemObject") for each file in .Getfolder(sPath).Files sFilter = left(file.Path, 7) sDate = DateForSort(file.DateLastModified) DataList.Add sFilter & vbTab & Date & vbTab & file.Path next End With ' FSO DataList.Sort ' Just for example ... sLastFile = Split(DataList.Item(DataList.count - 1), vbTab) wsh.echo "File name:", sLastFile(2), "Date:", sLastFile(1) Function DateforSort(sDate) DateforSort = Year(sDate) & "-" _ & Right("0" & Month(sDate), 2) & "-" _ & Right("0" & Day(sDate), 2) & " " _ & Split(sDate)(1) & " " & Split(sDate)(2) End Function This will get you the list sorted the way you say you want it, but does not provide the final result. The other approach constructs a dictionary with the first six or seven characters of the file name as the item key - adding new keys as needed, while updating the file name and date for existing keys. Once all files are processed, the dictionary contains the names of the files having the latest date, something like this ... sPath = "C:\Documents and Settings\username\someplace" Set objList = CreateObject("Scripting.Dictionary") with Createobject("Scripting.FileSystemObject") for each file in .Getfolder(sPath).Files sFilter = left(file.Path, 7) sDate = DateForSort(file.DateLastModified) if objList.Item(sFilter).Exist Then if sDate > objList.Item(sFilter)(1) Then objList.Item(sFilter) = Array(file.Path, sDate) end if else objList.Add sFilter, Array(file.Path, sDate) end if next for each item in objList oFile = .GetFile(item(0)) ' add process for your file here next End With ' FSO All things considered, I think the dictionary approach makes more sense, as it takes fewer steps to accomplish your stated objective. BTW, this is all "air code", though it is derived from working examples. I think it is syntactically and logically correct, but can make no guarantees. At the very least, it should point you in the right direction. Tom Lavedas *********** |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Sorting files Hugh Janus schrieb: Quote: > Hi all, > > I wrote a script that looped through all files in a folder, however, > it seems that the files created in the collection are not appearing in > alphabetical order, which is causing my routine to fail. Basically, I > want to loop through all files until I find the last one created, and > then perform an action on this file. > > Does anyone know how to do this? Can they provide an example etc? > Below is listed an example the files that I want to process. I don't > know the name of the files in advance, apart from a portion of them. > As you can see, the first portion before the underscore does not > change, so I want to find the last file written that starts "File123_" > and the process this file. Once done, I need to move onto the next > group of files that start "File456_" and process the last one written. > > File123_abc.txt > File123_def.txt > File123_ghi.txt > File456_ghi.txt > File456_abc.txt > File456_def.txt > > Hopefully this makes sense :-S > > > Thanks in advance Const csSDir = ".\tmp\fg" Const csSep = "_" Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) Dim dicGroups : Set dicGroups = CreateObject( "Scripting.Dictionary" ) Dim oFile For Each oFile In oFS.GetFolder( csSDir ).Files WScript.Echo oFile.DateLastModified, oFile.Name Dim aParts : aParts = Split( oFile.Name, csSep ) If 1 = UBound( aParts ) Then If Not dicGroups.Exists( aParts( 0 ) ) Then Set dicGroups( aParts( 0 ) ) = oFile Else If oFile.DateLastModified < dicGroups( aParts( 0 ) ).DateLastModified Then Set dicGroups( aParts( 0 ) ) = oFile End If End If End If Next Dim sKey For Each sKey In dicGroups.Keys WScript.Echo "--", sKey WScript.Echo dicGroups( sKey ).DateLastModified, dicGroups( sKey ).Name Next output: === findOldestFile03: find oldest file in folder with groups == 01.06.2009 17:00:15 abc.txt 01.06.2009 17:00:07 File123_abc.txt 01.06.2009 17:00:15 File123_def.txt 01.06.2009 15:41:05 File123_ghi.txt 01.06.2009 15:41:11 File123_other.txt 01.06.2009 17:00:42 File456_abc.txt 01.06.2009 15:41:23 File456_def.txt 01.06.2009 15:41:29 File456_ghi.txt 01.06.2009 15:41:35 File456_other.txt 01.06.2009 15:41:35 filexx.txt 01.06.2009 15:41:41 nasty_abc.txt 01.06.2009 15:41:47 nasty_def.txt 01.06.2009 15:41:53 nasty_ghi.txt 01.06.2009 15:41:59 nasty_other.txt -- File123 01.06.2009 15:41:05 File123_ghi.txt -- File456 01.06.2009 15:41:23 File456_def.txt -- nasty 01.06.2009 15:41:41 nasty_abc.txt === findOldestFile03: 0 done (00:00:00) ======================= |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Sorting files On Jun 1, 10:43 am, Hugh Janus <my-junk-acco...@xxxxxx> wrote: Quote: > On Jun 1, 3:52 pm, Jason <flaja...@xxxxxx> wrote: > > > Quote: > > On Jun 1, 8:32 am, Hugh Janus <my-junk-acco...@xxxxxx> wrote: Quote: Quote: > > > Hi all, Quote: Quote: > > > I wrote a script that looped through all files in a folder, however, > > > it seems that the files created in the collection are not appearing in > > > alphabetical order, which is causing my routine to fail. Basically, I > > > want to loop through all files until I find the last one created, and > > > then perform an action on this file. Quote: Quote: > > > Does anyone know how to do this? Can they provide an example etc? > > > Below is listed an example the files that I want to process. I don't > > > know the name of the files in advance, apart from a portion of them. > > > As you can see, the first portion before the underscore does not > > > change, so I want to find the last file written that starts "File123_" > > > and the process this file. Once done, I need to move onto the next > > > group of files that start "File456_" and process the last one written. Quote: Quote: > > > File123_abc.txt > > > File123_def.txt > > > File123_ghi.txt > > > File456_ghi.txt > > > File456_abc.txt > > > File456_def.txt Quote: Quote: > > > Hopefully this makes sense :-S Quote: Quote: > > > Thanks in advance Quote: > > Here you go. (Watch for line wraps and indentation) Quote: > > I am assuming that the underscore "_" is the only delimiter in the > > file name, and that from your response to Heinz, that the last > > modified date is not always the last file? Instead the 2nd half of the > > file name identifies that order of the files? Quote: > > The script uses a dictionary object to store the file groups as the > > dictionary keys. As it loops through the folder, it splits the file > > name by the underscore character. The first half (arr_temp(0)) is the > > group name (i.e. File123). If it is the first "group" to be found it > > adds it to the dictionary, along with the second half of the file > > name. If a file from the same group has been encountered it checks the > > 2nd half of the name to see if it is bigger. (Text comparison). If > > it's bigger, it gets replaced. Quote: > > Once it's through with the folder, the last few lines just iterate > > through all the dictionary items and echo back the group and last file > > in that group. Quote: > > Set fso = CreateObject("Scripting.FileSystemObject") > > Set d = CreateObject("Scripting.Dictionary") Quote: > > Set fldr = fso.GetFolder("C:\Temp") > > For each file in fldr.Files > > If InStr(1, file.name, "_") Then > > arr_temp = Split(file.name, "_") > > If d.Exists(arr_temp(0)) Then > > If arr_temp(1) > d.Item(arr_temp(0)) Then > > d.Item(arr_temp(0)) = arr_temp(1) > > End If > > Else > > d.Add arr_temp(0), arr_temp(1) > > End If > > End If > > Next Quote: > > d_keys = d.keys Quote: > > For i = 0 to d.Count-1 > > WScript.echo d_keys(i) & vbTab & d.Item(d_keys(i)) > > Next- Hide quoted text - Quote: > > - Show quoted text - > this looks great, I think we are getting there! However, I guess i > didn't explain myself perfectly. the part of the file name after the > underscore is random, so i need to find the last file created/modified > in the folder per file group. is it hard to modify your code to do > this? retrieve the file names we'll have to store both the file name and the last modified date in the dictionary item. No change to the Key part, that will still be the file group. In the script below, I just used a pipe symbol ( | ) to separate the file name from the date. Used another array from the Split function to pull out what's needed, and compare the dates. Set fso = CreateObject("Scripting.FileSystemObject") Set d = CreateObject("Scripting.Dictionary") Set fldr = fso.GetFolder("C:\Temp") For each file in fldr.Files If InStr(1, file.name, "_") Then arr_temp = Split(file.name, "_") If d.Exists(arr_temp(0)) Then DateCheck = Split(d.Item(arr_temp(0)), "|") If CDate(file.DateLastModified) > CDate (DateCheck(1)) Then d.Item(arr_temp(0)) = file.Name & "|" & file.DateLastModified End If Else d.Add arr_temp(0), file.Name & "|" & file.DateLastModified End If End If Next d_keys = d.keys For i = 0 to d.Count-1 FileName = Split(d.Item(d_keys(i)), "|") WScript.echo "Group:" & d_keys(i) & vbTab & "Last Mod: " & FileName(0) Next |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Sorting files Il giorno Mon, 1 Jun 2009 07:43:42 -0700 (PDT), Hugh Janus <my-junk-account@xxxxxx> ha scritto: Quote: >this looks great, I think we are getting there! However, I guess i >didn't explain myself perfectly. the part of the file name after the >underscore is random, so i need to find the last file created/modified >in the folder per file group. is it hard to modify your code to do >this? creates a file with the most recent file at the top. Check if it is maybe a folder. -- Giovanni Cenati (Bergamo, Italy) Write to "Reventlov" at katamail com http://digilander.libero.it/Cenati (Esempi e programmi in VbScript) -- |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Sorting files Reventlov schrieb: Quote: > Il giorno Mon, 1 Jun 2009 07:43:42 -0700 (PDT), Hugh Janus <my-junk-account@xxxxxx> > ha scritto: Quote: >> this looks great, I think we are getting there! However, I guess i >> didn't explain myself perfectly. the part of the file name after the >> underscore is random, so i need to find the last file created/modified >> in the folder per file group. is it hard to modify your code to do >> this? > dir /o-d /b >list.txt > creates a file with the most recent file at the top. Check if it is maybe a folder. > by file name prefix). Is there a way to make dir sort on names *and* dates? |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Sorting files in a folder | VB Script | |||
| Automatic Sorting of Files | Vista General | |||
| Autmatically Sorting Files | Vista General | |||
| WMP - sorting files | Vista music pictures video | |||
| Sorting files vertically? | Vista file management | |||