Delete file and folder under path with wildcard

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cronoping
Posts: 3
Joined: 23 Jul 2023 07:26

Delete file and folder under path with wildcard

#1 Post by cronoping » 23 Jul 2023 07:37

Hi all, I have this issue and don't found any solution... (I worked on it all the last week without lucky)
I have a structure like this:
C:\Users\UT45\AppData\Logs\
C:\Users\CR45\AppData\Logs\
C:\Users\UT45\AppData\Logs\
I need to delete all the file and folder under Logs folder, but only for the user which username starts with: UT*
This command works for all the file, but not for the folder:

Code: Select all

for /D %%I in ("C:\Users\UT*") do del /A /F /Q  "%%I\AppData\Logs\*" 2>nul
I hope I can find help here...
Thank you to all, have a nice day!

cronoping
Posts: 3
Joined: 23 Jul 2023 07:26

Re: Delete file and folder under path with wildcard

#2 Post by cronoping » 23 Jul 2023 13:53

I found it on powershell command:

Code: Select all

powershell -Command "Remove-Item -Path C:\\Users\UT*\AppData\cancellare_contenuto\* -Recurse"
and works fine from the shell.
Do you know how takes no effect through powershell script?
Thank you!

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Delete file and folder under path with wildcard

#3 Post by Batcher » 24 Jul 2023 01:52

test-1.bat

Code: Select all

@echo off
for /d %%i in ("C:\Users\UT*") do (
    del /a /f /q  "%%i\AppData\Logs\*"
    for /d %%j in ("%%i\AppData\Logs\*") do (
        rd /s /q "%%j"
    )
)

cronoping
Posts: 3
Joined: 23 Jul 2023 07:26

Re: Delete file and folder under path with wildcard

#4 Post by cronoping » 24 Jul 2023 02:53

Hi Batcher,
thank you very much! Works perfectly!
Have a nice day, regards

Post Reply