none
File deletion in root and subdirectories. RRS feed

  • Question

  • Greetings,

    I have a question regarding Batch scripting. I am trying to create a system maintenancing script which removes .TMP and .BAK files from all of my drives both subdirectories and directories above. Let's say the Batch script would be placed into "Folder3" in the following internal drive address "C:\Folder1\Folder2\Folder3\Folder4\Folder5". And I want the batch script to remove files from folders 4 and 5 as subdirectories but also to remove files from C: Drive and folders 1, 2, and of course 3 itself but without removing folders. And I'd like to apply this method to all drives simultaneously without a human user personally moving the script via cut/copy and pasting from drive to drive. The other alternative which can come into consideration is to create a Batch File with any address location on the internal drive and when running this Batch file it'll create additional Batch Files on specified (multiple) drives at the very root of drives in the drive address of C:\ for instance, and initializes the required file deletion process. Some of the folders require my Administrator priviliges therefore I'd like to create a code within the script that ask for user permission and says "Yes" but without actually asking from me as the user to click "Ok" or "Yes" when running the script itself or to fully bypass the firewall. I have created the other part of the coding for deleting specified superfluous file types and in what way it's just the sub and above-directory part of it that gives me a pause to proceed. Any help is greatly welcome or pointers. If my request would seem cluttered please notify me of it and I'll try to simplify it down, although I hope I wasn't too confusing with the elaboration.

    Thank you in advance, even for the consideration.

    Kind Regards.

    Saturday, July 19, 2014 2:07 AM

Answers

  • Yes, but I'd also like the batch file to delete the designated files from the entire drive even if the batch file is placed somewhere in a subdirectory.

    The location of the batch file is entirely irrelevant. Reporting the result or repeating the process in case of failure is possible but rather meaningless because some .tmp files will always be locked. Try this:

    @ECHO OFF
    for /F %%a in ('mountvol.exe ^| find ":\"') do call :Sub %%a
    goto :eof

    :Sub
    echo Processing %1
    del /s /q %1*.tmp
    del /s /q %1*.bak


    Sunday, July 20, 2014 9:42 PM

All replies

  • Yes.

    Just put "Echo YES" in your file and it will say yes without actually asking for anything.

    I think you are in the wrong forum.  This is not Comedy Central.


    ¯\_(ツ)_/¯

    Saturday, July 19, 2014 2:49 AM
  • I have a question regarding Batch scripting.

    Would this be a summary of your question:

    How to delete all .tmp and .bak files from all drives without getting any prompts, using a batch file?


    Saturday, July 19, 2014 5:30 AM
  • Yes, but I'd also like the batch file to delete the designated files from the entire drive even if the batch file is placed somewhere in a subdirectory. Essentially this is the function I am trying to create;

    1, Ask for user Administrator User permission and say yes (without actually prompting the human user)

    2, Delete the designated files from specified drives given by their letters (multiple drives)

    3, Repeat process if unsuccessful.

    I was able to put together the rest of the code instructions and verified the success. But getting the Batch file to delete files from the entire drive even if it's in a subdirectory has proven difficult. I know for subdirectories I need to inser the "s/".

    Sunday, July 20, 2014 9:03 PM
  • Yes, but I'd also like the batch file to delete the designated files from the entire drive even if the batch file is placed somewhere in a subdirectory.

    The location of the batch file is entirely irrelevant. Reporting the result or repeating the process in case of failure is possible but rather meaningless because some .tmp files will always be locked. Try this:

    @ECHO OFF
    for /F %%a in ('mountvol.exe ^| find ":\"') do call :Sub %%a
    goto :eof

    :Sub
    echo Processing %1
    del /s /q %1*.tmp
    del /s /q %1*.bak


    Sunday, July 20, 2014 9:42 PM
  • Thank you, I'd like to ask one for more thing. The code you provided will allow the code to seek the wanted files for deletion on all drives or will I need to insert in the code somewhere? Also could you give me some pointers on good Batch scripting tutorials please? I'd like delve into this further.
    Sunday, July 20, 2014 10:46 PM
  • That's an elegant batch file

    It will only include drives that are local without hard-coding what the drives are, it does not include network mapped drives.

    When I write a batch file, I follow this general structure

    @echo off
    if "%1"=="command1" goto command1
    if "%1"=="command2" goto command2

    :default
    call %0 command1
    call %0 command2
    goto end

    :command1
    echo command1
    goto end

    :command2
    echo command2
    goto end

    :end

    command line reference
    http://technet.microsoft.com/en-us/library/bb490890.aspx


    Sunday, July 20, 2014 11:04 PM
  • I am sorry but I was unable to follow your code.
    Sunday, July 20, 2014 11:11 PM
  • This is like a main program calling subroutines except it call itself with parameters

    :default
    call %0 command1
    call %0 command2
    goto end

    these lines direct which subroutine you are calling
    if "%1"=="command1" goto command1
    if "%1"=="command2" goto command2 

    this is an example of a subroutine, additional parameters would be %2 %3 etc
    :command1
    echo command1
    goto end

    Sunday, July 20, 2014 11:18 PM
  • I see now and thank you, but regarding the previous comment from another helper could you please tell me on how can I make changes to it to specify the drive letters (multiple) to where it should perform the system maintenance please?
    Monday, July 21, 2014 1:22 AM
  • This is the unelegant version

    for %%a in (C: D: E: F: G:) do for %%b in (bak tmp) do del /s /q %%a\*.%%b

    or alternately

    set drives=C: D: E: F: G:

    for %%a in (%drives%) do for %%b in (bak tmp) do del /s /q %%a\*.%%b


    Monday, July 21, 2014 1:36 AM
  • Thank you, I'd like to ask one for more thing. The code you provided will allow the code to seek the wanted files for deletion on all drives or will I need to insert in the code somewhere? Also could you give me some pointers on good Batch scripting tutorials please? I'd like delve into this further.
    You can invoke the batch file as it is. It will delete .bak and .tmp files on all local drives without further modification.
    Monday, July 21, 2014 6:17 AM
  • Is there a way to incorporate your line of code of

    for %%a in (C: D: E: F: G:) do for %%b in (bak tmp) do del /s /q %%a\*.%%b

    and adding the wanted files to deleted in the following format of

    del /s /q %1*.tmp
    del /s /q %1*.bak

    The main Reason why I am asking is because I already have the Batch File coded this way with vertical file enlisting instead of horizontal as you suggested in yours. Mainly I am asking is to save time to my current one. This code I am suggesting in the first line is that one can be found at the upper area of this page recommended by another user but his method did not enlist the drive letters as opposed to yours. I am trying to incorporate the User, Frederik Long's line of code of file enlisting and yours with the drive letter specifications into a single Batch Script. If there's no way to have both then I'll stick to the one you recommended.

    Thank you in advance.

    Thursday, July 24, 2014 2:01 AM
  • @ECHO OFF
    for %%a in (C: D: E: F: G:) do call :Sub %%a
    goto :eof

    :Sub
    echo Processing %1
    del /s /q %1\*.tmp
    del /s /q %1\*.bak


    Thursday, July 24, 2014 2:25 AM
  • Thank you very much for your help I am extremely grateful. I will try to do a test run with the code you suggested and leave feedback.
    Thursday, July 24, 2014 2:44 AM