Delete folders specified by user

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Devil
Posts: 4
Joined: 13 Apr 2011 01:02

Delete folders specified by user

#1 Post by Devil » 13 Apr 2011 01:21

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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Delete folders specified by user

#2 Post by !k » 13 Apr 2011 11:45

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"
Last edited by !k on 16 Apr 2011 07:16, edited 1 time in total.

Devil
Posts: 4
Joined: 13 Apr 2011 01:02

Re: Delete folders specified by user

#3 Post by Devil » 14 Apr 2011 01:21

!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

scienceguru1.bat
Posts: 44
Joined: 01 Jan 2011 20:54

Re: Delete folders specified by user

#4 Post by scienceguru1.bat » 14 Apr 2011 06:23

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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Delete folders specified by user

#5 Post by !k » 14 Apr 2011 11:28

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
Last edited by !k on 16 Apr 2011 07:14, edited 1 time in total.

Devil
Posts: 4
Joined: 13 Apr 2011 01:02

Re: Delete folders specified by user

#6 Post by Devil » 16 Apr 2011 00:00

!k, you are the man.

Its working gr8.

Thanks for all your help.

Devil
Posts: 4
Joined: 13 Apr 2011 01:02

Re: Delete folders specified by user

#7 Post by Devil » 16 Apr 2011 00:55

!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?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Delete folders specified by user

#8 Post by !k » 16 Apr 2011 06:52

Read help about "findstr" command
Code ... means 3 characters, .* maybe what you want

Post Reply