Dos or Win procedure

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Dos or Win procedure

Post by bknight »

I'm looking for a procedure that would delete files with an extension of .xyz from a folder and all subfolders. The subfolders could be 3d deep.
One filter would be files created/modified more than two months ago.

These folders/files are not contained in My Documents

User avatar
John Gray
PlatinumLounger
Posts: 5401
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Dos or Win procedure

Post by John Gray »

Are you familiar with the Command Prompt?
If so, run the two commands:
cd C:\path\to\top\folder
:: obviously change the above to the correct path!
del /s *.xyz

If not, ask again!

That will simply delete all files with the xyz extension from the top folder and all lower-level folders.
Any form of selection/ignoral based on modified dates is quite another matter...
John Gray

Venison is quiet deer, and quite dear.

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

Does that delete all the files in all the subfolders?
If so then I still need to preserve files younger than 2 months ago.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Dos or Win procedure

Post by HansV »

Here is a VBA solution. It can be run from any Windows application that supports VBA (Word, Excel, Access, ...)

Code: Select all

Sub DeleteAll()
    ' Path of top folder
    Const strFolder = "C:\MyFolder\MySubfolder"
    Dim objFSO As Object
    Dim objFolder As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolder)
    Call DeleteFiles(objFolder)
End Sub

Sub DeleteFiles(objFolder As Object)
    Dim objFile As Object
    Dim objSubfolder As Object
    ' Check files
    For Each objFile In objFolder.Files
        If LCase(objFile.Name) Like "*.xyz" Then
            If objFile.DateLastModified < Date - 60 Then
                objFile.Delete
            End If
        End If
    Next objFile
    ' Check subfolders
    For Each objSubfolder In objFolder.SubFolders
        Call DeleteFiles(objSubfolder)
    Next objSubfolder
End Sub
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Dos or Win procedure

Post by HansV »

I have moved this thread from Test Area to Windows General.
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

Hans
Good job but a couple of issues so far.

Code: Select all

Sub DeleteOldFiles()
    ' Path of top folder
    Const strFolder = "C:\Program Files (x86)\Tradestation 9.5\Scans"
    Dim objFSO As Object
    Dim objFolder As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strFolder)
    Call DeleteFiles(objFolder)
End Sub

Sub DeleteFiles(objFolder As Object)
    Dim objFile As Object
    Dim objSubfolder As Object
    ' Check files
    For Each objFile In objFolder.Files
        If LCase(objFile.Name) Like "*.tsrslts" Then
            If objFile.DateLastModified < Date - 60 Then
                objFile.Delete
            End If
        End If
    Next objFile
    ' Check subfolders
    For Each objSubfolder In objFolder.SubFolders
        Call DeleteFiles(objSubfolder)
    Next objSubfolder
End Sub
When attempting to delete a file I get a run time error 70 permission denied; that seems to be an easy fix.
The second is perhaps more questionable. The scans folder contains two sub folders each with more sub folders. The first is named by the software and I am unable/unwilling to change. "_root" or " _root". The routine did not check any sub folders. The other folder is checking sub folders and ran into the first delete.

ETA: I jumped over the delete command and continued stepping through the code the next delete was in the "_root", so the first issue appears to be a non-issue. I just need a step to grant permission to delete. See you in the morning.

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: Dos or Win procedure

Post by JoeP »

You should be aware that basing decisions on the date created and/or date modified for a file is problematic in Windows. Those dates are not consistent.
Joe

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

I'm attempting to automate deletion of older files. The date only allows more current data to remain in the files. It seems to me that using a cutoff of two months sufficiently does that and the two months is arbitrary.
Stepping through the procedure yesterday "found" files that were written, in this case, more than two months ago.
Presently I'm looking for I'm looking for a permission statement that will allow to the code to delete such files.

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

Is there no way to gain access for a permission?

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Dos or Win procedure

Post by HansV »

I cannot help you with that, sorry.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Dos or Win procedure

Post by ChrisGreaves »

bknight wrote:
05 Dec 2021, 16:40
I'm looking for a procedure that would delete files with an extension of .xyz from a folder and all subfolders. The subfolders could be 3d deep. One filter would be files created/modified more than two months ago.
OK. I'll stick out my neck. Better DOS wizards than I can fill in the details (if not I'll give it a shot), but here is how I would look at it:-
(1) Your main problem is identifying candidate files for deletion.
(2) Notwithstanding Joe’s caution about using Created/Modified dates ...
(3) ... Set up a RAM disk or similar as a work drive\folder
(4) Use ROBOCOPY to select and copy those files which satisfy your criteria.
(5) Your criteria include at least a file extension and a date value.
(6) ROBOCOPY switches of immediate interest are /S /LEV /CREATE /MINAGE /MINLAD
(7) There are more switches for finessing Robocopy
(8) With ROBOCOPY you are able to create a copy of the folder tree with only files that satisfy your criteria.
(9) Note that /CREATE merely creates the directory tree on your RAM drive, so you can probably identify files across a network with just a 5MB RAM disk
(10) At this point your RAM disk identifies candidate files for deletion, albeit with the wrong drive letter
(11) I know VBA, so at this point I would feed my redirected DIR list of my RAM drive into VBA, swap the drive letters, and issue a series of VBA “KILL” file commands based on the list.
(12) There are DOS techniques for parsing and modifying strings, but I am not good at that. I believe that the CMD prompt facilities are strong enough to accomplish switching the drive letters.

You can view Robocopy parameters at https://docs.microsoft.com/en-us/window ... s/robocopy

I hope that this helps.
Chris
Last edited by ChrisGreaves on 07 Dec 2021, 16:12, edited 1 time in total.
An expensive day out: Wallet and Grimace

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Dos or Win procedure

Post by ChrisGreaves »

ChrisGreaves wrote:
07 Dec 2021, 15:45
...Better DOS wizards than I can fill in the details ...
In the meantime the old brain chugs along ...
At all stages of the procedure you can send a list of files to your hard drive.
You could split your procedure into
(a) ROBOCOPY to create the file list on the ram drive and spit out that list
(b) Manually eyeball the list to make sure that “MyMasterPasswordList.xyz” did not get swept up into the net
(c) Run the second part of the procedure to effect the deletions.
Further more
(d) You could rerun the first part of the procedure to see which files had NOT gotten themselves deleted; you have mentioned problems with permissions and so on.
(e) If you are in a huge cooperate environment [ I feel sorry for you ] you could emit candidate lists of each area of your server to department heads and ask for their conformation before deleting files.
More to this post as I think of it.
Cheers
Chris
An expensive day out: Wallet and Grimace

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Dos or Win procedure

Post by StuartR »

This is beyond me too, but there are some suggestions on vbforums
https://www.vbforums.com/showthread.php ... ermissions
StuartR


User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Dos or Win procedure

Post by ChrisGreaves »

ChrisGreaves wrote:
07 Dec 2021, 15:57
More to this post as I think of it.
Attached is a simple example of the Robocopy commands. I look for "backup of" word document files because I have no *.xyz.
Now on to the string parsing fun ...
Cheers
Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

ChrisGreaves wrote:
07 Dec 2021, 17:45
ChrisGreaves wrote:
07 Dec 2021, 15:57
More to this post as I think of it.
Attached is a simple example of the Robocopy commands. I look for "backup of" word document files because I have no *.xyz.
Now on to the string parsing fun ...
Cheers
Chris
If you were able to write/create files to that drive, I assume that you had permissions, which I didn't have.
Thanks

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Dos or Win procedure

Post by ChrisGreaves »

bknight wrote:
08 Dec 2021, 17:29
If you were able to write/create files to that drive, I assume that you had permissions, which I didn't have.
Hi BNight.
If you are referring to my use of "drive E:", then yes.
But you can, should, change my use of "E:\" to be a path to wherever you can write.
I gather that you are on a network, and you are probably assigned some space on a network drive which (space) is mapped to some drive letter other than C: I assume that your drive is "U:", although you will know which letter is used.

Whatever is "your" network drive, as long as you can create a folder there, suppose you call the folder "ChrisGreaves" so that it doesn't get confused with any other folder related to your work, then wherever I have written "E:\" you can write "U:\ChrisGreaves".
Does that make sense now?

Cheers
Chris
An expensive day out: Wallet and Grimace

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

The drive is listed on the third line of code
Const strFolder = "C:\Program Files (x86)\Tradestation 9.5\Scans"
Which I don't have permissions.

Is there something I'm missing in your code?

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Dos or Win procedure

Post by ChrisGreaves »

bknight wrote:
08 Dec 2021, 17:59
Const strFolder = "C:\Program Files (x86)\Tradestation 9.5\Scans"
I think that we are all confused by now.
Let's see if I can straighten this out.

In this post Hans supplied a VBA sample. This is VBA program code that contains the line "Const strFolder = "C:\MyFolder\MySubfolder"". Much as I love Hans, only Hans is responsible for Hans's code. Did you mean you most recent reply to go to Hans?

In this post I laid out a 12-step English language proposal for a RoboCopy/DOS solution. My proposed solution does not use VBA.

In this post you referred to a post of mine with "If you were able to write/create files to that drive, I assume that you had permissions, which I didn't have." although now I suspect that you were referring to something in Hans's code, not my Robocopy/DOS solution.

So now I think that your most recent post in which you say "The drive is listed on the third line of code; Const strFolder = "C:\Program Files (x86)\Tradestation 9.5\Scans" Which I don't have permissions. Is there something I'm missing in your code?" should be directed towards Hans.

Hans and I have each provided a different proposed solution for you. Hans's proposal is written in VBA, and my proposal is written using RoboCopy and the DOS command facilities. Two different people, two different solutions, each solution aimed at solving your problem, but each with a unique approach!'

Cheers
Chris
An expensive day out: Wallet and Grimace

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

I am attempting to delete files that meet criteria. So if RoboCopy bypasses the admin permission requirement, then copying to identify files would be good, if not then the method fails.
Thanks

bknight
BronzeLounger
Posts: 1353
Joined: 08 Jul 2016, 18:53

Re: Dos or Win procedure

Post by bknight »

To give everyone how important circumventing the permissions, when this macro is run there will be approximately 1020 deletions. Yes, many could be selected as a group for deletion, but this takes 34 minimum select low date and high date then delete and then answer yes to permissions. That is tedious and takes about 15- 30 minutes. The routine should eliminate or bypass the permissions and run smoothly.