Windows Batch Scripting: Delete All Files Inside A Folder Except - windows

Hey i want to delete all files inside a folder except for few files, now these files i want to keep are normal folders & some of them are just blank files.
my files that i want to be kept
heres my current code, it keeps 1 folder only but i want to keep all 3 of the folders + my 2 blank files.
pushd "C:\Folder2" || exit /B 1
for /D %%D in ("*") do (
if /I not "%%~nxD"=="Important Folder1" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
popd
[Edit /] (from comment section)
I tried, like this:
pushd "%LOCALAPPDATA%\Google\ChromeVoter\User Data\Default\" || exit /B 1
for /D %%D in ("*") do (
if /I not "%%~nxD"=="Important Folder1" rd /S /Q "%%~D"
if /I not "%%~nxD"=="Important Folder2" rd /S /Q "%%~D"
if /I not "%%~nxD"=="Important Folder3" rd /S /Q "%%~D"
)
for %%F in ("*") do (
del "%%~F"
)
popd
It didn't work.

Related

Batch program to automatically sort zip files into folders and extract

I am a teacher aid, and my job is to sort student's labs into their folders. The format they turn in their files is like this
lastname_Labname.zip
What I wish to accomplish is to automatically have these zips moved into their folders in a different location. Which follows this format
Lastname1
Lastname2
Lastname3
Finally I would like the zip files to be deleted after.
Here is what I have so far:
To extract the files:
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
FOR %%X in (*.rar *.zip) DO (
"C:\Program Files\7-zip\7z.exe" x "%%X"
pause
)
popd
)
To sort the files:
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Something\Something"
SET "destdir=C:Somethign\Something"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.zip" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%\*%%b*" '
) DO (
MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
I would like to combine these functions, and to delete the zips after.
I have a similar setup.
#Echo OFF
set Down=E:\Download
set Extracted=E:\Extracted
for /R "%Down%" %%I in ("*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" x -y "%%~fI" -pPasswordGoesHere
Del "%%I"
)
FOR %%i IN ("%Down%\*") DO MOVE /Y "%%i" "%Sorted%\%%~nxi" /xf Unzip.bat
FOR /D %%i IN ("%Down%\*") DO ROBOCOPY /MOVE /E "%%i" "%Sorted%\%%~nxi" /xf Unzip.bat

Batch script to delete all folders except of "Starts with"

I have a folder with several subfolders. Structure is like:
C:\foo
C:\foo\web.foo
C:\foo\web.bar
C:\foo\win.foo
C:\foo\win.bar
C:\foo\mobile.foo
C:\foo\mobile.bar
I sometimes wish to delete the folders with its containing files with following batch script:
rmdir C:\foo /s /q
Here it didn't matter that the whole folder C:\foo was deleted completely.
But now I only want to delete only the subfolders of C:\foo with its containing files, which DO NOT start with "web.".
Do you have any good solution for this?
The following should do the trick, note it's a batch file using the current directory:
#echo off
for /F "delims=" %%D in ('dir /B /AD ^| findstr /V "^web."') do (
echo rmdir %%D /s /q
)
If it's okay remove the echo in front of rmdir.
The dir command just list directory names because of /AD and use a simple name output because of /B. To search on the beginning use findstr with /V. For negation use ^. Further the pipe symbol needs to be escaped ^|.
If you want a dynamic batch script that uses arguments you can use the following, call it via batchname.bat "C:\foo" web. (if it's okay remove the echo in front of rmdir.):
#echo off
set INVARGS=0
if [%1] == [] set "INVARGS=1"
if [%2] == [] set "INVARGS=1"
if %INVARGS% == 1 (
echo echo %0 ^<directory^> ^<directory_prefix^>
goto eof
)
set "folder=%1%"
set "prefix=%2%"
pushd "%folder%"
echo List of folders that should be deleted:
for /F "delims=" %%D in ('dir /B /AD ^| findstr /v "^%prefix%"') do (
echo "%cd%\%%D"
)
popd
:choice
echo.
set /P "c=Are you sure you want to continue [Y/N]?"
if /I "%c%" EQU "Y" goto yes
if /I "%c%" EQU "N" goto eof
goto :choice
:yes
echo.
pushd "%folder%"
for /F "delims=" %%D in ('dir /B /AD ^| findstr /v "^%prefix%"') do (
echo rmdir %%D /s /q
)
popd
:eof
This will remove all files begin with web.
#echo off
setlocal EnableDelayedExpansion
for /f %%F in ('dir /b /s') do (
set "name=%%~nF"
if /i not "!name:~0,4!" == "web." (
rm !name!
)
)

Batch : Search Directory in every Directory and Subdirectory

I want to delete all directories with a specific name. E.g. "vendor"
D:\Data\Project1\Dir\vendor
D:\Data\Project2\AnotherDir\AnotherDir\vendor
D:\Data\Project3\vendor
This is what I have at this moment. This lists all the folders, and subfolders.
for /d %%a in (*) do dir /ad /on /s /b %%a
Now I wish to put one path in a variable, and check whether the directory is equal to a name (for example "vendor"). But I can't find how.
Can you help me?
Regards,
Demian
This is my working solution if you have RimRaf installed:
#ECHO off
setlocal enableDelayedExpansion
:: Folders where the batch program doesn't need to search.
set skipTheseFolders=Decleir Pinokkio
:: All the folders that need to be deleted
set foldersToDelete=vendor node_modules
for /d /r %%d in (*.*) do (
#ECHO %%d
set folder=%%~nxd
set canI="true"
:: For loop to skip folders
for %%s in (%skipTheseFolders%) do (
:: Need to be implemented
set canI="true"
)
IF !canI! =="true" (
for %%l in (%foldersToDelete%) do (
:: If the folder is one of the folders To Delete
IF "!folder!" == "%%l" (
#echo %%d
cd %%d
cd ..
:: Using Rimraf because when you delete node_modules, normal delete won't work
start /B rimraf !folder!
)
)
)
)
The DIR command has an unfortunate limitation that it cannot recursively list specific folder names. The simplest solution is to list all folders, and use FINDSTR to filter out all but the folders that match.
dir /ad /on /s /b | findstr /iec:"\vendor"
Then you simply iterate the results using FOR /F and add your RD command
for /f "eol=: delims=" %%F in
'dir /ad /on /s /b ^| findstr /iec:"\vendor"'
) do rd /s /q "%%F" 2>nul
You can try this :
#Echo OFF
set "Folder=D:\Data"
set "FolderString=vendor"
Setlocal Enabledelayedexpansion
FOR /f "tokens=*" %%F IN ('dir /b /s /ad %Folder%\ ^| find "%FolderString%"') DO (set var="%%F"
ECHO rd /s /q !var! && rd /s /q !var!)
EndLocal
pause

Need your assistance with Windows cmd shell script

I have following script that removes all subdirectory from ROOT directory and removes all files in a ROOT directory except *.bat files.
pause
FOR /R C:\Temp\ %%F IN (*.*) DO IF NOT "%%~xF" == ".bat" DEL /F /S "%%F"
for /d %%i in ("c:\Temp\*") do rmdir /s /q "%%i"
pause
Help me improve script that it can also scan files *.bat in a ROOT subdirectories and remove all files with other extensions.
Test. If what it shows in console is what needs to be done, remove the echo command from destructive lines keeping the rmdir or del commands.
#echo off
setlocal enableextensions
set "root=c:\temp"
rem For the %root% tree
for /r "%root%" %%d in (.) do (
rem Test for the presence of .bat files under directory
dir /s /b "%%~fd\*.bat" 2>nul | find /v "" > nul
if errorlevel 1 (
rem If there is no .bat files under directory, remove directory
echo(**** rmdir /s /q "%%~fd"
) else (
rem There are bat files in directory or under it. Remove anything else in this directory
for %%f in ("%%~fd\*") do if not "%%~xf"==".bat" echo(-------- del "%%~ff"
)
)
EDITED - To adapt to comments - How to adapt the code so it will process more than one root folder? Take the code and convert it into a subroutine, then call it with the corresponding root. Iterate over the list of root directories
#echo off
setlocal enableextensions
for %%r in ("c:\temp" "c:\temp2" "c:\temp3") do call :processRoot %%r
endlocal
exit /b
:processRoot
setlocal enableextensions disabledelayedexpansion
set "root=%~1"
rem For the %root% tree
for /r "%root%" %%d in (.) do (
rem Test for the presence of .bat files under directory
dir /a /s /b "%%~fd\*.bat" 2>nul | find /v "" > nul
if errorlevel 1 (
rem If there is no .bat files under directory, remove directory
echo(**** rmdir /s /q "%%~fd"
) else (
rem There are bat files in directory or under it. Remove anything else in this directory
for %%f in ("%%~fd\*") do if not "%%~xf"==".bat" echo(-------- del "%%~ff"
)
)
endlocal
goto :eof

Windows batch script to delete everything in a folder except one

I have a script to delete all subfolders and files in a folder:
FOR /D %%i IN ("D:\myfolder\*") DO RD /S /Q "%%i" & DEL /Q "D:\myfolder\*.*"
And it works great!
Only problem is that I would like to exclude one or more folders, like the XCOPY exclude feature.
I just cant figure how I could add that to the script.
You could try to hide the folders before the for-loop, and unhide them afterwards, like this:
ATTRIB +H D:\myfolder\keepit
FOR /D %%i IN ("D:\myfolder\*") DO RD /S /Q "%%i" DEL /Q "D:\myfolder\*.*"
ATTRIB -H D:\myfolder\keepit
there needs to be an & just between "%%i" and DEL or else it will delete folders but not files.
Here is a way that does not touch the excluded file and/or directory, so no attributes are altered:
rem // Change to target directory (skip if not found):
pushd "D:\Data" || exit /B 1
rem // Iterate through all subdirectories:
for /D %%D in ("*") do (
rem // Exclude a certain subdirectory:
if /I not "%%~nxD"=="ExcludeDir" rd /S /Q "%%~D"
)
rem // Iterate through all immediate files:
for %%F in ("*") do (
rem // Exclude a certain file:
if /I not "%%~nxD"=="ExcludeFile.txt" del "%%~F"
)
popd

Resources