I'm looking for script which will remove all folders containing del word in the end of the name but exclude containing prep word.
for /f "delims=" %%i in ('dir /a:d /s /b ?*del') do rd /s /q %%i
This should do the trick :
#echo off
for /f "delims=" %%i in ('dir /a:d /s /b ?*del') do echo %%i | find /i "prep" >nul || rd /s /q "%%i"
Related
I'm trying to search for a folder (c:\test) in a certain directory that has the word "current" in it. And then i would like to copy from a folder inside it (c:\test\current\first).
Any help would be much appreciated. I have done my research but so far I have only managed to do xcopy, but not the first 2. Sorry I'm relatively new to this.
I am not sure of the structure, but seems you want to do:
from Batch file:
For /f "delims=" %%i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do echo %%i
From Cmdline (Console):
For /f "delims=" %i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do echo %i
So as per your last comment, this is more or less what you would need to perform the xcopy:
For /f "delims=" %%i in ('dir /S /B /AD "N:\8\Installation Release" ^| findstr /i "web" ^| findstr /i "current"') do (
xcopy "%%i"* /D /C /Q /R /Y /I /S "D:\Abc" & goto :eof
)
I found this on S.O.:
for /f "eol=: delims=" %%F in ('dir /b /ad^|findstr /vxilg:"keep.txt"') do rd /q /s "%%F"
Where do I specify the folder path of the folders I want to delete?
Use pushd to temporarily change to the folder to process and popd to restore the original one:
pushd "D:\your\folder\path" && ((for /f "eol=: delims=" %%F in ('dir /b /ad^|findstr /vxilg:"keep.txt"') do rd /q /s "%%F") & popd)
The && operator avoids the following command(s) to be executed when your folder path cannot be found.
set "folder=C:\My Folder"
for /f "eol=: delims=" %%F in ('dir /b /ad "%folder%" ^|findstr /vxilg:"keep.txt"') do rd /q /s "%folder%\%%F"
I have created a batch file which should keep the 4 newest folders on a network. This is because it is a back up of one of the computers.
I tested it first on my own system and it worked correctly. After testing I copied the batch to the other system and let it run by the Task Scheduler of Windows. It does make back ups, but it will not remove the oldest folders. I gave that task all needed permissions. I will paste the code here, because I think there could be a fault in the code.
#echo off
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _yyyy=%%L
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
Set _isodate=%_yyyy%-%_mm%-%_dd%
Echo %_isodate%
#echo off
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup1\backup1_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup1\%%I"
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup2\backup2_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup2\%%I"
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup3\backup3_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup3\%%I"
for /F "skip=4 delims=" %%I in ('dir "\\server\Folder1\folder2\backup4\backup4_*" /AD /B /O-N 2^>nul') do rd /Q /S "\\server\Folder1\folder2\backup4\%%I"
#echo off
cd C:\
ROBOCOPY "C:\backup1" "\\server\Folder1\folder1\backup1\backup1"_%_isodate% /COPYALL /E /W:5 /R:5
ROBOCOPY "C:\backup2" "\\server\Folder1\folder2\backup2\backup2"_%_isodate% /COPYALL /E /W:5 /R:5
ROBOCOPY "C:\backup3" "\\server\Folder1\folder2\backup3\backup3"_%_isodate% /COPYALL /E /W:5 /R:5
ROBOCOPY "C:\backup4" "\\server\Folder1\folder2\backup4\backup4"_%_isodate% /COPYALL /E /W:5 /R:5
The name 'Folder' is used to indicate a regular folder, the name 'Backup' is used to indicate the backup folder.
This script clears the Firefox cache for the first profile in the directory. How do you do the same thing to every profile found if there are more than one? As you can see, I don't want to delete the folders named cache2, doomed, and entries. I just want to wipe their content.
CD /D %LOCALAPPDATA%\Mozilla\Firefox\Profiles\**.default\cache2
FOR /F "DELIMS=" %%I IN ('DIR /B') DO IF NOT "%%I"=="doomed" IF NOT "%%I"=="entries" (RMDIR "%%I" /S /Q || DEL "%%I" /S /Q)
CD /D %LOCALAPPDATA%\Mozilla\Firefox\Profiles\**.default\cache2\doomed
FOR /F "DELIMS=" %%I IN ('DIR /B') DO (RMDIR "%%I" /S /Q || DEL "%%I" /S /Q)
CD /D %LOCALAPPDATA%\Mozilla\Firefox\Profiles\**.default\cache2\entries
FOR /F "DELIMS=" %%I IN ('DIR /B') DO (RMDIR "%%I" /S /Q || DEL "%%I" /S /Q)
For some reason I am getting an "File Not Found" Error when I run this DOS command to delete empty folders and subfolders. It looks correct as far as I can tell. Does anyone have any suggestions?
for /f "delims=" %%x in (dir /s /b /ad ^| sort /r') do rd "%%x" 2>NUL
You are missing a tick (') in front of dir. Try this:
for /f "delims=" %%x in ('dir /s /b /ad ^| sort /r') do rd "%%x" 2>NUL
This is cleaner
for /f "delims=" %%x in ('dir /b /ad') do echo.rd /s /q "%%x"
Remove the echo. ONLY if the results appear to be correct.