I'm trying to delete all directories based off a naming convention using a batch file in windows. I don't want to delete all directories, only one's that match a pattern.
I'm doing this on Windows Server, so I don't know if that matters. I can get this to work on my personal desktop, but when I attempt on the Windows Server computer it doesn't work.
Echo Deleting Folders
cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp
pause
For /D /r %%i in ("*mail*") DO rd /Q /S %%i
pause
echo Done
When this is run, it iterates through all the directories and lists them. But then after it lists them all it says the following:
"The system cannot find the file specified."
"The system cannot find the path specified."
I find this odd since it literally lists every path and then says it can't find it. I'm sure I'm missing something small. Any help is appreciated.
Solved:
Echo Deleting Folders
cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp
pause
for /F "delims=" %%I in ('dir /S /B /A:D "*mail*" ^| sort /R') do #rd /S /Q "%%I"
pause
echo Done
Echo Deleting Folders
cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp
pause
for /F "delims=" %%I in ('dir /S /B /A:D "*mail*" ^| sort /R') do #rd /S /Q "%%I"
pause
echo Done
Related
I am trying to delete everything in a user defined location with exception on one pre-defined folder using a for loop. How do I go about adding a exception in order to not delete a folder.
I am trying to learn how to code, but I admit I am doing baby steps. I got some excellent tips for the first input part of this script, but I lack the knowledge to move forward. I have searched and found similar code, but none seems to work. This script is intended for flight simulation and hopefully ease the workload of installing a particular item.
This is just the part of the code due to stackoverflow guidelines, it deletes everything including the folder I want to exclude.
...
Rem This code is intended to delete all except one pre-defined folder
Echo Deleting all the files except testmappe3
del /s /q "%CD%"
for /d %%p in ("%CD%") do rmdir "%%p" except "%%testmappe3" /s /q
dir
Pause
...
I expected the output to delete all folders except testfolder3
for /d %%A in ("%CD%\*") do (
set "except="
if /i "%%~nxA" == "testmappe3" set "except=1"
if not defined except rmdir /s /q "%%~A"
)
This code will iterate the folders in the current directory.
If the name+extension of the folder is testmappe3,
then except will be set as 1 i.e. defined with a value.
If except is not defined, rmdir will remove the folder.
You can add more if lines for checking folders to except.
The modifiers will recognize a folder such as named
testmappe3.test1 as name testmappe3 and
extension of .test1.
View for /? and call /? about modifiers.
View for /?, set /?, if /? and rmdir /? for
help with those commands.
First of all, I would be a very careful deleting everything using %cd% especially if the script can accidently be run as Administrator, where %cd% would then be c:\windows\system32.
Instead, use %~dp0 as path to ensure that you are in the correct directory. This all assumes you did not cd somewhere else earlier in the script.
Then to the actual issue, I would include findstr to exclude your directory `testmappe3 as well as your script itself.
#echo off
cd /d "%~dp0"
for /f %%p in ('dir /b ^| findstr /vi /r /c:^testmappe3$') do (
rmdir "%%p" /s /q >nul 2>&1
if not "%%p"=="%~nx0" del /s /q "%%p" >nul 2>&1
)
If you want to stick to your original delete method, then it would be as below, but if your script is in the same dir, then it will also be deleted:
#echo off
cd /d "%~dp0"
del /s /q *
for /f %%p in ('dir /b ^| findstr /vi /r /c:^testmappe3$') do (
rmdir "%%p" /s /q >nul 2>&1
)
If your folder to exclude contains spaces, double quotes are required.. i.e
dir /b ^| findstr /vi /r /c:^"test mappe3"$
So I have a folder called Folder1 and inside of that folder has multiple other Folders for our build. The naming convention of those build folders are the date that they were built. For example: 20190314141438 (which is 2019-03-14 time 14:14:38). So what I like to do is to delete everything except for the last latest 3 folders. I started it out in a batch script but was able to delete it based on last modified date, which works, but need to somehow compare all the folders name and delete the old ones. Here is my script:
if not exist "%Location%" (
echo The Location - "%Location%" does not exist
) ELSE (
for /F "skip=3 delims=" %%i in ('dir "%Location%" /AD /B /O-D 2^>nul') do (
echo Removing "%Location%\%%i"
rd /Q /S "%Location%\%%i"
)
)
I was also thinking if it's not possible with a batch script, I can create a java program that does that if anyone has any idea on how i should start. Thanks.
UPDATE:
Some of the folders will also have a release number at the end of the folder name such as 20190314141438_Release2. How would I tell the batch file to ignore those. I was thinking of using findstr /C:"Release1"and somehow put that in a if else statement?
Thank you LotPings and Mofi for helping me out with this question. I have posted the script that works:
if not exist "%Location%" (
echo The Location - "%Location%" does not exist
) ELSE (
for /F "skip=3 delims=" %%i in ('dir "%Location%" /AD /B /O-N 2^>nul ^|
%SystemRoot%\System32\findstr.exe /I /V /C:Release') do
echo Removing "%Location%\%%i"
rd /Q /S "%Location%\%%i"
)
)
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 ""
From
How to delete files containing certain string using batch file in Windows?
I learned how to mass delete files which contain certain strings. What I did is
del *(2)* /f /s
but this did not delete directories. It only delete files.
How can I also mass-delete directories which contain certain strings?
There's no standard Windows command to delete files and directories on the same level. DEL is used for files, RMDIR / RD is used for directories (however it can delete files within directories).
RMDIR / RD does not work with wildcards, so you need to use a FOR loop. As it is, the below code will print out the commandos to delete the directories in your question. Remove the ECHO when you're confident the deletion will do what you want.
#ECHO OFF
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO (
ECHO RMDIR /S /Q "%%G"
)
You can also reduce this to a one-liner...
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%%G"
...and if you want to execute it directly in the shell (as opposed to from a .bat file), do:
FOR /F "tokens=*" %G IN ('DIR /B /AD /S "*(2)*"') DO ECHO RMDIR /S /Q "%G"
Flags explanation:
FOR
/F: iterate over a fileset
DIR
/B: bare format (needed so it works with FOR)
/AD: filter for directories
/S: work recursive
RMDIR
/S: work recursive
/Q: quiet mode
I am trying to set up a batch program that will go to the different computers on my network (from .txt file) and then delete files from the users on the that PC, and then empty the recycle bin. I've got the second part working so I can delete files from multiple users on a PC, but I can't get it to look at other PC's. I was hoping someone might point out what I'm missing here. Here is what I have so far:
#ECHO off
Setlocal EnableDelayedExpansion
FOR /F "delims=" %%i IN (test.txt) DO (
for /f %%a in ('dir /B /AD C:\Users') do (
REM for /f "tokens=*" %%a in (userlist.txt) do (
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Lotus\Notes\Data\workspace\logs"
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Google\Chrome\User Data\Default\*"
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files"
)
RD %systemdrive%\$Recycle.Bin /S /Q
)
pause
Anybody got any pointers?
You need to use UNC paths.
\\server\sharename\folder\file.ext
You can get a list of computers with
for /f "skip=3" %A in ('net view ^| findstr /C:"\\"') do Echo %A
All computers have a share for admins only called C$ that is the C drive (and D$ etc). $ makes it hidden. So
for the local computer via UNC
del /S /Q "\\127.0.0.1\C$\User\username\AppData\Local\Lotus\Notes\Data\workspace\logs"
Also there is no point to If Exist, just delete it - it will work or not. If you care if it worked or not you test the return value If errorlevel 1 echo error. You are causing extra disk access and network traffic. In programming we do and test not test and do.
You can also run a batch on the other computer.
wmic /node:"#Computerlist.txt" process call create "c:\\batchfile.bat"
Note \\ in paths. C:\\batcfile.bat is C: on the remote computer. This allows you to only use one for loop in your batchfile. You copy the batch file with copy. Net View can generate the computer list although you have to remove \\
for /f "tokens=1 delims=\" %A in ('net view ^| findstr /C:"\\"') do Echo %A