Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Mar 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help creating a batch file

    I would appreciate assistance in creating a batch file I can run manually that would delete all files in a certain directory across all users in our Domain.

    Basically I have discovered that a program is downloading and storing updates on our Server in each user profile inside Terminal Services and it's just taking up unnecessary space on the hard drive. Rather then go into each user's file individually and remove the files, I was wondering if I could instead have a batch file that would remove all the files contained in the following sub-directory:

    E:\User Shares\RedirectedFolder$\%username%\Application Data\Foxit Software\Addon\Install

    Thank you in advance.
    Last edited by tetley; 2016-03-15 at 13:25.

  2. #2
    4 Star Lounger
    Join Date
    Jan 2010
    Location
    Fort McMurray, Alberta, Canada
    Posts
    557
    Thanks
    51
    Thanked 68 Times in 66 Posts
    Certainly this can be done. To do it the way I suspect you are implying is... nontrivial.

    Maybe explore a simpler alternative first. Suppose the updates are named in an organized and highly distinctive way. If you can use wildcards to target the named files accurately, then you can try something like this:

    cd E:\User Shares\RedirectedFolder$
    erase /S /Q Foxit-Reader-Update-*.exe
    The danger here is mainly, what if the file identification isn't as good as you need? This code will examine every subfolder of the current folder looking for those files. If there are matches existing you don't anticipate, this code could go rogue on you and cause a lot of damage.

    Perhaps then you don't need or want fully automatic code. You can review each individual deletion with:

    cd E:\User Shares\RedirectedFolder$
    erase /S /P Foxit-Reader-Update-*.exe

  3. #3
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,414
    Thanks
    33
    Thanked 195 Times in 175 Posts
    It's fairly straightforward:
    * generate a list of user names using something like NET USERS /DOMAIN > ListOfUsers.txt, and possibly remove those you don't want to consider. Reformat the list into a single "column" of usernames in the TXT file.
    * set up a FOR loop to process each username's files, perhaps
    FOR /F %%a IN (ListOfUsers.txt) DO ECHO DEL "E:\User Shares\RedirectedFolder$\%%a\Application Data\Foxit Software\Addon\Install\*.*"

    The ECHO is there for testing; remove when you're happy.

    I wouldn't advise using *.* but something more restrictive, based on the pattern of the file names.

    That's a start, I have to go to work now!

    Later...
    Here's the first part done for you:
    Code:
    @echo off
    title Get Users from NET USERS
    setlocal
    set list=ListOfUsers.txt
    if exist %list% del %list%
    :: note that there are TWO spaces between the FIND double-quotes!
    ::   this test removes all non-username lines
    :: assumes that usernames do NOT contain spaces
    for /f "tokens=1-3" %%a in ('net users /domain^|find "  " ') do (
      if "%%a" neq "" echo %%a>>%list%
      if "%%b" neq "" echo %%b>>%list%
      if "%%c" neq "" echo %%c>>%list%
    )
    start "" notepad %list%
    endlocal
    You'll have to test it works on a domain; I don't have one now, so ran it on my machine without specifying "/domain".
    Last edited by BATcher; 2016-03-16 at 03:56.
    BATcher

    Time prevents everything happening all at once...

  4. #4
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,178
    Thanks
    47
    Thanked 983 Times in 913 Posts
    Here's my one line version.
    To use it in a batch file you need to add an extra % per BATcher's example above.

    Note: I've assumed the $ symbol is for the share name, not the directory, so I've removed it.
    Code:
    for /f %x in ('dir /ad /b "E:\User Shares\RedirectedFolder"') do if exist "E:\User Shares\RedirectedFolder\%%x\Application Data\Foxit Software\Addon\Install\*.*" echo del /q "E:\User Shares\RedirectedFolder\%x\Application Data\Foxit Software\Addon\Install\*.*"
    cheers, Paul

  5. #5
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,414
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Neat - but I eschewed the/your DIR method because it didn't give the OP a chance to remove from the list those username(s) whose downloaded files he might wish to keep (like, perhaps, for Administrator?)!
    BATcher

    Time prevents everything happening all at once...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •