Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Windows batch & Powershell: remove folders and exclude folders based on txt file

Hello experts,

I am looking for a procedure in order to remove folders located in a RootFolder
and exclude from the removal folders reported in a txt file.
Values to report in txt file: folder name and not folder path.

To keep in mind:
-Windows or Powershell approach are welcome
-Drill down mode not necessary
-RootFolder should be a variable to declare
-Log file to generate with folders removed

If you have questions, please contact me.
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Thank you, unable to test it right now. I will keep you informed.
I tested and both procedures work!
@oBdA: I was wondering how you can send to next line if you have a long path folder in powershell: 
Example : 
$rootFolder = "C:\Temp\Root\toto\toto\toto”
$rootFolder = "C:\Temp\Root\” _
 “toto\toto\toto”

Open in new window

Thank you in advance for your feedback.
Avatar of oBdA
oBdA

In this case, just use a "+" - and watch out for your quotes! The two closing ones in lines 3 and 4 and the ones on the last line are not the ones PS expects in a script (" - ASCII 34), they're the 'purdy' typographical ones.
$rootFolder = "C:\Temp\Root\" +
	"toto\toto\toto"

Open in new window

The backtick as last character on a line does that in PowerShell, but it won't work the same way as in VB(A/S). You need to build valid expressions, "string part1" "string part2" does not create a single string but two. In your example:
$rootFlder = "C:\Temp\Root\" +
 "toto\toto\toto"

Open in new window

would work. PowerShell sees that there has to be something after the plus operator, and so will continue to look for stuff in the next line. But you can add a backtick to make sure things work as expected:
$rootFlder = "C:\Temp\Root\" + `
 "toto\toto\toto"

Open in new window

Noted. Thank you.