Page 1 of 1

Delete folders specified by user

Posted: 13 Apr 2011 01:21
by Devil
I have got folders in the format,
D:\Myfiles\ABC\04April\06
D:\Myfiles\ABC\04April\07
D:\Myfiles\ABC\04April\08
D:\Myfiles\ABC\04April\09

D:\Myfiles\DEF\04April\06
D:\Myfiles\DEF\04April\07
D:\Myfiles\DEF\04April\08
D:\Myfiles\DEF\04April\09

D:\Myfiles\GHI\04April\06
D:\Myfiles\GHI\04April\07
D:\Myfiles\GHI\04April\08
D:\Myfiles\GHI\04April\09

04April part changes each month to 05May, 06June and so on..
So in each folder say ABC, DEF there will be a month folder in the above format and within that there wil be date folders in the format 01,02,03... . Within these date folders have got numerous files.

What I need is a batch file which will delete the files from a date range specified by me. So if i say, 01 to 05, it will search all the folders within D:\Myfiles and delete it.

Thanks in advance.

Re: Delete folders specified by user

Posted: 13 Apr 2011 11:45
by !k

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
if "%2"=="" (echo call %0 01 09 &pause &exit /b)
set "from=10%1"
set "to=10%2"
set "fldr=d:\Myfiles"

dir /b/s/ad "%fldr%\*" |findstr /rec:"%fldr:\=\\%\\...\\[0-1][0-9][A-z]*\\[0-3][0-9]" >"%temp%\dirs.tmp"

for /l %%d in (%from%,1,%to%) do (
  set d=%%d
  for /f "delims=" %%r in ('findstr /ec:"!d:~-2!" "%temp%\dirs.tmp"') do echo rd /s /q "%%r"
)
del /q "%temp%\dirs.tmp"

if all right, remove last "echo"

Re: Delete folders specified by user

Posted: 14 Apr 2011 01:21
by Devil
!k Thanks for your prompt reply.

As am a novice in this, please be patient.

I copied this code, i assume the range of files to be deleted is to be specified here (echo call %0 01 09 &pause &exit /b)

However after running the batch, nothing happened. Am I doing something wrong.

Devil

Re: Delete folders specified by user

Posted: 14 Apr 2011 06:23
by scienceguru1.bat
try removing the echo in a copy of the batch file. i am still a novice, but i see 2 commands, one after another, and i have found that doesnt really work.

Re: Delete folders specified by user

Posted: 14 Apr 2011 11:28
by !k
Devil wrote:i assume the range of files to be deleted is to be specified here (echo call %0 01 09 &pause &exit /b)

No. It's simple help for usage. I work with not Explorer, but TotalCommander and i just type "kil'm 01 09" in command prompt and run.
For Explorer try next code. If all ok, erase "echo" in do echo rd /s /q "%%r" and final "pause"

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
set /p "from=Kill from "
set /p "to=to "
set "from=10%from%"
set "to=10%to%"
set "fldr=d:\Myfiles"

dir /b/s/ad "%fldr%\*" |findstr /rec:"%fldr:\=\\%\\...\\[0-1][0-9][A-z]*\\[0-3][0-9]" >"%temp%\dirs.tmp"

for /l %%d in (%from%,1,%to%) do (
  set d=%%d
  for /f "delims=" %%r in ('findstr /ec:"!d:~-2!" "%temp%\dirs.tmp"') do echo rd /s /q "%%r"
)
del /q "%temp%\dirs.tmp"
pause

Re: Delete folders specified by user

Posted: 16 Apr 2011 00:00
by Devil
!k, you are the man.

Its working gr8.

Thanks for all your help.

Re: Delete folders specified by user

Posted: 16 Apr 2011 00:55
by Devil
!k, on further experimenting with your code, it works only if the folder names are of 3 characters say "ABC", DEF", "SRI" etc..
However when I try to apply to folders which are having more / less than 3 characters including spaces, it fails.

Can you please modify the code to achieve this?

Re: Delete folders specified by user

Posted: 16 Apr 2011 06:52
by !k
Read help about "findstr" command
Code ... means 3 characters, .* maybe what you want