Garacesh Posted May 18, 2016 Posted May 18, 2016 Since DELETE/DEL doesn't support deleting folders, and RMDIR/RD doesn't support wildcards, I find myself in a bit of a pickle. I need a way to delete all folders except x/y/z (which to throw a spanner in the works are named the same across all systems, but not guaranteed to exist). My first idea was to use a combination of IF EXIST and ATTRIB +H to make the folders I want to keep hidden, but remove everything else, expecting it to not touch hidden files. Then I found out RD doesn't support wildcards. So I tried searching the Googz, but I cannot for the life of me get my head around all this for /D %%i stuff and nobody is really explaining how it works, so I can't take a currently existing script and modify it to suit my needs.
Steve21 Posted May 18, 2016 Posted May 18, 2016 for /d %%I in (c:\123\*) do ( if /i not "%%~nxI" equ "b" rmdir /q /s "%%~I" ) Should work. "b" being the name of the exclusion Steve
Garacesh Posted May 18, 2016 Author Posted May 18, 2016 for /d %%I in (c:\123\*) do ( if /i not "%%~nxI" equ "b" rmdir /q /s "%%~I" ) Should work. "b" being the name of the exclusion Steve If I had to hazard a guess, I'd suggest that /d is 'Directory' and that /i, %%I and %%~I are 'Item'? No idea what %%~nxI is, but given that it precedes 'equ "b"' I'm guessing that it's a regex against the item name? Issue is, there are potentially 3 files that need to be kept. Does this allow for multiple values of "b"?
Steve21 Posted May 18, 2016 Posted May 18, 2016 There's probably a smarter way to do it, but this should let you do that: for /d %%I in (c:\123\*) do ( if /i not "%%~nxI" equ "b" ( if /i not "%%~nxI" equ "c" ( if /i not "%%~nxI" equ "d" ( rmdir /q /s "%%~I" ) ) ) ) Basically line by line: for all folders (assign names to %%i variable) in "folder" do if (non-case sensitive) filename (expanded to filename + extension) equals "b" repeat c/d remove folder quietly including subolders for variable passed in line 1 If that makes sense? Steve
Garacesh Posted May 18, 2016 Author Posted May 18, 2016 Bummer. That works, but doesn't fix my problem. Back to the drawing board I go. Thank you anyway I'll probably still keep that in use.
Steve21 Posted May 18, 2016 Posted May 18, 2016 Haha what is the problem? Might be epic fix for it! Steve
Garacesh Posted May 18, 2016 Author Posted May 18, 2016 (edited) Mandatory profiles. If $(Member of staff) logs on and gets the mandatory profile, all is well. The Start Menu and Start Layout (tiles xml) are copied to the local machine (ROBOCOPY /MIR on startup) If I make an amendment to the tiles XML, or the Start Menu structure, what should happen is when the machine reboots, the old xml/structure gets overwritten with the new one, staff logs in, tiles change. As you can imagine by the fact that I'm having a problem with this, this isn't what actually happens in practise. Instead, the start menu breaks. Just another Windows 10 bug feature. Since no registry entry is made in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList when using a mandatory profile, the idea was to delete all user folders except C:\Users\Default, Public and the Local Admin on startup. This means the staff member has no folder in C:\Users after a reboot and the machine will generate a new one using the new tiles XML, and all is well. This bit appears to be working, so far as my testing so far shows. However, if I log in as my account, which, due to its administrative nature, does not have a mandatory profile (and as such does generate an entry in HKLM\...\ProfileList), logout, and then log in as $(Member of staff), boom, start menu doesn't work again. If I log in as the local administrator and delete my corresponding HLKM\...\ProfileList entry, log out, log in as $(Member of staff), it works again but only after a reboot. So it looks like I need to find a way to delete all registry entries in ProfileList except S-1-5-18 (Local System), S-1-5-19 (NT Authority), S-1-5-20 (also NT Authority) and S-1-5-21-domain-500* (Local Administrator) But by now we've traversed through the Plains of Fixing, past the Lake of It'lldo, and we're deep into the Mountains of Kludge, and I ain't really wanting to set up camp. * I have yet to see if the ID is identical across computers. Edit: Nope. Even after a reboot, the start menu still isn't working.. Grr. There must be something I'm missing.. Edit: Hmm. I'm also noticing that it isn't deleting the test staff's C:\Users folder every reboot, either.. It's taking multiple reboots to delete it, which may be part of the problem.. Edited May 18, 2016 by Garacesh
Garacesh Posted May 25, 2016 Author Posted May 25, 2016 Update: I remembered that Windows 10 can have Powershell scripts as startup/shutdown scripts. That makes things significantly easier. $DoNotDelete = ("Administrator", "Public", "Default") ForEach ($UserFolder in Get-ChildItem "C:\Users") { if ($DoNotDelete -notcontains $UserFolder.Name) { Remove-Item $UserFolder } }
phildyer Posted June 29, 2016 Posted June 29, 2016 This post has been really useful, I was wonder if there is a way you could do the opposite? Batch delete just one folder in multiple folders. When our users log onto the iMacs OS X creates a Library folder in their root of their home directory %homeshare%\Library and I've tried doing things like for /d /r %a in (library) do echo rd /s /q "%a" at the root level of the users folder to try and delete the Library folder in the root of everyone's account but its not working. Any suggestions would be welcome.
Garacesh Posted June 29, 2016 Author Posted June 29, 2016 I could probably do that with Powershell but I wouldn't have a clue with batch. (I ended up using DelProf2 to sort my deleting c:\Users folders)
phildyer Posted June 29, 2016 Posted June 29, 2016 I've got del prof to get rid of the local profiles but unfortunately the library folder gets added to their network home folder which is really infuriating. I'm not particularly good at powershell so I haven't really had a look at it because I'm convinced i'll break something
Arthur Posted June 29, 2016 Posted June 29, 2016 I've got del prof to get rid of the local profiles but unfortunately the library folder gets added to their network home folder which is really infuriating. If I have understood it correctly the following script should do what you need. @echo off pushd "H:\Users\Students\Intake 16" for /D %%I in (*) do rmdir /s /q "H:\Users\Students\Intake 16\%%I\FolderToDelete" popd
dapaulio Posted June 29, 2016 Posted June 29, 2016 (edited) This post has been really useful, I was wonder if there is a way you could do the opposite? Batch delete just one folder in multiple folders. When our users log onto the iMacs OS X creates a Library folder in their root of their home directory %homeshare%\Library and I've tried doing things like for /d /r %a in (library) do echo rd /s /q "%a" at the root level of the users folder to try and delete the Library folder in the root of everyone's account but its not working. Any suggestions would be welcome. Can you not run a logoff script which essentially deletes the folder Not sure what the mac equivalent to rd ... blah n:\library Edited June 29, 2016 by dapaulio
phildyer Posted June 30, 2016 Posted June 30, 2016 I came to a similar conclusion yesterday and wrote this: @echo off setlocal set "root=." pushd "%root%" for /d %%F in (*) do ( robocopy /mir D:\data\test "%%F\library") I just ran an elevated command prompt from the file server and set the directory to the folder containing the users. The Library folder contains some files which are too long to be deleted so I've used robocopy to copy an empty folder over them so it blanks them. I then have second script set to delete the folder: @echo off setlocal set "root=." pushd "%root%" for /d %%F in (*) do ( rd /s /q "%%F\library") 1
Garacesh Posted June 30, 2016 Author Posted June 30, 2016 The Library folder contains some files which are too long to be deleted so I've used robocopy to copy an empty folder over them so it blanks them. Huh.. Didn't know you could do that. That's actually ridiculously handy to know. Thanks!
bossman Posted June 30, 2016 Posted June 30, 2016 (edited) @Garacesh I have tested this simple batch file to delete a test folder with contents including other folders with files (.docx, .ppt, .txt etc etc) and it works just fine for me, your parameters within the quotation marks will change obviously, quotation marks are required: @echo off RD /S /Q "C:\Test" @Garacesh my bad didn't read your first post properly, having a senior moment, you don't want to just delete all the files and folders sorry Edited June 30, 2016 by bossman
Garacesh Posted June 30, 2016 Author Posted June 30, 2016 @Garacesh my bad didn't read your first post properly, having a senior moment, you don't want to just delete all the files and folders sorry I actually did, since the HLKM\...\ProfileList entries do get deleted properly by W10, so it's just the folders that get left behind, but I settled on using DELPROF anyway. 1
dapaulio Posted June 30, 2016 Posted June 30, 2016 Huh.. Didn't know you could do that. That's actually ridiculously handy to know. Thanks! yeah robocopy mirror switch (/mir) essentially clones the source with the destination. if there are extra files in the destinations these are purged. like a forced delete 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now