Recursively find and delete a folder using batch file - file

I am trying to write a simple batch file which will recursively find and delete a folder. But the following script is not looking under sub folder. Wondering how to do that?
#echo off
cd /d "C:\"
for /d %%i in (temp) do rd /s "%%i"
pause
Thanks!

for /d /r "c:\" %%a in (temp\) do if exist "%%a" echo rmdir /s /q "%%a"
For each folder (/d), recursively (/r) under c:\ test for the presence of a temp folder and if it exist, remove it
directory removal command is only echoed to console. If the output is correct, remove the echo command

The /S switch to rd means
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
It does not mean it will search all directories looking for one with the specified name and delete them.
In other words, if you run rd /S Test from the C:\Temp folder, it will delete C:\Temp\Test\*.*, including all subdirectories (of any name) of C:\Temp\Test. It does not mean it will delete C:\Temp\AnotherDir\Test, because that isn't a subfolder of C:\Temp\Test.

Related

Batch command to delete all subfolders with specific name having special character

I have a directory as such:
D:\DATA\DATA2\Demo&_1\Demo\1\deployment
D:\DATA\DATA2\Demo&_1\Demo\2\deployment
D:\DATA\DATA2\Demo&_1\Demo\3\deployment
And I am running the below batch file command to delete all deployment folders present inside Demo folder:
cd /d D:\DATA\DATA2\Demo&_1\Demo
FOR /d /r . %%d IN (deployment) DO #IF EXIST "%%d" rd /s /q "%%d"
The above command is not working(not deleting deployment folder) when I have special character in in folder name (Demo&_1) and when I have do not have special then it is working(deleting deployment folder).
For e.g. if I change name of folder "Demo&_1" to "Demo1" the above batch file will work fine but not work if have special character.
Simply because the paths are not in quotes, but regardless here is a much simpler solution without having to do cd /d
for /D /R "D:\DATA\DATA2\Demo&_1\Demo" %%d IN (deployment) DO #IF EXIST "%%d" rd /s /q "%%d"

how to delete subfolders without deleting parent folder and child folder?

For this type of directory structure:
\\rdwlhsdevserver\root\user1\folders\testdat.txt
\\rdwlhsdevserver\root\abhay\testdat.txt
\\rdwlhsdevserver\root\testuser\folders1\folder2\testdat.txt
\\rdwlhsdevserver\root\devadmin\input\testdat.txt
\\rdwlhsdevserver\root\admin\testdata\testdat.txt
I know that I can use del /s /q \\rdwlhsdevserver\root\* to delete files from parent folder and all sub-folders. But I want to delete all folders and files except \\rdwlhsdevserver\root\<folder>\.
After running cmd output should be like:
\\rdwlhsdevserver\root\user1\
\\rdwlhsdevserver\root\abhay\
\\rdwlhsdevserver\root\testuser\
\\rdwlhsdevserver\root\devadmin\
\\rdwlhsdevserver\root\admin\
pushd "\\rdwlhsdevserver\root" && (
for /d %%a in (*) do ( cd "%%a" && ( 2>nul rmdir . /s /q & cd .. ) )
del /f /q *
popd
)
This will change the current active directory (pushd) to the target directory and if there are no problems (conditional execution operator &&) for each folder (for /d) change to it (cd), remove its contents (rmdir) and return to the parent folder. Once done, remove (del) the files inside the root folder and restore the initial active directory.
Why not change the inner for to for /d %%a in (*) do rmdir "%%a" /s /q ? Because this will also remove the folder. But if we first make the folder the current active directory (cd) we will be able to delete its contents but not the folder itself as it is in use (the 2>nul is a redirection of the stderr stream to nul to hide the error in the rmdir saying it can not remove the folder because it is in use)
You need to iterate over all sub-directories of \\rdwlhsdevserver\root\ using a for /D loop, then loop over each sub-directory's sub-directories again by another for /D loop, then apply the rmdir (or rd) command on each of the returned items, like this:
for /D %%J in ("\\rdwlhsdevserver\root\*") do (
for /D %%I in ("%%~J\*") do (
rmdir /S /Q "%%~I"
)
)
Or in command prompt directly:
for /D %J in ("\\rdwlhsdevserver\root\*") do #for /D %I in ("%~J\*") do #rmdir /S /Q "%~I"
For the sake of overall performance, if you want to delete directories and files, I recommend to do the above directory removal before the file deletion (del /S /Q "\\rdwlhsdevserver\root\*"). Note that your del command line also deletes files located in the directory \\rdwlhsdevserver\root\.

Why is my bat script deleting itself when I run it

I have made a bat script as follows
cd "D:\ACT\ACTBKUP"
del /Q *.*
FORFILES /P E:\ACT_Backups /M *.zip /D +1 /C "cmd /c del #D:\ACT\ACTBKUP"
The script is supposed to delete everything in "D:\ACT\ACTBKUP" and then move the newest zip folder to that same folder from E:\ACT_Backups. When I run this script from windows server 2012 it just disappears.
thanks
In order to switch to a directory that is located on a different drive, you need to use cd /d instead of just cd. If you do not do this, the directory will not change.
When you run a script by double-clicking on it, batch considers the current directory to be the directory where the script is currently located. Since you are not using the /d option with cd, you are running del /Q *.* on the directory where the script is located.
To fix this, you have two options:
cd /d "D:\ACT\ACTBKUP"
del /Q *.*
or
del /Q "D:\ACT\ACTBKUP"
There is no option in forfiles to get just the most recent file; /D +1 will return all files with a last-modified date of today or later. In order to get the most recent file and nothing else, you will need a for /f loop:
rem /b returns just the file name and /o:d sorts the files by date
rem Since newest_file gets set for each .zip file in the directory,
rem the last file set will be the newest
for /f "delims=" %%A in ('dir /b /o:d E:\ACT_Backups\*.zip') do set newest_file=%%A
copy %newest_file% D:\ACT\ACTBKUP

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.

batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory

So if I have C:\drivers\* with * indicating many sub-folders, I want to find out where my inf files are located, and then copy ALL files that are located in the same directory where my inf files are located and all sub-directories.
It has been easy to create a script that will copy all .inf files found to my directory:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y %%a C:\test
But copying the other files that are located in the same directory and all sub-directories has been difficult.
Such as, if the inf file is located under C:\drivers\sbdrv\hseries\usb30\amdhub\w7 and the sys file is located in the sub-folder of x86, I need the sys file to be kept in the same sub-folder but under the destination of C:\test\x86.
Any ideas?
EDIT: Maybe this will make it easier. As soon as it finds one .inf file in a folder, it should copy the entire folder over to test as well as all sub-folders and move on to the next one. So if it sees the first .inf file located C:\drivers\sb3045\sb407.inf it should copy all files and folders under sb3045 without copying the folder sb3045 itself, and then move on to folder C:\drivers\sb4055\drivers\oem\intel\id6077.inf and copy all files and folders under the intel folder without copying the intel folder itself.
EDIT2:
It looks like this will work, but it is slow as it finds every .inf and is copying over any old files if there is more than one .inf file per folder
#ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION
CD\
CD drivers
FOR /f "tokens=* delims=" %%B IN ('DIR /b /s /o:gen .inf') DO (
XCOPY "%%~dpB.*" "C:\test\" /e /c /h /y
If anyone has a cleaner or quicker idea, let me know please. Until then, I'll have to work with this one.
try this:
FOR /R C:\drivers\ %%a in (*.inf *.cat *.sys) do xcopy /c /h /y "%%~dpa*" C:\test
for /R C:\WINDOWS\inf %%a in (*.inf*) do (
xcopy /qv /T %%a .\test\
xcopy /qv %%a .\test\
)
pause
this code worked for me.

Resources