Computer Hope

Microsoft => Microsoft DOS => Topic started by: Rodriguez on February 21, 2017, 04:35:22 AM

Title: Batch File Help
Post by: Rodriguez on February 21, 2017, 04:35:22 AM
Hi all, I was hop(e)ing for some assistance with a batch script I have been writing.

I have to delete a bunch of files from thousands of subfolders, and I'm having some trouble figuring out the right syntax for it.

So far I have this:

@echo off
 ;;;remove all temporary subfolders that are older than today
 ;;;del isn't the right command for folders of course
 del "Z:\projects\*_backup\"
 del "D:\localdatatemp\*_backup\"
 
 ;;;remove all temporary files that are older than 3 months ago
 del /S /Q "Z:\projects\*.00??.rvt"
 
 ;;;remove all temporary files that are older than 6 months ago
 del /S /Q "D:\localdatatemp\*.rvt"

 

I am using Windows 10 x64 with system date format as DDMMYYYY, which seems to be important for filtering filedates, but I'm not sure how to go about it as the only example I found was for US filedates.

If I have posted in the wrong section, please let me know.
I would be grateful for any help, thankyou.

Title: Re: Batch File Help
Post by: Squashman on February 21, 2017, 12:26:34 PM
Look at the FORFILES command.  It has the date option you need.
Title: Re: Batch File Help
Post by: Rodriguez on February 27, 2017, 04:17:36 AM
Hi Squashman,

Thankyou for the reply — the FORFILES is very handy

FORFILES /S /D -7 /C "cmd /c del Z:\projects\@fname*.00??.rvt"

works very well, but it doesn't seem to have a feature to remove folders ?
Title: Re: Batch File Help
Post by: Squashman on February 28, 2017, 08:24:36 AM
Hi Squashman,

Thankyou for the reply — the FORFILES is very handy

FORFILES /S /D -7 /C "cmd /c del Z:\projects\@fname*.00??.rvt"

works very well, but it doesn't seem to have a feature to remove folders ?
No more then any other command that was not designed to remove folders or files.  FORFILES just helps you chose the files you need to process.

I bet if you Goolge searched bactch file remove directory, this would be the first link it finds.
https://technet.microsoft.com/en-us/library/bb490990.aspx
Title: Re: Batch File Help
Post by: Rodriguez on March 03, 2017, 07:49:40 AM
Thanks, you pointed me in the right direction -- I found a site that had code to remove wildcard-selected subfolders recursively, which works perfectly for the 10,000s of subfolders I need to manage:

Code: [Select]

FOR /d /r . %%d in (*temp *_backup) do @if exist "%%d" echo "%%d" && RD /s/q "%%d"

Title: Re: Batch File Help
Post by: Squashman on March 03, 2017, 10:16:23 AM
Thanks, you pointed me in the right direction -- I found a site that had code to remove wildcard-selected subfolders recursively, which works perfectly for the 10,000s of subfolders I need to manage:

Code: [Select]

FOR /d /r . %%d in (*temp *_backup) do @if exist "%%d" echo "%%d" && RD /s/q "%%d"

Why are you checking if it exists?  It does exist.  The FOR command would not output the folder name unless it existed.
Title: Re: Batch File Help
Post by: patio on March 03, 2017, 01:22:57 PM
Yer killin me Squash....
Title: Re: Batch File Help
Post by: Squashman on March 03, 2017, 07:28:44 PM
Yer killin me Squash....
Does that mean I can quit my day job?
Title: Re: Batch File Help
Post by: patio on March 03, 2017, 10:47:10 PM
Absolutely...have at it...
Title: Re: Batch File Help
Post by: Rodriguez on March 10, 2017, 07:00:49 AM
Why are you checking if it exists?  It does exist.  The FOR command would not output the folder name unless it existed.

I didn't write the whole syntax, I have only adjusted the wildcard part to search for the folders I need to remove.  If you can suggest a simpler syntax, I'd be happy to try it out, thanks.