we recently switched from Windows Server 2008 to 2016 and we have some tasks to delete files and folders based on batch every day.
Here is the code:
set successful="D:\TEST\logs\successful.log"
set failed="D:\TEST\logs\failed.log"
set delpath="D:\TEST\1 und 2"
echo Logged time = %time% %date% >> %successful%
echo Logged time = %time% %date% >> %failed%
echo Files deleted: >> %successful%
echo Files failed to delete: >> %failed%
forfiles -p %delpath% -m *.* -c "cmd /c del /q @path && echo @path>>%successful% || echo @path>>%failed%"
forfiles -p %delpath% -c "cmd /c IF @isdir == TRUE rd /S /Q @path && echo @path>>%successful% || echo @path>>%failed%"
echo. >> %successful%
echo. >> %failed%
Since server 2016 this batch does not work. Some issue with spaces in the path.
Output is:
ERROR: Invalid argument/option - 'und'.
Type "FORFILES /?" for usage.
Could there be some changes in syntax of the path? Did I miss quotation marks?
Here's a quick example of what I understand to be the same task, (emptying "D:\TEST\1 und 2"
and logging both failed and successful actions); but using For
loops instead of ForFiles
commands. Please feel free to give it a try and report back as necessary.
@Echo Off
Set "successful=D:\TEST\logs\successful.log"
Set "failed=D:\TEST\logs\failed.log"
Set "delpath=D:\TEST\1 und 2"
(Echo Logged time = %TIME% %DATE%&Echo Files deleted:)>>"%successful%"
(Echo Logged time = %TIME% %DATE%&Echo Files failed to delete:)>>"%failed%"
For /D %%A In ("%delpath%\*")Do RD /S/Q "%%A" 2>Nul&&(>>"%successful%" Echo %%A)||>>"%failed%" Echo %%A
For %%A In ("%delpath%\*")Do Del "%%A" 2>Nul&&(>>"%successful%" Echo %%A)||>>"%failed%" Echo %%A
Echo(>>"%successful%"
Echo(>>"%failed%"