Windows rmdir batch to delete FOLDERS but skip files - batch-file

So i have a dir called c:\user\jdoe\desktop\Folder1\
Inside folder1 i have many folders with subfolder and files.
I have a 7-Zip batch that zips all the folders in place.
So now i have subfolders and subfolders.zip inside folder1.
I need a batch that will detele all the non zipped folders but skip the zip files and the .bat file that lives in c:\user\jdoe\desktop\Folder1\
Any ideas?
If i can get a 7-zip batch that will zip fodlers (example subfolder1.zip subfolder2.zip) and then afterwards delete all non zipped folders that would be great.
Been all over the internet and after deleting half my data by testing my own scripts i have now decided to come here.

So after a long collaboration with a friend we came up with this.
Tested this and it is working 100%
REM create list of directories
SET cwd="C:\Users\user\Desktop\New folder"
SET date=date /T
CD %cwd%
DIR /AD /B >dirlist.txt
REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
"c:\program files\7-zip\7z.exe" a "%%a.zip" "%%a"
del /s /q "%%a"
rmdir /s /q "%%a"
)
del dirlist.txt
pause

Related

Batch file that would delete all the files and folders that the batch file is located in?

Is it possible to make a batch file that would delete all the files and folders that the batch file is located in?
I.E: I place the batch file into folder with useless files, I run it, and it deletes all the files and folders in that folder.
Then I can just move the batch file to another folder and do the same there...
Would really help me out! I need this to delete temporary files on other people's computers before installing new files... But sadly I am not very familiar with batch.
To remove all the files in the current directory except your batch file use:
echo off
for %%i in (*.*) do if not "%%i"=="del.bat" del /q "%%i"
Note that the "del.bat" is the name of the batch file you save this as.
As another side note you could add after the del /q the "/p" command to make this a little bit safer. That way it will prompt you before deleting each file.
To remove everything in the folder including the .bat file use
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
To remove everything including folders except the .bat file use
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || if not "%%i"=="del.bat" del "%%i" /s/q)
Where del.bat is your batch file name.

Batch file to delete folder but leave a certain subfolder?

I'm trying to clear a folder of all it's contents but a certain sub-folder and its contents. My deleting works fine but I can't figure out how to exclude.
cd C:\testfolder
del * /S /Q
rmdire /S /Q "C:\testfolder"
But I do not want to delete the folder C:\testfolder\subf. How can I do this?
If you are using at least windows vista (robocopy command is used), this should do the job
#echo off
setlocal enableextensions disabledelayedexpansion
rem Create a temporary empty folder
set "tempFolder=%temp%\%~nx0.%random%%random%%random%.tmp"
md "%tempFolder%" >nul 2>nul
rem Purge from target folder anything not in the empty source folder,
rem but exclude the indicated folder
robocopy "%tempFolder%" "c:\testfolder" /nocopy /purge /xd "c:\testfolder\subfolder"
rem Cleanup
rmdir "%tempFolder%" /s /q >nul 2>nul
If you mean you want to delete all files but not the subfolder you can just use the following
cd C:\testfolder
del *.* /S /Q
This will delete any files in the "testfolder" and any files in the subfolders but will leave the subfolder there.

Creating files in all subfolders of a file with a bat file

I have a folder with lots of subfolders and I want to create a folder in each of these subfolders. As There is a lot of these folders I was wondering if there was a way of automatically doing this with a bat file.
This should work for your problem
FOR /d %A IN (d:\softwares\files*) DO mkdir %A\source
Run this in the folder in question: it works on all subfolders in the tree.
If you only want to process the immediate set of folders then remove the /s
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do md "%%a\folder" 2>nul
echo done
pause

How to delete folder while deleting file inside it?

Can we delete the folder too when deleting the file inside it using batch command.I have the file name buy not the folder name.So when my code deletes that file inside the folder i want the folder to be deleted as well,as its empty.
Code
String tmpfolder = System.getProperty("java.io.tmpdir");
Runtime.getRuntime().exec("cmd /c del "+tmpfolder+"IEDriver.dll /f /s /q");
This code deletes the file but not folder.
Runtime.getRuntime().exec("cmd /c \"for /f \"delims=\" %a in ('dir /a-d /b /s \""+tmpfolder+"IEDriver.dll\"') do rd /s /q \"%~dpa\" \"");
This uses dir command to search for the dll and removes the folder in where it is stored.
Not tested.

delete all folder except one using batch

I have a folder named Scripts in current directory Say C:\QTP\Script\. I am not sure the folders names and count in that scripts folder. Inside each unknown folder name i have Objective Evidences folder. I want a code that deletes all the files and folders in unknown folders but not objective evidences folder.
EG:
Scripts\Folder1\Objective Evidences (this folder1 contains many files too)
Scripts\Folder2\Objective Evidences (This folder2 also contains many files)
I am not sure how many folders are present in scripts folder and wat are their names.
I should get the list of folder names present in Scripts folder and delete all files and folder present in it except Objective Evidences folder.
Please let me know the batch code for the same.
for /f "tokens=*" %%G in ('dir /b /s /a:d "C:\QTP\Script\*"') do (
echo %%~fG | FIND "Objective Evidences" || rd /s /q "%%G"
)
Try this...Or this
pushd "C:\QTP\Script\"
for /D /R %%a in (*) do echo "%%~fa" | find "Objective Evidences" || rd /s /q "%%a"
popd

Resources