Search through an array of paths to delete folders containing string - Batch - arrays

I want to search and delete folders containing a specific string in their names, or delete all files in a folder except one, using a list of paths to browse.
Here's a working example, without the array of paths :
#echo OFF
set "sources0=%userprofile%\New folder with spaces"
set "sources1=%userprofile%\New folder with spaces 2"
set "folder1_to_delete=[test.com]"
set "folder2_name=My folder"
set "file2_to_keep=My app.lnk"
for /D /R "%sources0%" %%d in ("*%folder1_to_delete%*") do (
rem RD /S /Q "%%d" >nul
echo Found1: "%%d"
)
for /D /R "%sources1%" %%d in ("*%folder1_to_delete%*") do (
rem RD /S /Q "%%d" >nul
echo Found1: "%%d"
)
for %%i in ("%sources0%\%folder2_name%\*.*") do if not "%%~ni%%~xi" == "%file2_to_keep%" (
rem DEL /Q "%%i" >nul
echo Found2: "%%i"
)
for %%i in ("%sources1%\%folder2_name%\*.*") do if not "%%~ni%%~xi" == "%file2_to_keep%" (
rem DEL /Q "%%i" >nul
echo Found2: "%%i"
)
pause
exit
Output :
Found1: "C:\Users\marin\New folder with spaces\123 [test.com]"
Found1: "C:\Users\marin\New folder with spaces 2\456 [test.com]"
Found2: "C:\Users\marin\New folder with spaces 2\My folder\New file.txt"
But as I have many search paths, I want to iterate through an array or a list. Here's what I tried, but which doesn't work as expected :
#echo OFF
setlocal enabledelayedexpansion
set "sources[0]=%userprofile%\New folder with spaces"
set "sources[1]=%userprofile%\New folder with spaces 2"
set "folder1_to_delete=[test.com]"
set "folder2_name=My folder"
set "file2_to_keep=My app.lnk"
for /L %%n in (0,1,1) do (
set "source=!sources[%%n]!"
for /D /R "!source!" %%d in ("*%folder1_to_delete%*") do (
rem RD /S /Q "%%d" >nul
echo Found1: "%%d"
)
for %%i in ("!source!\%folder2_name%\*.*") do if not "%%~ni%%~xi" == "%file2_to_keep%" (
rem DEL /Q "%%i" >nul
echo Found2: "%%i"
)
)
pause
exit
Output :
Found2: "C:\Users\marin\New folder with spaces 2\My folder\New file.txt"
Is there a way to achieve what I want, and/or is there a better method to declare and iterate through a list or an array of paths?
Thanks!

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
)
)

How to delete only text files except few files

In a certain path I have some different kinds of file type. Eg., .txt, .bas, .cls, etc.
I need to delete only the text files in that path except few files.
For eg, if the path has a.txt, b.txt, c.txt, aa.bas, bb.cls, it should delete only a.txt. It should not delete b.txt and c.txt (Also it should not delete the other extension files).
To delete all ?.txt files in the root folder, excluding b.txt and c.txt
#echo off
for %%i in (?.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
To do this in the root and subdirectories:
#echo off
for /R %%i in (?.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
If the files are to be all *.txt files and not just single digit as per your example (add /R to recurse:
#echo off
for %%i in (*.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
Similarly, but using findstr to only exclude:
#echo off
for /f %%i in ('dir /b /a-d ^|findstr /vi "b.txt" ^|findstr /vi "c.txt"') do (
echo del "%%i"
)
and to search only include:
#echo off
for /f %%i in ('dir /b /a-d ^|findstr /i "a.txt"') do (
echo del "%%i"
)
and to include and search subdirectories:
#echo off
for /f %%i in ('dir /b /s /a-d ^|findstr /i "a.txt"') do (
echo del "%%i"
)
On all of the above examples, remove echo to actually perform the delete, echo is used as a safety measure and will only display the del result to console.
Edit
Seeing as you specifically have a list of files (as per one of you comments) to exclude, you can use something like this. You have to create a file called exclusion.txt and add the files to exclude in list form:
b.txt
c.txt
file with space.txt
d.txt
Then create the batch file and add the code below. When ran, it will prompt for the file extention to filter on, where you can type an extension. i.e txt or simply press enter to perform a delete on all files, except the excluded ones. Just to be safe, I added an additional for loop to simply echo the files and prompt you if you are sure you want to delete the files.
#echo off
set cnt=0 & set excl= & set ext=
echo(
if not exist exclusion.txt echo You have not created an "exclusion.txt" file. & echo( & echo You need to create it first, then rerun the script & echo( & pause & goto :eof
echo Ensure you have listed all files to be excluded in "exclusion.txt" file
echo(
set /p "ext=Add File extention to search on (txt, pdf, etc), or press enter for all files: "
if not defined ext goto cont
if not "%ext:~0,1%"=="." set "ext=.%ext%"
set "ext=*%ext%"
:cont
setlocal enabledelayedexpansion
for /f "delims=" %%a in (exclusion.txt) do (
set /a cnt+=1
set "nlr!cnt!=%%a"
)
for /l %%i in (1,1,%cnt%) do (
if not defined excl (
set "excl=!nlr%%i!"
) else (
set "excl=!excl! !nlr%%i!"
)
)
echo(
echo WARNING: You are about to delete the following files!!
echo(
for /f "delims=" %%i in ('dir /b /a-d %ext% ^|findstr /VIE "%excl%"') do (
if /i not "%%i"=="exclusion.txt" if not "%%i"=="%~0" echo %%i
)
echo(
Choice /c YN /m "Are you sure you want to delete these files?"
if %errorlevel% equ 2 goto :eof
for /f "delims=" %%i in ('dir /b /a-d %ext% ^|findstr /VIE "%excl%"') do (
if /i not "%%i"=="exclusion.txt" if not "%%i"=="%~0" del %%i
)

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

Script to delete all folders except

I found a script that will delete all folders except a certain one
for /d %i in ("C:\test\*") do if /i not "%~nxi"=="test2" rd /q "%i"
How do you change this to add more folders that I don't want deleting?
#echo off
set "list=test1 test2 test3"
setlocal enableDelayedExpansion
set "delete=0"
pushd "C:\test\"
for /d %%i in (*) do (
set "delete=1"
for %%# in (%list%) do (
if /i "%%i" equ "%%#" (
set "delete=0"
echo %%i will be not deleted
)
)
if !delete!==1 (
rd /s /q "%%i"
)
)
popd

Batch to find a folder in subfolders, move files one level up and delete that folder

I have this structure:
..
..\FolderA\FolderX\File1.txt
..\FolderB\FolderX\File2.txt
..\FolderC\FolderD\FolderE\FolderX\File3.txt
I need a batch to find all "FolderX" in subfolders and move all the files in "FolderX" to one level up and delete that "FolderX"
..
..\FolderA\File1.txt
..\FolderB\File2.txt
..\FolderC\FolderD\FolderE\File3.txt
How write a batch? I tried this, but is incomplete, the code not find the folders:
#Echo Off
Set _Source=%~dp0
Set _FindDir=FolderX
Set _Path=%_Source%\%_FindDir%
If Exist "%_Path%" (
Move /-Y "%_Path%\*.*" "%_Source%"
For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Path%"') Do Move "%_Path%\%%I" "%_Source%"
RD /S /Q "%_Path%"
) Else (
Echo There is no %_FindDir% folder in %_Source%
)
Resolved:
#echo off & setlocal enabledelayedexpansion
for /d /r %~dp0 %%a in (*) do (
if /i "%%~nxa"=="FolderX" (
set "folderpath=%%a" (
move /y !folderpath!\* !folderpath:~,-8!
rmdir !folderpath!
)
)
)

Resources