Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NT Batch to VBS - File Manipulation

Status
Not open for further replies.

digimahn

IS-IT--Management
Jul 25, 2002
27
US
Here is the Batch file first
Code:
dir %1\*.gbk /o-d /b >templist.fil
for /F "skip=%2" %%A in (templist.fil) do (del %1\%%A)
del templist.fil
Line 1 does lists all GBK files in VARIABLE's directory by date (new first) and redirects output to a file.
Line 2 Process this list skipping the first VARIABLE GBK files in that list, and deletes the remaining files based on data not skipped in that list.
Line 3 Deletes templist.fil

Now I have tried several scripted approaches to this and can not figure this little bugger out! Can someone tell by simply pointing me in the right direction...
Dictionary? or Array? ReDimmed or not?
and I am sure I can figure it out with about 5 hours of hacking at it <GRIN>

Thanks guys!
 
Hiya,

Any chance of knowing what it ISN'T doing that you want it to do?

Regards,

Darrylles &quot;Never argue with an idiot, he'll bring you down to his level - then beat you with experience.&quot;
 
Darrylles,

Thank you for peeking in here...
I set up an array originally (VBScript) to read all *.GBK files in a particular dir (Using WMI to scan the drive for all GBK files is a TAD overkill). In the array building I would msgbox echo each item as it was encountered - just to make sure it was in the array. I only built the array if the object count > 4 (or any variable number for the sake of reuseability).

The array built fine, but I could not figure out how to get the script to delete the OLDEST GBK's while maintaining the newest 4. It would delete them all or would delete none of them.

I have not tried a dictionary yet as I am kinda new to this stuff and am trying to figure this out on my own. If you search this thread for my one other post (quite some time ago) you will see I have been trying for some time! Any way I got so friggin frustrated that I just deleted my work - STOOPID mistake - and was going to start over. If you want to &quot;proofread&quot; my script I will try to get back to where I was and let you look at it.

What's the best way to REALLY learn this stuff? I have several books (they do help), and training courses and all, but I can't help but feel that there is a fundamental key I am missing out on that will magically glue all these pieces together for me...
DigiMahn
 
Well I recovered my script works- that was a chore in itself.

I am isolating files in their own dir(s) and deleting them regardless of file extension since there should only be GBK files in the dir anyway - if there are other types in there some one put them there that should not have and deserves them to get whacked!

Here's what I did and worked out this afternoon with some dedicated time...

[tt]
Code:
'Keeping myself &quot;Honest&quot;
 Option Explicit

'Don't Know if I like this or not :)
 On Error Resume Next

'My Variables
 Dim fso
 Dim Folder
 Dim File
 Dim FileDate
 Dim OldestFile
 Dim CurrentDate: CurrentDate = ConvertDate(Now)

'Assign &quot;Dim fso&quot; Varible
 Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)

'Assingn &quot;Dim Folder&quot; Variable
 Set Folder = fso.GetFolder(&quot;<drive letter>:\<your path here>&quot;)

'Check file count, if less than or = to 4 files then quit
 If Folder.Files.Count <= 4 Then
   msgbox &quot;Nothing to do&quot;
  Wscript.Quit
 End If

'Check file count, if more than 4...
 do while Folder.Files.Count > 4
 msgbox &quot;There are &quot; & Folder.Files.Count & &quot; Files in &quot; & Folder     'Just Checking on things

'Begin Function against each file in &quot;Dim Folder&quot; Variable
CurrentDate = ConvertDate(Now)
    For Each File In Folder.Files
        'Use function against file's Date Last Mod value
        FileDate = ConvertDate(File.DateLastModified)
        'Use FileDate function work's value against a straight CurrentDate or &quot;Now&quot; value - if it's less...
        If FileDate < CurrentDate Then
            'Use FileDate value for CurrentDate value
            CurrentDate = FileDate
            'Use the file's path for OldestFile variable value
            OldestFile = File.Path
        End If
    Next
msgbox &quot;fso.DeleteFile OldestFile to occur on &quot; & OldestFile     'Just Checking on things again
       'kill the oldest file(s) one at a time
       fso.DeleteFile OldestFile
Loop

'Reset some variables and quit
Set fso = Nothing
Set Folder = Nothing
Wscript.Quit

'-------------------------------------------------------
Function ConvertDate(Date)
Dim Yr
Dim Mth
Dim Dy

Yr = Year(Date)
Mth = Month(Date): If Len(Mth) = 1 Then Mth = &quot;0&quot; & Mth
Dy = Day(Date): If Len(Dy) = 1 Then Dy = &quot;0&quot; & Dy
ConvertDate = Yr & Mth & Dy
End Function
[/tt]

Thanks anyway - I posted this here for anyone that may need it...

I have another question that I will post in a separate thread in the morning - Regarding the date coding used above...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top