Select & Delete files inside a tree

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Select & Delete files inside a tree

#1 Post by Ranjit » 12 Dec 2015 04:32

I have a large directory tree having over 30 sub-directories, each contains between 5 to 50 files.
D:\Instal\Maintainance
D:\Instal\Maintainance\Pl Files
D:\Instal\Maintainance\Software
D:\Instal\Maintainance\Pipe\Update
D:\Instal\Misc
D:\Instal\Misc\Emails
D:\Instal\Misc\Sent Emails
D:\Instal\Misc\Temp\Temporary Files
... and so on.
Some of files and new and some are regenerated.
My need is as below :
1. I need to display contents of each sub-directory, mark files for deletion with option "D:\Misc\Test.Txt - Delete ? Yes/No" preferably not in CMD display but like screenshot below :
http://i.imgur.com/dvrjz0I.png
2. To store deleted file path into a list file, which may be deleted automatically at next run, before display begins.
3. To update list file by removing path for files not existing before display begins.
4. After deletion, move over to next sub directory and repeat process.
I tried best, as below, which gave me a 10 page text file.
Dir /B /S D:\Instal\. > Search.Txt
I beg for a suitable Batch script please.
Thank you in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Select & Delete files inside a tree

#2 Post by foxidrive » 12 Dec 2015 05:43

Can I clarify that you want to press yes or no for each file that was in the 10 page list you created?
That's going to be quite a lot of clicking.

You also want a log file of each file that is deleted, and it should be cleared each time this batch script is run.
If that is the case, then what is point 3. for?

Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Re: Select & Delete files inside a tree

#3 Post by Ranjit » 12 Dec 2015 14:52

foxidrive wrote:Can I clarify that you want to press yes or no for each file that was in the 10 page list you created?
That's going to be quite a lot of clicking.
You also want a log file of each file that is deleted, and it should be cleared each time this batch script is run.
If that is the case, then what is point 3. for?

You are right Sir.
1. It has to be a lot of clicking for the first time because there are 500+ files involved.
2. This deletion process is very frequent, almost daily.
3. The names of files to be retained or deleted are constant and permanent.
4. There could be log files remembering selected option.
5. Before running next time, these log files be updated, removing files which no more exist in directories.
6. In display, file names should not appear, for re-selection.
7. This batch script being permanent, it is preferred if an option is available to edit saved log files, before selection starts.
In my original post, I think I repeated #3 which I already told in #2.
Thank you Sir.

Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Re: Select & Delete files inside a tree

#4 Post by Ranjit » 12 Dec 2015 23:48

I wish to add further information, which might help to create actual script.
My target directory is on D: drive :
http://i.imgur.com/JUYnLic.jpg
My working directory should be D:\Instal\Maintainance
http://i.imgur.com/8NWiJiJ.jpg
Thank you Sir.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Select & Delete files inside a tree

#5 Post by foxidrive » 13 Dec 2015 05:02

Ranjit wrote:2. This deletion process is very frequent, almost daily.
3. The names of files to be retained or deleted are constant and permanent.


The point 3 above could be very important detail in creating this script.

Do you mean that you want to delete every file that is not in a permanent list,
or that you wish to delete only the files that ARE in a permanent list.

Or have I misunderstood this aspect?

Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Re: Select & Delete files inside a tree

#6 Post by Ranjit » 13 Dec 2015 10:37

foxidrive wrote:
Ranjit wrote:2. This deletion process is very frequent, almost daily.
3. The names of files to be retained or deleted are constant and permanent.


The point 3 above could be very important detail in creating this script.

Do you mean that you want to delete every file that is not in a permanent list,
or that you wish to delete only the files that ARE in a permanent list.

Or have I misunderstood this aspect?

Let me further explain more precisely what I need.
1. When the first time batch file is run, there are target 500 files. All should be displayed, to earmark deletion individually with "YES/NO"
2. I earmark 150 files for deletion. This information of 350 is stored in Keep.Txt & 150 in Delete.Txt. The first time operation is over.
3. Next time when I start Batch, both list text files get updated, removing entries which are not present in target directories. This is required because unless Keep.Txt & Delete.Txt are trimmed they may get inflated into megabytes.
4. After this, sometimes I may need to manually edit Delete.Txt. Example : Originally had 150 files. Out of while now there are only 120 remaining. I want to remove another 30, so only 90 remain for automatic deletion.
5. Same process for Keep.Txt. Example : Originally had 350 files. Out of while now there are only 280 remaining. I want to remove another 10, so only 270 remain for automatic keep.
6. The above edit process will perhaps be required at 7-21 days span. "The names of files to be retained or deleted are constant and permanent," subject to edit else I may lose relevance of the Txt files.
I hope this will help you. If any other clarification is needed I shall be glad to post.
Thank you in advance, Sir.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Select & Delete files inside a tree

#7 Post by Aacini » 13 Dec 2015 13:26

Excuse me. I wrote a Batch file for your problem based on the original specifications. When I open this topic in order to post my code, I found a new reply from you that contain a series of different specifications! For example, you first said "To store deleted file path into a list file", but now you said: "This information of 350 is stored in Keep.Txt & 150 in Delete.Txt"; that is, you describe two files when previously just one was mentioned! Also, all the new information about what you need to do "sometimes" was not mentioned before and is pretty irrelevant.

Previously you mentioned: "it is preferred if an option is available to edit saved log files", and now you said: "4. After this, sometimes I may need to manually edit Delete.Txt". I don't know if you realize that the way to "edit saved log files" is via Notepad.exe Windows program, so I don't understand if you want that the Batch program include an option to start Notepad; otherwise, what is the purpose of give us such information? In the same case are your extensive explanations about what the manual editions comprise. Ask yourself: what is the relation of all those manual editions with the program? Should some manual edition of the data modify program operation? If you conclude that the answer is "no", then you should realize that force us to read all those descriptions with no purpose just distract us from the real request! You should keep your request as simple as possible if you want that we read it, understand it and try to solve it.

I posted my code below anyway, but you must realize that changing specifications means that part of original code will go to trash and new code must be written again, and I certainly don't like to do so. You should carefully read the first post in this forum entitled "How to get help for a batch script - quickly!"

Code: Select all

@echo off
setlocal EnableDelayedExpansion

if exist ListFile.txt goto nextRun

cls
echo This is the first time this program run: Create the list of files
echo -^> You may cancel this process with Ctrl-C and run again the program later;
echo    the list created so far will be preserved.
echo/
echo For each file, answer the "Mark file for deletion?" question with Y or N:
echo/

REM "1. I need to display contents of each sub-directory, mark files for deletion with option Yes/No"
for /F "delims=" %%a in ('Dir /B /S D:\Instal') do (
   set /P "=- %%a  " < NUL
   set "choice="
   for /F %%b in ('choice /N') do set "choice=%%b"
   if not defined choice goto break
   echo !choice!
   REM "2. To store deleted file path into a list file,"
   echo !choice!:%%a>> ListFile.txt
)
:break
echo/
echo/
echo Marked files will be deleted automatically at next run
goto :EOF


:nextRun
REM "... which may be deleted automatically at next run, before display begins."
(for /F "tokens=1* delims=:" %%a in (ListFile.txt) do (
   if exist "%%b" (
      if %%a equ Y del "%%b" > NUL
      REM 3. To update list file by removing path for files not existing before display begins.
      rem    that is, by preserving path for files that exists
      echo %%a:%%b
   )
)) > ListFile.new
move /Y ListFile.new ListFile.txt


cls
echo For each file, answer the "Mark file for deletion?" question with Y or N:
echo/
for /F "delims=" %%a in ('Dir /B /S D:\Instal') do (
   find "%%a" ListFile.txt > NUL
   if errorlevel 1 (
      set /P "=%%a  " < NUL
      for /F %%b in ('choice /N') do set "choice=%%b"
      echo !choice!
      echo !choice!:%%a>> ListFile.txt

   )
)
echo/
echo/
echo Marked files will be deleted automatically at next run

Antonio

Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Re: Select & Delete files inside a tree

#8 Post by Ranjit » 13 Dec 2015 14:54

foxidrive wrote:Do you mean that you want to delete every file that is not in a permanent list,
or that you wish to delete only the files that ARE in a permanent list.
Or have I misunderstood this aspect?

I had responded in my post to above. I tried to suggest a few options to explain elaborately to get closer to my needs.

Aacini wrote:Excuse me. I wrote a Batch file for your problem based on the original specifications. When I open this topic in order to post my code, I found a new reply from you that contain a series of different specifications For example, you first said "To store deleted file path into a list file", but now you said: "This information of 350 is stored in Keep.Txt & 150 in Delete.Txt"; that is, you describe two files when previously just one was mentioned! Also, all the new information about what you need to do "sometimes" was not mentioned before and is pretty irrelevant.Previously you mentioned: "it is preferred if an option is available to edit saved log files", and now you said: 4. After this, sometimes I may need to manually edit Delete.Txt". I don't know if you realize that the way to "edit saved log files" is via Notepad.exe Windows program, so I don't understand if you want that the Batch program include an option to start Notepad; otherwise, what is the purpose of give us such information? In the same case are your extensive explanations about what the manual editions comprise. Ask yourself: what is the relation of all those manual editions with the program? Should some manual edition of the data modify program operation? If you conclude that the answer is "no", then you should realize that force us to read all those descriptions with no purpose just distract us from the real request! You should keep your request as simple as possible if you want that we read it, understand it and try to solve it.

I had later suggested two files because running batch file second time the files in Keep.Txt would not be displayed in select list. As you suggested, file list can be modified by Notepad.Exe.
However Sir,
Let me try your code and I come back if there are any problems.
Please forgive me and ignore my irrelevant explanations.
Thank you in advance for taking interest Sir.

Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Re: Select & Delete files inside a tree

#9 Post by Ranjit » 13 Dec 2015 19:10

Aacini Sir,

I tried your code on InstalCleaner.Cmd. When run It gave error. also did not select first file. Screenshot :
http://i.imgur.com/g6Nrhhr.png
Then I edited file.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

if exist ListFile.txt goto nextRun

cls
echo This is the first time this program run: Create the list of files
echo -^> You may cancel this process with Ctrl-C and run again the program later;
echo    the list created so far will be preserved.
echo/
echo For each file, answer the "Mark file for deletion?" question with Y or N:
echo/

REM "1. I need to display contents of each sub-directory, mark files for deletion with option Yes/No"
for /F "delims=" %%a in ('Dir /B /S D:\Instal') do (
   set /P "=- %%a  " < NUL

:: REM OLD   set "choice="
:: REM NEW  set choice=""
   
   set choice=""

   for /F %%b in ('choice /N') do set "choice=%%b"
   if not defined choice goto break
   echo !choice!
   REM "2. To store deleted file path into a list file,"
   echo !choice!:%%a>> ListFile.txt
)
:break
echo/
echo/
echo Marked files will be deleted automatically at next run
goto :EOF


:nextRun
REM "... which may be deleted automatically at next run, before display begins."
(for /F "tokens=1* delims=:" %%a in (ListFile.txt) do (
   if exist "%%b" (
      if %%a equ Y del "%%b" > NUL
      REM 3. To update list file by removing path for files not existing before display begins.
      rem    that is, by preserving path for files that exists
      echo %%a:%%b
   )
)) > ListFile.new
move /Y ListFile.new ListFile.txt


cls
echo For each file, answer the "Mark file for deletion?" question with Y or N:
echo/
for /F "delims=" %%a in ('Dir /B /S D:\Instal') do (
   find "%%a" ListFile.txt > NUL
   if errorlevel 1 (
      set /P "=%%a  " < NUL
      for /F %%b in ('choice /N') do set "choice=%%b"
      echo !choice!
      echo !choice!:%%a>> ListFile.txt

   )
)
echo/
echo/
echo Marked files will be deleted automatically at next run

it created Listfile.Txt, 516,724b and CMD window closed. Screenshot :
http://pastebin.com/JLgndNzh

When I opened Listfile.Txt, it had directory names listed too, which I think should not be there and would hinder selection, After reading Listfile.Txt, I think it should store selected file names for automatic "Keep" and delete files which do not appear in Listfile.Txt, when selection process is over, not when run next.
Next run, before staring, Listfile.Txt be updated by removing non-existent target files. This way, files selection process will be minimized in subsequent runs.
Sir. I would prefer the selection process in Msgbox like thing than CMD window, if possible.
Thank you Sir.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Select & Delete files inside a tree

#10 Post by foxidrive » 14 Dec 2015 08:34

Ranjit, I'd like to briefly comment on batch files and programming, to help you follow why the comments were made.

A batch file, like any program, is a set of exact instructions to perform an exact task.
If the task is not described accurately then any code that is given to you will simply not work, or not work reliably.

Another benefit of giving good information is that the volunteers can write the simplest, fastest, and most appropriate code - because they don't have to cater for situations that don't exist in the actual task.


The people here love batch files and programming and are always willing to help - but they have all seen questions in the past where the task is poorly described, and bang their heads on their keyboard as the code that was provided has to be re-written when more details are finally revealed - because the question-writer comes back and says "The code doesn't work!"

Real details about the actual task helps you, because you're likely to get code swiftly, and code that works.

Just hide sensitive data in the way mentioned in Aacini's link viewtopic.php?f=3&t=6108

Ranjit
Posts: 20
Joined: 26 Nov 2015 20:39

Re: Select & Delete files inside a tree

#11 Post by Ranjit » 14 Dec 2015 09:42

foxidrive wrote:The people here love batch files and programming and are always willing to help - but they have all seen questions in the past where the task is poorly described, and bang their heads on their keyboard as the code that was provided has to be re-written when more details are finally revealed - because the question-writer comes back and says "The code doesn't work!"
Real details about the actual task helps you, because you're likely to get code swiftly, and code that works.
Just hide sensitive data in the way mentioned in Aacini's link viewtopic.php?f=3&t=6108

foxdrive Sir,
I respond in three parts.
1. I follow ethics 100% as described in mentioned topic :
Please be respectful to the people who give their free time to provide you with code, please come back and tell them if the code helped, and don't forget to say thank you after you receive some help. Be polite - social lubrication works on the Internet too.
By default I fully appreciate the selfless support all experts here do to me.
2. I am not very expert on coding part, but when I post a problem, I try explain my needs in precise details. Being end user, perhaps sometimes I do not analyze how much my information helps experts in coding. Subsequently, when someone takes over and asks additional information, based on questions asked, I try to explain best because the perfect end result helps only me, by the selfless efforts of experts.
I am very sorry when I look dumb in my answers. I beg forgiveness for this. As I see it, in my case, probably moderator has to bare with me.
3. After I get a script to run, there could be programming defects which I also try to point out with best of my ability.
I assure I shall try harder in future to be more specific Sir.
4. Last but not the least, I specifically update forum when my batch file works perfectly with tons of thanks Sir.
Thank you in advance.
This forum is best of bests.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Select & Delete files inside a tree

#12 Post by penpen » 14 Dec 2015 17:20

Just some sidenotes:
You don't need post images of your dos shell, you may use the code tag and copy paste the content of the shell,
by right click on the shell, select "mark" (or similar), then hold down the left mouse button,
move the mouse until all you want is marked, and then hit the "return/enter" key.
Now use "ctrl+v" to paste it to a text view.


Ranjit wrote:I tried your code on InstalCleaner.Cmd. When run It gave error. also did not select first file.
You are probably using winxp, which has no "choice.exe".
So you may use one of these (or any other) alternatives:
- Extract the "choice.exe" from a FreeDos installation (or using "7-zip" or similar).
- Copy paste this alternative choice.bat (maybe you have to change it to fit your purposes).


penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Select & Delete files inside a tree

#13 Post by foxidrive » 14 Dec 2015 18:12

Ranjit wrote:1. I follow ethics 100% as described in mentioned topic :
Please be respectful to the people who give their free time to provide you with code, please come back and tell them if the code helped, and don't forget to say thank you after you receive some help. Be polite - social lubrication works on the Internet too.


You *are* respectful - more so than most people.

What I tried to point out, and obviously was not quite successful, is that 'actual details' means a sample of the filenames and which ones you are keeping, which you discard, and the rationale behind sorting the files.

That's because it is easy to program a solution when we can see what is going on, when we know what is happening.
We may also know a way to solve your problem that you aren't aware of - and the joy of programming is in the details.



The following is a general description of what is common in technical support forums now:

Many people use terms like yours "135 files here" "48 files there" "myfile.txt" "e:\folder" "computer A" and made-up things like these don't show what has to be solved.
It's kind of like asking your son to go down to the newsagent and buy "Magazine Y" and he knows that he has to buy a magazine, but has no idea which one to get.

That's what happens with a huge number of requests for help these days - they want accurate code but expect to be able to get it with inaccurate details.

I'm still willing to help people but I don't go to much coding effort when the task is unclear - instead I'll ask questions and see if the person is willing to provide real details of the task.

Post Reply