Delete files and subfolders in the folders "tmp" and "cache" - batch-file

I want to use this script below to clean out "tmp" and "cache" folders in websites like "C:\Storage\Websites\Site1".
It tries to delete files and subfolders in "C:\Storage\Websites\Site1\tmp" and "C:\Storage\Websites\Site1\cache".
Which is right, but it also tries to delete files and subfolders in, for example, "C:\Storage\Websites\Site1\MySpecialLittleProgram\tmp" and, for example, "C:\Storage\Websites\Site1\MySpecialLittleProgram\cache".
Which is wrong. It should only clean up the "tmp" and "cache" folder in the root of the website and not in other subfolders.
If I delete the /s parameter in 'dir /a:d /b /s tmp cache' it will not find anything.
How can I do this part?
(I have deleted the /q parameter in the file deleting part and the folder removing part if anyone copies my script.)
#echo off
call:CleanUp "C:\Storage\Websites"
echo.&pause&goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:CleanUp
IF EXIST %~1 (
cd /d %~1
FOR /f "tokens=*" %%i in ('dir /a:d /b /s tmp cache') DO (
echo %%i
::DELETING FILES I FOLDERS AND SUBFOLDERS
del %%i /s
::DELETING NOW EMPTY FOLDERS AND SUBFOLDERS
FOR /D %%p IN ("%%i\*.*") DO rmdir "%%p" /s
)
)
goto:eof
UPDATE:
I updated my code to be (it is working now):
#echo off
call:CleanUp "C:\Storage\Web"
call:CleanUp "C:\Storage\Web-IIS"
goto:eof
::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------
:CleanUp
IF EXIST %~1 (
cd /d %~1
FOR /f "tokens=*" %%i in ('dir /a:d /b') DO (
IF EXIST %%i\tmp (
del %%i\tmp /s /q
FOR /D %%p IN ("%%i\tmp\*.*") DO rmdir "%%p" /s /q
)
IF EXIST %%i\cache (
del %%i\cache /s /q
FOR /D %%p IN ("%%i\cache\*.*") DO rmdir "%%p" /s /q
)
)
)
goto:eof

This should remove the files in those two locations:
#echo off
del "C:\Storage\Websites\Site1\tmp\*.*" /a /s
del "C:\Storage\Websites\Site1\cache\*.*" /a /s
From your comment, this may be what you need to do: remove the echo keyword after testing it to see the commands on the console that would be executed.
#echo off
cd /d "C:\Storage\Websites"
for /d %%a in (*) do (
for %%b in (tmp cache) do (
pushd "%%~fa\%%b" 2>nul && (echo rd /s /q "%%~fa\%%b" 2>nul & popd)
)
)
pause

I suggest to use rmdir or rd for this task:
rd "C:\Storage\Websites\Site1\tmp" /S /Q
md "C:\Storage\Websites\Site1\tmp"
rd "C:\Storage\Websites\Site1\cache" /S /Q
md "C:\Storage\Websites\Site1"
Command rd with options /S for all subdirectories and /Q for quiet deletes also tmp and cache, but those 2 directories can be easily recreated using command md although I'm quite sure that this would not be really necessary here as the application creating tmp and cache would do it also automatically.
If that is not what you want, please show as directory listings with files and folders in one of the two directories before cleanup and after cleanup.

Related

Batch script to delete all the subfolders inside specific folder in all remote servers and output as a text file

Here I am trying to execute a batch script which deletes subfolders under folder "updates" in all remote machines. List of servers are passed as input
(serverlist.txt)
echo off
for /f %%l in (C:\deleteauto\delrem\serverlist.txt) do
if exist C:\updates goto sub
if not exist C:\updates goto nofile
:sub
del /f /q "C:\updates\*.*"
for /d %%d in ("C:\updates\*.*") do rmdir /s /q "%%d"
echo folder is deleted in %%l >>c:\finaloutput.txt
:nofile
echo No folders in %%l >>c:\final output.txt
Can someone please help in rectifying the errors. Script executed but outputs nothing.
Try this code:
#echo off
for /f "tokens=*" %%l in (C:\deleteauto\delrem\serverlist.txt) do (
if exist "\\%%l\C$\updates" (
pushd "\\%%l\C$\updates"
del /f /q "*."
for /d %%d in ("*.") do rmdir /s /q "%%d"
echo Folder is deleted on server: %%l>>finaloutput.txt
popd
) else (
echo Folder does not exist on server: %%l>>finaloutput.txt
)
)
Here's an example of how I may tackle the task:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SvrLst=C:\deleteauto\delrem\serverlist.txt"
If Not Exist "%SvrLst%" GoTo :EOF
(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%SvrLst%"
) Do PushD "%%~G\C$\updates" 2>NUL && (
RD /S /Q . 2>NUL
Echo Instruction to empty directory applied on %%~G & PopD
) || Echo Directory path not found on %%~G) 1>"C:\finaloutput.txt"
You will note that I have not stated that the directory was emptied. Just because an instruction was applied, does not mean that it was successful at doing so. Unless you check that the \C$\updates directory is completely empty afterwards, you should not imply that it is.
Errors were rectified by #Fire Fox , Thank you! Modified slightly & it worked as expected.
Code:
#echo off
for /f "tokens=*" %%l in (C:\deleteauto\delrem\serverlist.txt) do (
if exist "\\%%l\C$\updates" (
pushd "\\%%l\C$\updates"
del /f /q "\\%%l\C$\updates\*.*"
for /d %%d in ("\\%%l\C$\updates\*.*") do rmdir /s /q "%%d"
echo Folder is deleted on server: %%l>>C:\finaloutput.txt
popd
) else (
echo Folder does not exist on server: %%l>>C:\finaloutput.txt
)
)

delete unwanted directories, error produced is forced on screen

this deletes .unwanted directories from qbittorrent downloads. it works great but the esthetic side effect that I can't shake is that it echoes the file not found error if none are found.
for /f "delims=" %%u in ('dir .unwanted /a:d /b /s') do rmdir /s /q "%%u" 2>nul && if defined v%~n0 echo deleted "%%u"
Better idea is to use /D /R than /F in the For loop for Directories. Check For /? documentation.
for /d /r %%u in (.unwanted) do rmdir /s /q "%%u" 2>nul & echo deleted %%u
This is assuming you are running this from the directory of execution.
It will only echo when the directory is deleted without the ""

Delete all except two folders with .bat file

Batch
#echo off
set folder="c:\FTP\"
set keep="keep1"
set keeptwo="keep2"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (
if /i "%%~ni" NEQ %keep% if /i "%%~ni" NEQ %keeptwo% (rmdir "%%i" /s/q || del "%%i" /s/q)
)
pause
Situation
folder1/file1.txt
folder2/file1.txt
keep1/file1.txt
keep2/file1.txt
file1.txt
Expected result
I need to keep "keep1" and "keep2" folders and all included files, but "folder1" and "folder2" and "file1.txt" with all subdirectories and files must be deleted.
Current result
It removes all files in all folders, removes "folder1" and "folder2", and keeps "keep1" and "keep2"
Any clue what I'm missing.
You cannot use the /S option with the DELETE command as that will delete the file in the current directory and all subdirectories.
Regardless of that, this is how I would accomplish the task so that you don't get the error from the RMDIR command. I use an IF EXIST command to determine if it is a file or directory.
#echo off
set "folder=c:\FTP\"
set "keep=keep1"
set "keeptwo=keep2"
cd /d %folder%
for /F "delims=" %%G in ('dir /b') do (
if /I NOT "%%G"=="%keep%" (
if /I NOT "%%G"=="%keeptwo%" (
REM check if it is a directory or file
IF EXIST "%%G\" (
rmdir "%%G" /s /q
) else (
del "%%G" /q
)
)
)
)
I'm assuming that this is what you wanted to do:
#Echo Off
Set "folder=C:\FTP"
Set "keep=keep1"
Set "keeptwo=keep2"
CD /D "%folder%" 2>Nul || Exit /B
Del /F/A/Q *
For /D %%A In (*) Do If /I Not "%%A"=="%keep%" If /I Not "%%A"=="%keep2%" RD /S/Q "%%A"
Pause

loop through folder, copy and delete

I have a folder That has subfolders as below : I was able to loop and copy the folder and subfolders from test1 to test 3. But I want to extend the functionality by removing the each folder after copying
but am not able to instead is removing all the test 1 to test 3 folder.The test folder is just an example, the folders can be any name.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Copy succesffully copied reason Files were copied without error and you can delete the content folder
rd "%%G\*" /s /q
mkdir "%%G"
)
)
popD
pause
If you resort to XCOPY, why not use the "successor" ROBOCOPY without any loop, deleting and re-creating the folder? Like in
robocopy "C:\BACKUP_FDM_FILES\localbackup\" "C:\FDM\Upload\localbackup" /s /e /COPYALL /dcopy:T /move
This will even keep the timestamp on the moved folder, and all attributes on moved files. Added benefit: you can use all the other helpful options of this command at no extra cost.
"C:\BACKUP_FDM_FILES\localbackup\test1"
"C:\BACKUP_FDM_FILES\localbackup\test2"
"C:\BACKUP_FDM_FILES\localbackup\test3"
pushd "C:\BACKUP_FDM_FILES"
for /f "tokens=*" %%G in ('dir /b /s /a:d ') do (
echo Found %%G
XCOPY %%G "C:\FDM\Upload" /e /s /y
if errorlevel 0 (
echo Successfully copied now you can delete folder
popd
rd "%%G\*" /s /q
mkdir "%%G"
)
pause
You are using pushd to go to backup folder so you need to use popd to go back to parent folder.
SS64 - POPD

Command to delete files in UNC path

Hi I tried below command to delete files in UNC path
set folder="\\SERVERNAME\Publish"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
But I got error saying:
UNC paths are not supported. Defaulting to Windows Directory
Somehow I need to delete files that are residing in Server's shared path using batch command. Any help appreciated.
edited 2015-09-16 - Original answer remains at the bottom
Code reformated to avoid removal of non desired folders if the mapping fails. Only if the pushd suceeds the removal is executed.
set "folder=\\SERVERNAME\Publish"
pushd "%folder%" && (
for /d %%i in (*) do rmdir "%%i" /s /q
popd
)
original answer:
set "folder=\\SERVERNAME\Publish"
pushd "%folder%"
for /d %%i in (*) do rmdir "%%i" /s /q
popd
pushd will create a drive mapping over the unc path and then change to it. Then, all the operations are over drive:\folders. At the end popd will remove the drive assignation.
This deletes all files with name like 'ms' and over a year.
#echo off
set "year=-365"
PushD "\\SERVERNAME\FolderName" && (
"forfiles.exe" /s /m "*_ms_*" /d %year% /c "cmd /c del #file"
) & PopD

Resources