jamester64 Posted February 8 Share Posted February 8 What is the best way to delete the contents of a folder every night ? This is the one in particular, C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/ Share on other sites More sharing options...
Brandon H Supervisor Posted February 8 Supervisor Share Posted February 8 there are several ways. the way I would do it is set a task in task scheduler and either set the delete command right in the task or point the task to a batch file snowy owl and +Matthew S. 2 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881110 Share on other sites More sharing options...
Nick H. Supervisor Posted February 8 Supervisor Share Posted February 8 Off the top of my head, I'd probably create a batch script and put it into the task scheduler to run each night, maybe at 0300 or something. I think the command would be: cd C:\Users\James\AppData\Local\Plex Media Server\Cache\ rmdir /s /q PhotoTranscoder jamester64 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881114 Share on other sites More sharing options...
Brandon H Supervisor Posted February 8 Supervisor Share Posted February 8 On 08/02/2024 at 10:03, Nick H. said: Off the top of my head, I'd probably create a batch script and put it into the task scheduler to run each night, maybe at 0300 or something. I think the command would be: cd C:\Users\James\AppData\Local\Plex Media Server\Cache\ rmdir /s /q PhotoTranscoder technically don't even need the cd command; can just be a one line command. also would need "" around the directory path due to the spaces in the Plex Media Server folder. rmdir /s /q "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" jamester64, Steven P., Nick H. and 2 others 5 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881116 Share on other sites More sharing options...
jamester64 Posted February 8 Author Share Posted February 8 On 08/02/2024 at 08:07, Brandon H said: technically don't even need the cd command; can just be a one line command. also would need "" around the directory path due to the spaces in the Plex Media Server folder. rmdir /s /q "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" Thank you, what would be the command to just delete the contents of the folder instead of the folder itself ? Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881138 Share on other sites More sharing options...
Brandon H Supervisor Posted February 8 Supervisor Share Posted February 8 On 08/02/2024 at 11:09, jamester64 said: Thank you, what would be the command to just delete the contents of the folder instead of the folder itself ? I think a standard del command with a * wildcard should work del /S /Q /F "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881142 Share on other sites More sharing options...
satukoro Posted February 8 Share Posted February 8 On 08/02/2024 at 12:18, Brandon H said: I think a standard del command with a * wildcard should work del /S /Q /F "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" Please note, these "del" and "rmdir" commands will not work with powershell and will only work in the context of cmd.exe If you are going to create a scheduled task for this and wish to use these CMD commands, you will need to create the "Action" as follows: In the "Program/script" portion of adding a new "Start a program" action, you would put cmd.exe with the arguments /C "del /S /Q /F 'C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*"' A functional scheduled task would look like this: jamester64 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881145 Share on other sites More sharing options...
jamester64 Posted February 8 Author Share Posted February 8 On 08/02/2024 at 09:18, Brandon H said: I think a standard del command with a * wildcard should work del /S /Q /F "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" That deleted the contents of the folders inside the directory but left the folders Looking to delete the entire contents of this folder C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881147 Share on other sites More sharing options...
satukoro Posted February 8 Share Posted February 8 (edited) On 08/02/2024 at 12:28, jamester64 said: That deleted the contents of the folders inside the directory but left the folders Looking to delete the entire contents of this folder C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder You can do this with Powershell and the "Remove-Item" command: remove-item c:\utils\test\* -recurse -force If you wanted to bake it into a scheduled task, you can run powershell.exe with the -command argument like follows: powershell.exe -command "remove-item c:\utils\test\* -recurse -force" jamester64, Dick Montage and Brandon H 2 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881151 Share on other sites More sharing options...
Brandon H Supervisor Posted February 8 Supervisor Share Posted February 8 On 08/02/2024 at 11:28, jamester64 said: That deleted the contents of the folders inside the directory but left the folders Looking to delete the entire contents of this folder C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder yeah, bad limitation of the two commands. alternate option would be go back to previous command and just have a 2nd command to recreate the folder after I think rmdir /s /q "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" mkdir "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" On 08/02/2024 at 11:32, satukoro said: You can do this with Powershell and the "Remove-Item" command: remove-item c:\utils\test\* -recurse -force also a good option. I'm still familiarizing myself with the PowerShell version of commands like that so didn't think of it. satukoro 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881152 Share on other sites More sharing options...
satukoro Posted February 8 Share Posted February 8 On 08/02/2024 at 12:33, Brandon H said: yeah, bad limitation of the two commands. alternate option would be go back to previous command and just have a 2nd command to recreate the folder after I think rmdir /s /q "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" mkdir "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" also a good option. I'm still familiarizing myself with the PowerShell version of commands like that so didn't think of it. I nearly posted the same solution of "make the directory after deleting it" but realized I had just done this in a .ps1 script I wrote for something random at my job. jamester64 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881156 Share on other sites More sharing options...
jamester64 Posted February 8 Author Share Posted February 8 On 08/02/2024 at 09:32, satukoro said: You can do this with Powershell and the "Remove-Item" command: remove-item c:\utils\test\* -recurse -force If you wanted to bake it into a scheduled task, you can run powershell.exe with the -command argument like follows: powershell.exe -command "remove-item c:\utils\test\* -recurse -force" Running this in PS doesn't seem to do anything, remove-item C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\* -recurse -force Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881178 Share on other sites More sharing options...
Brandon H Supervisor Posted February 8 Supervisor Share Posted February 8 On 08/02/2024 at 13:02, jamester64 said: Running this in PS doesn't seem to do anything, remove-item C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\* -recurse -force don't forget your quotes around the directory path due to the spaces remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force and this would be run from powershell.exe instead of cmd.exe | or as a .ps1 file instead of .cmd satukoro 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881181 Share on other sites More sharing options...
jamester64 Posted February 8 Author Share Posted February 8 (edited) On 08/02/2024 at 11:08, Brandon H said: don't forget your quotes around the directory path due to the spaces remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force and this would be run from powershell.exe instead of cmd.exe | or as a .ps1 file instead of .cmd Got it :-) remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force Thank you Brandon H, Nick H. and satukoro 3 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881185 Share on other sites More sharing options...
jamester64 Posted February 8 Author Share Posted February 8 On 08/02/2024 at 11:08, Brandon H said: don't forget your quotes around the directory path due to the spaces remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force and this would be run from powershell.exe instead of cmd.exe | or as a .ps1 file instead of .cmd I'm having trouble running this in task schedule this is what I have in scheduler and I see PS quickly open then close but files remain, not sure what I am missing -command remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881206 Share on other sites More sharing options...
Brandon H Supervisor Posted February 8 Supervisor Share Posted February 8 On 08/02/2024 at 15:25, jamester64 said: I'm having trouble running this in task schedule this is what I have in scheduler and I see PS quickly open then close but files remain, not sure what I am missing -command remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force if you're setting the 'program' to powershell.exe then you don't need '-command' at the beginning of your argument satukoro 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881208 Share on other sites More sharing options...
jamester64 Posted February 8 Author Share Posted February 8 (edited) On 08/02/2024 at 13:30, Brandon H said: if you're setting the 'program' to powershell.exe then you don't need '-command' at the beginning of your argument Program is set to Powershell.exe and added just this to argument and still not working in scheduler, remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force not sure what I'm doing wrong Edit- I just found a way to make it work by adding this -ExecutionPolicy Bypass C:\Users\James\PlexClean.ps1 Edited February 8 by jamester64 Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881231 Share on other sites More sharing options...
jamester64 Posted February 9 Author Share Posted February 9 Thank you all for your help satukoro 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881244 Share on other sites More sharing options...
satukoro Posted February 9 Share Posted February 9 On 08/02/2024 at 18:30, jamester64 said: Program is set to Powershell.exe and added just this to argument and still not working in scheduler, remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force not sure what I'm doing wrong Edit- I just found a way to make it work by adding this -ExecutionPolicy Bypass C:\Users\James\PlexClean.ps1 I apologize for the late reply. If you wanted to avoid including -executionpolicy bypass and/or using a .ps1, the argument field should look like the following: -command 'remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force' # or alternatively this: -command "remove-item 'C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*' -recurse -force" The issue with what you had in there before was the missing single quote. By placing the entire command within a set of quotes (either single or double, but not the same ones you use for your folder path), you pass the entire quoted string into powershell. If you want to continue using your powershell script file (.ps1), the argument field could look like this: -executionpolicy bypass -file "c:\path\to\script.ps1" It looks like you figured it out on your own, but I wanted to add this information for clarity. -- On 08/02/2024 at 16:30, Brandon H said: if you're setting the 'program' to powershell.exe then you don't need '-command' at the beginning of your argument In regards to this, I think you only need the -command argument if you are not going to reference a script. I have not tested this however to see if -command is an implied argument for powershell. Another thing to note (according to the following link) is that the -command argument must be the final argument, otherwise the rest of the arguments are run as part of the -command arg. More information can be found here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1 jamester64 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881393 Share on other sites More sharing options...
jamester64 Posted February 9 Author Share Posted February 9 On 09/02/2024 at 08:24, satukoro said: I apologize for the late reply. If you wanted to avoid including -executionpolicy bypass and/or using a .ps1, the argument field should look like the following: -command 'remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force' # or alternatively this: -command "remove-item 'C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*' -recurse -force" The issue with what you had in there before was the missing single quote. By placing the entire command within a set of quotes (either single or double, but not the same ones you use for your folder path), you pass the entire quoted string into powershell. If you want to continue using your powershell script file (.ps1), the argument field could look like this: -executionpolicy bypass -file "c:\path\to\script.ps1" It looks like you figured it out on your own, but I wanted to add this information for clarity. -- In regards to this, I think you only need the -command argument if you are not going to reference a script. I have not tested this however to see if -command is an implied argument for powershell. Another thing to note (according to the following link) is that the -command argument must be the final argument, otherwise the rest of the arguments are run as part of the -command arg. More information can be found here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1 Thank you for the explanation of what i was doing wrong and the fix ;-) satukoro 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881413 Share on other sites More sharing options...
jamester64 Posted February 11 Author Share Posted February 11 Did some testing and this line worked -command "remove-item 'C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*' -recurse -force" This line did not -command 'remove-item "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder\*" -recurse -force' satukoro 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881742 Share on other sites More sharing options...
binaryzero Posted February 11 Share Posted February 11 # Specify directory to clear $Path = "C:\Users\James\AppData\Local\Plex Media Server\Cache\PhotoTranscoder" # Remove items Remove-Item "$Path\*" -Recurse -Force Save as Clear-PlexCache.ps1 Task scheduler: Run: powershell.exe Command arguments: -ExecutionPolicy Bypass -File "Clear-PlexCache.ps1"Folder to start in: <path to script folder> jamester64 1 Share Link to comment https://www.neowin.net/forum/topic/1438180-delete-contents-of-a-folder-every-night/#findComment-598881754 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now