SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Geek Stuff

(Solved) - Batch file to empty some folders

(19 posts)
  • Started 1 year ago by SarahJames
  • Latest reply from SarahJames
  • Topic Viewed 628 times

SarahJames
SarahJames
Posts: 6581

Hi guys and gals,

As to be expected I use CCleaner to keep my pc clean, but I also have some folders that I don't want to clean out as often as I normally run Ccleaner.
So I was thinking of a batch file to clean out those folders, where I can put a link to in rocketdock.
But how do I write a batchfile like that?

One of the folders is E:\Chat
I want the visible content erased, but the desktop.ini file shouldn't be touched.

Can someone give me a hand with this? :D

Posted 1 year ago
Top
 
ispalten
ispalten
Posts: 6259

Should be easy...

First you need to change into the folder.

E:
CD \CHAT

Next PROTECT desktop.ini

ATTRIB DESKTOP.INI +R +S +H

Then delete the contents of the folder

DEL *.*

(I've not tried this so you might first want to make a copy of the folder to try, and you might need to send a 'Y' to the DEL command, I forget?)

Lastly unhide DESKTOP.INI

ATTRIB DESKTOP.INI -R -H -S

(some of those attributes might be set already, check with the plain ATTRIB command).

That should do it.

Irv S.

Posted 1 year ago
Top
 
Enthusiast
Enthusiast
Posts: 566

DEL /Q *.*

The command to delete in "Quiet" mode. It doesn't ask Y/N.

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

Now that looks promissing :)
Though not quite sure now how to actually write this thing ...
And why hide / unhide desktop.ini? It is already hidden and system. I can set it permanently to read only if needed.
I've created a copy on my D drive for testing. Will give it a try ...

Edit: nope, my try at writing this is no good. Nothing happens. <LOL>
Could you guide me in baby steps on writing this? Please :)))

Posted 1 year ago
Top
 
ispalten
ispalten
Posts: 6259

Post what you created Sarah, should look like this...

Use NOTEPAD DEL_DIR.BAT

Enter :

========
E:
CD \CHAT
ATTRIB DESKTOP.INI +R +S +H
DEL *.* /Q
ATTRIB DESKTOP.INI -R -H -S
ECHO FOLDER EMPTY CHECK
DIR
============

Irv S.

Posted 1 year ago
Top
 
G41M
Posts: 902

In Notepad type:

@ECHO OFF
E:
CD \CHAT
DEL /Q *.*
EXIT

Save as .bat file

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

Irv's option makes the desktop.ini file visible after running, which is not supposed to happen.
G41M's option works perfectly, but I now understand what went wrong yesterday.
It doesn't erase files from subfolders and I only had a folder with files in it - so everything stayed in place.

What do I need to add to erase subfolders too?

Posted 1 year ago
Top
 
ispalten
ispalten
Posts: 6259

Easy, CD into the sub-folders and issue the DEL *.* OR use the /S parameter on the DEL command to also delete the files in the sub-folders. Note, sometimes this WILL fail. Files might be in use or protected.

DESKTOP.INI should only have the ATTRIBUTES reset to what they were when you started. Just open a COMMAND prompt and change into CHAT and issue ATTRIB. It will show you the attributes for the various files. Probably doesn't need the -H on the last command (or the +H on the first) and DESKTOP.INI was probably hidden already (or the +S and -S as it might have been marked as a SYSTEM file as well).

If sub-folder contents are NOT deleted, you'll need to change into each and issue the DEL *.* command and if none are locked or open they will be deleted. You will STILL be left with the sub-folders though. To delete these, you must be on the (sub-)folder above the one to be deleted and issue RD sub-foldername.

Post a DIR /S of what you want to handle and an ATTRIB as well. Will make it easier to be sure we get it right.

Irv S.

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

Inside the Chat folder there is a subfolder with my emailaddress that's used for the chat.
And inside that subfolder are all kinds of things connected to the conversation - logfiles, history files, shared files. They mostly are in subfolders that vary in name.
To avoid my iconsettings to be deleted I've already added to CD \Chat 'CD mymail@mailserver.com' and then added /S to the DEL command.
Now my main subfolder get's emptied, while desktop.ini in E:\Chat is left alone.

But I end up with a lot of empty subfolders, which I'd also like to have deleted.

So this is what I have now (I have of course changed my real email for mymail@mailserver.com :D) :

@ECHO OFF
E:
CD \CHAT
CD \mymail@mailserver.com
DEL /S /Q *.*
EXIT

Posted 1 year ago
Top
 
ispalten
ispalten
Posts: 6259

RD command will remove folders...

==================
C:\Users\Irv>rd /?
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S
=================

RD and RMDIR are the same, synonym for each other.

So rather than :

============
CD \mymail@mailserver.com
DEL /S /Q *.*
=============

You can just use

===============
RD \mymail@mailserver.com /Q /S
===============

Irv S.

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

Ha Irv,
I just found this link which gave me the same idea :)

This is what I have now and it seems to work :)))

@ECHO OFF
E:
CD \CHAT
RD /S /Q mymail@mailserver.com
EXIT

Does that look alright? (I don't want to be deleting mymail@mailserver.com folders from any other place <LOL>)

Posted 1 year ago
Top
 
ispalten
ispalten
Posts: 6259

That is good. Will only delete that specific folder and everything under it, no where else.

Give it a try on the copy of that folder, but change the drive and the drive (or CHAT folder name if on same drive) and ensure it does what you want.

Irv S.

Accepted Answer · Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

I've done so and it works like a charm!
Thank you :)

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

One more question ...
I tried to pin this to my startmenu, but bat files apparently can't be pinned, nor can the link that points to it.
Is there a way around this?

Posted 1 year ago
Top
 
ispalten
ispalten
Posts: 6259

I think you need an icon for it? Create on for your desktop and RMB on it. Otherwise just put it in your STARTUP folder (the file). That should work I think?

Irv S.

Posted 1 year ago
Top
 
Xhi
Xhi
Posts: 6298

Several good solutions here. pin bat file to start menu windows 7

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

@ Irv - I can put it in the start menu (all programs part), but I can't 'pin' it to the startmenu.
Found a way around it though - I renamed the extension to exe <LOL>.
Still runs (doesn't work on all bat files, but this one is simple enough for it still to work) and now I can pin it :)

@ Xhi - nice try, but uhmmm, I'm on Vista ;)))

Posted 1 year ago
Top
 
Xhi
Xhi
Posts: 6298

Worked on my Vista machine.

Posted 1 year ago
Top
 
SarahJames
SarahJames
Posts: 6581

@ Xhi - did have another look and used the second method here. Silly thing is that the shortcut is bigger than the actual file! <LOL>

Edit: Nope, that doesn't work. A cmd window flashes by but it doesn't do anything.
Btw. all the options mentioned are about pinning to the taskbar.
I don't want to pin to the taskbar, I want to pin it to the startmenu.
Pretty annoying ...

Posted 1 year ago
Top
 



Topic Closed

This topic has been closed to new replies.