How to Clear Download content using a .Bat or Script file.

wcgplc
wcgplc used Ask the Experts™
on
Hello Experts

At the moment we are running a batch file on every computer to empty the downloads folder every week, but it deletes only the files but not the folders.
could you be able to help me by creating a new batch file that clears all the contents of the downloads folder for all the user's profiles on the PCs?
we would like to know as well if we can add this on the group policy and it can deploy to all the users in the company.

Here is the one we are using which we are running from a scheduled task list.

forfiles /p "C:\Users\PofileName\Downloads" /s /m *.* /c "cmd /c Del @path" /D -0

Thank you very much.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Test your restores, not your backups...
Top Expert 2016
Commented:
Here's an approach for a BAT file that will empty the downloads folder of all user profiles, including the sub-folders.  Naturally it would need to be executed with permissions that allow this against all profiles.  I can't help with the GPO deployment...

for /d %%P in ("c:\users\*") do (
    if exist "%%~P\Downloads" (
        for /d %%D in ("%%~P\Downloads\*.*") do rd /s /q "%%~D"
        del /q "%%~P\Downloads\*.*"
    )
)

Open in new window


»bp

Author

Commented:
Hello Bill,

sorry for the question I am about to ask.

are those signs after /d needs to be added as well? or is it something I need to change depending on our systems.
thank you for the quick answer.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
The %% followed by a letter should not be changed.  They are special "loop variables" in BAT commands that get assigned a value by the FOR loop statement when it executes.

So for example the outer FOR loop is:

for /d %%P in ("c:\users\*") do (
    . . .
)


It is a FOR /D loop, so that indicates to only enumerate folders in the specified path, in this case "c:\users\*.*".  As the FOR /D executes, it will assign the full path of the first file to %%P and then process the code inside the loop (enclosed in parens).  Then it will will look for the next folder, assign it's path to %%P and process the code inside the loop.  The code in the loop can refer to the "loop variable" %%P to get the current file path and act on it.  Since loops can be nested, "loop variables" can be named upper or lower case single letters, and are case sensitive.  So notice I used %%D in a nested loop inside the output loop as a different variable, getting the paths of subfolders of the Downloads folder.

One other thing to mention, the syntax is slightly different when in a BAT file, versus at the command prompt.  When doing FOR statements at a command prompt, the loop variables only have a single % in front of them.  So this in a BAT file:

for /d %%P in ("c:\users\*") do (

becomes this at a command prompt:

for /d %P in ("c:\users\*") do (

You can get more info on the FOR command by doing FOR /? at a command prompt, which will show the following:

Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

  %variable  Specifies a single letter replaceable parameter.
  (set)      Specifies a set of one or more files.  Wildcards may be used.
  command    Specifies the command to carry out for each file.
  command-parameters
             Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

FOR /D %variable IN (set) DO command [command-parameters]

    If set contains wildcards, then specifies to match against directory
    names instead of file names.

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

    Walks the directory tree rooted at [drive:]path, executing the FOR
    statement in each directory of the tree.  If no directory
    specification is specified after /R then the current directory is
    assumed.  If set is just a single period (.) character then it
    will just enumerate the directory tree.

FOR /L %variable IN (start,step,end) DO command [command-parameters]

    The set is a sequence of numbers from start to end, by step amount.
    So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
    generate the sequence (5 4 3 2 1)

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    or, if usebackq option present:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

    file-set is one or more file names.  Each file is opened, read
    and processed before going on to the next file in file-set.
    Processing consists of reading in the file, breaking it up into
    individual lines of text and then parsing each line into zero or
    more tokens.  The body of the for loop is then called with the
    variable value(s) set to the found token string(s).  By default, /F
    passes the first blank separated token from each line of each file.
    Blank lines are skipped.  You can override the default parsing
    behavior by specifying the optional "options" parameter.  This
    is a quoted string which contains one or more keywords to specify
    different parsing options.  The keywords are:

        eol=c           - specifies an end of line comment character
                          (just one)
        skip=n          - specifies the number of lines to skip at the
                          beginning of the file.
        delims=xxx      - specifies a delimiter set.  This replaces the
                          default delimiter set of space and tab.
        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
        usebackq        - specifies that the new semantics are in force,
                          where a back quoted string is executed as a
                          command and a single quoted string is a
                          literal string command and allows the use of
                          double quotes to quote file names in
                          file-set.

    Some examples might help:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

    would parse each line in myfile.txt, ignoring lines that begin with
    a semicolon, passing the 2nd and 3rd token from each line to the for
    body, with tokens delimited by commas and/or spaces.  Notice the for
    body statements reference %i to get the 2nd token, %j to get the
    3rd token, and %k to get all remaining tokens after the 3rd.  For
    file names that contain spaces, you need to quote the filenames with
    double quotes.  In order to use double quotes in this manner, you also
    need to use the usebackq option, otherwise the double quotes will be
    interpreted as defining a literal string to parse.

    %i is explicitly declared in the for statement and the %j and %k
    are implicitly declared via the tokens= option.  You can specify up
    to 26 tokens via the tokens= line, provided it does not cause an
    attempt to declare a variable higher than the letter 'z' or 'Z'.
    Remember, FOR variables are single-letter, case sensitive, global,
    and you can't have more than 52 total active at any one time.

    You can also use the FOR /F parsing logic on an immediate string, by
    making the file-set between the parenthesis a quoted string,
    using single quote characters.  It will be treated as a single line
    of input from a file and parsed.

    Finally, you can use the FOR /F command to parse the output of a
    command.  You do this by making the file-set between the
    parenthesis a back quoted string.  It will be treated as a command
    line, which is passed to a child CMD.EXE and the output is captured
    into memory and parsed as if it was a file.  So the following
    example:

      FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

    would enumerate the environment variable names in the current
    environment.

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

Open in new window


»bp
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
Hello Bill, thank you very much for your reply and a good explanation.
I am going to try this and if this works I will get back to you again.
good day.

Author

Commented:
Hello Bill,
Thank you so much for your help. it works perfectly well.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Great, glad that was helpful.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial