Delete AUC and KTI files for all users

pdesjardins1
pdesjardins1 used Ask the Experts™
on
We run Microsoft Dynamics AX 2012. We have Remote Desktop Servers which users access, to access AX.

I'd like a PS script or bat file that I can run once a week, at midnight, to delete *.AUC and *.KTI files from all the users C:\users\%users%\appdata\local   folder on these RDS machines.

I've tried
for /D %D in ("C:\USERS\*") do del "%~fD\AppData\Local\*.auc"
for /D %D in ("C:\USERS\*") do del "%~fD\AppData\Local\*.kti"
pause

Which Errors out:
The following usage of the path operator in batch-parameter substitution is invalid: "%~fD\AppData\Local\*.auc"

Also tried:
del "%userprofile%\AppData\Local\*.auc"
del "%userprofile%\AppData\Local\*.kti"

But that only delete the files for the current user.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Inside a batch script, you need to use double percent signs for the loop variable
for /D %%D in ("C:\USERS\*") do del "%%~fD\AppData\Local\*.auc"
for /D %%D in ("C:\USERS\*") do del "%%~fD\AppData\Local\*.kti"
pause

Open in new window

PowerShell in test mode; will only show the files it would delete; remove the -WhatIf at the end of line 3 to run it for real:
Get-ChildItem -Path C:\Users -Directory | ForEach-Object {
	ForEach ($ext in 'auc', 'kti') {
		Get-ChildItem -Path  "$($_.FullName)\AppData\Local" -File -Filter *.$ext -ErrorAction SilentlyContinue | Remove-Item -WhatIf
	}
}

Open in new window

Author

Commented:
Your batch script work.
Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial