Huge time gap. Oh well. From the future, you can only tell because I admitted so. // This post describes a little, little command line automation I made.
I have various files I occasionally need to delete irretrievably. It is possible to overwrite the newly deallocated space with new files, but not reliably unless one were to fill a significant portion of the total free space. Much better to open the file, paste random data inside, and then delete it. While I could do this, thankfully, there's an app for that. SDelete also knows how to fill the rest of the disk with garbage, so I put aside my 'not invented here' tendency. Plus, this stuff needs to be deleted as soon as possible.
I'd like to pretend I'm talking about the password file I encrypt and then leave unencrypted most of the week. But, I'm not. It is a big collection of files with an involved folder hierarchy. Personally, I'd be fine with zeroing all the data, but Winternals (that created sdelete and similar tools) produces garbage from a crypto process so the edges of the deleted stuff aren't so obvious.
That would be about the end of the story right there. I would set it to chewing this stuff up with sdelete.exe -r C:/user/me/infected/folder. That "-r" is supposed to make it walk the folder structure and delete all the subfolders and their contents. However, it didn't, in my tests. I had to run sdelete.exe C:/user/me/infected/folder/* to delete the files and then the earlier command to delete the folder itself. Running them in the opposite order would provoke a complaint that the folder isn't empty.
I was at an impass between the danger of not deleting the files and the tedium of typing in each folder. Luckily, my background encourages me to make a script in this case. I fantasized about using this as an opportunity to learn powershell, but didn't pursue that more than a few google searches in. I have an ebook illicitly accepted from a file shared source, but didn't broach more than the introduction. Oh well. Today, I decided to look at using python again. I'd looked through the CLI/folder-manipulation chapter of Manning's Quick Python before for the rote to execute shell arguments and not seen them. I chose the silly conclusion that it wasn't possible and pined for powershell. I was flat wrong, obviously.
The os module does all the heavy lifting here. First I'm fed all the folder paths via os.walk( here, False ), in bottom up order. Each time the tuple surrenders the path to that folder (and potentially the names of the folders or files within this one). Then, I just feed that to os.system( for_bash ). It is that simple (standing on this side) to have it send mkdir/cd/etc to the command line.
One aspect I didn't understand was that sdelete wasn't as forgiving as cd. Windows will forgive spaces in a change directory string, unlike fedora (& presumably other _nix). So, in my tests I noticed nothing special when I printed the string but test deletes failed to clear all the folders. I misunderstood the problem as using the "\n" as a newline because I used nn & nnn as demo folder names. Instead, I needed to surround the path string in escaped quotes to succeed.
Not earthshattering, but spending time on this today saved me a lot of inconvenience, and may help someone else. Hopefully if you use sdelete, your copy will recur correctly. If not, then - if you have python - then you can use that file to run it. Of course, if you have python installed, you hardly need either. (Also, while I'm glad github is hosting the gist, I wish I could turn down the contrast. :| )