0

I have recently installed a new NAS and am trying to create a 'Temp' folder as I had before, basically a folder that is used to move files around during work hours and then deleted every night.

My knowledge of batch files is very limited. I found this on another website but have not managed to get it working. Hopefully someone may be able to explain how I can improve this or why it is not working.

@echo off
rd "C:test" /F /Q

Just to make things slightly more difficult, I have a subfolder named 'This Share is Deleted Daily - Do Not Store Important Files'.

I was wondering if it is at all possible to delete all files and sub folders of this folder but keep the above named sub folder?

Any help would be really appreciated.

James

  • Please confirm that the folder you want to delete is test and it's in the root folder of the C drive. Also, what version of Windows are you using? – boot13 Dec 1 '15 at 12:45
  • test location is C:\test . Maybe I was not very clear sorry. Basically 2 folders. Test which s imitating the temp folder this batch file to relate to after testing. Inside Test we have the 'This Share is Deleted Daily - Do Not Store Important Files' Folder. There will also be other files/folders inside the top level test folder. What I want to do, is every day at midnight, remove all files and folders from test. Leaving only (If possible) the Do Not Store Folder. – JGreen Dec 1 '15 at 13:04
1

For your first question, I think you want /S /Q not /F /Q, and the ECHO OFF line is not needed you can just precede the command with @ which prevent the command itself from being echoed:

@RD "C:test" /S /Q

This will delete the test folder as well as all its contents. It acts on the test folder at the current C: location (as per your example).

If you want to keep the 'test\This Share is Deleted Daily - Do Not Store Important Files' folder then this should work, however you have to supply the full path twice - I have used %USERPROFILE% here):

FOR /F "usebackq delims==" %i IN (`dir /b /s "%USERPROFILE%\test"`) DO @IF "%~i" NEQ "%USERPROFILE%\test\This Share is Deleted Daily - Do Not Store Important Files" @DEL /S /Q "%i" 2>NUL

Of course you need to be very careful with commands like this, there is great potential for accidentally wiping something useful, or important, or critical.

If you want to try anything more complicated I suggest you think about installing cygwin and then writing a bash script to achieve your objective. bash is so much better than batch!

  • Thank you for the quick answer. Just to make sure I understand correctly, the first line of - @RD "C:test" /S /Q - will delete the test file and everything below it correct? The second answer you gave will keep both the test and This share is deleted daily folder but removes all other folders and files below test? Thank you again for the quick response – JGreen Dec 1 '15 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.