powershell versus windows shell

I am getting close to knowing enough powershell to make a clean up script. Years ago I had a clean up script to delete every folder and file in every part of a file system. It used a gather process to get all the files and folders, put into a txt file, then using win shell FOR command to go through that list and delete each one. It came in handy. CLeaning up user profiles was fun, It then ran the FOR command again on the list of files and folders, deleting every one. I’m working on a powershell version, when I get it done I will post it here.

Lets go back into the time of window shell and see what it could do. So if you type CMD, then “for /?” you would get a list like the one below. The below code is what I used as a basis for my clean up script.

FOR /F “eol=; tokens=2,3* delims=, ” %i in (myfile.txt) do @del “%i”

The above would get specific strings in the file.txt, then run the delete command on them. the actual help for that is below:

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 a

Copyright 2024 Rod Deluhery

Leave a comment