Batch-file forcing script to remove folder inside subfolders - windows

I need to delete 4 files, and 1 folder in a specific location and in unknown number of subfolders(different names). I would like to keep this as simple as possible. I managed to make this almost work, without any loops, but it doesn't work with folder removal in subfolders. All 4 files are deleted in main folder and all subfolders, but folder "scss" with all his content is still present in subfolders.
How to force this script to additionally delete folder in subfolders too ??
START /c cmd %this_dir%
DEL gulp_start.bat, .brackets.json, package.json, gulpfile.js /s /q scss
rmdir scss

You will have to use a For Loop.
Example
Add following to your batch.
FOR /d /r . %%d IN (scss) DO #IF EXIST "%%d" rd /s /q "%%d"

Related

How to search/delete multiple subfolders?

I have a "clean up" batch program that sits in a parent directory & deletes certain extraneous files within the directory "tree".
One function is to delete extra ".backup" save folders (just .backup, not main folders)
For reference, the folder path is like:
\Parent folder (w/ my .bat)
\Saves
\save 1\save files
\save 1.backup\save files
\save 2\save files
\save 2.backup\save files
(etc)
I can remove the unwanted subfolders using
for /d %%x in (Saves\*.backup) do rd /s /q "%%x"
However, I want to add a check into the code to verify that the folders have actually been deleted & give correct message for success, failure, no folder, etc. outcomes.
I can do that with file deletion functions using IF EXIST/IF NOT EXIST arguments, but I'm having trouble applying these to the subfolder deletion.
I can look for files in the save folders to satisfy the EXIST/NOT EXIST arguments, but using the * wildcard in the subfolder name seems to be throwing a wrench into the works, and the program fails to detect any folders/files using that statement. However if I define a specific save folder outright, the subroutine will work perfectly.
So:
if not exist "Saves\save 1.backup\*.*" ( goto nofile )
if exist "Saves\save 1.backup\*.*" ( for /d %%x in (Saves\*.backup) do rd /s /q "%%x" )
will delete the correct folders (or give a message if no valid folders), but
if not exist "Saves\*.backup\*.*" ( goto nofile )
if exist "Saves\*.backup\*.*" ( for /d %%x in (Saves\*.backup) do rd /s /q "%%x" )
doesn't delete anything, & just gives my "nofile" message.
Obviously, specifying every possible save name isn't an option.
Any solutions?
p.s. to head off any extra questions: I want to [occasionally] delete these backups because the program makes a new one each time it saves.
You can check folder into for loop after command rd.
for /d %%x in (Saves\*.backup) do (
rd /q /s %%x
if exist %%x (echo Failed) else echo Success
)

Moving files from more than one folder to the parent directory using CMD

I have a folder Target contains multiple folders,
each folder contains one file with the same name of the folder
I want to move the files in folders Part1,Part2,Part3,Part4 and Part5
to parent folder ("Target" in this case) using cmd then delete the folders.
The result should be like that :
In Linux i could've used mv Part*\*.* ..
I've tried copy "Part*\*" "" command,
but it doesn't work.
Use a For loop. The key to getting directory names in this code is "dir /a:d" which only lists directories. I put that into the %%a variable. Use %~dp0 to refer to the directory the batch file is in. If your bat is somewhere else, do a find and replace all for that to the directory path you need. Lastly use RMDIR to remove each folder with /q /s to make it silent and remove all files within the directory (part1 part2 etc...) and the directories themselves.
#echo off
for /f "tokens=* delims=" %%a in ('dir /a:d /b "%~dp0"') do (
copy "%~dp0%%a\*.*" "%~dp0"
RMDIR /q /s "%~dp0%%a"
)
In the Windows Command Prompt, copy "Part*\*" "" cannot work, because wildcards are only permitted in the last element of a path but not somewhere in the middle.
To work around this, use a for /D loop to resolve wildcards in Part*, then let copy deal with the remaining ones:
for /D %I in ("Part*") do #copy "%~I\*" ".."
To move the files rather than to copy, simply replace copy by move. If you want to remove empty sub-directories then, append rd using &&:
for /D %I in ("Part*") do #move "%~I\*" ".." && #rd "%~I"
To use the above code fragments in a batch-file, ensure to double all the %-signs.

Windows Command to delete existing directory and create new

I have a "release" folder inside c:\data.
I would like to delete the entire "release" folder, its sub directories and all files inside them using windows commands. After deleting it, I need to recreate the "release" folder and add another folder "app" inside "release" folder using commands.
I tried using rd /s and few other commands but it complains about non empty directory and workarounds to re-try rd /s command. I thought it should be fairly easy to do. Does anyone have a script / commands for them?
You can of course remove (rd) and recreate (md) a directory. However, you will lose its attributes then (like Hidden or Owner).
To maintain attributes, remove the contents of the directory but not the directory itself, either doing something like this:
rem // First remove all sub-directories with all their contents:
for /D %%I in ("C:\Data\release\*") do rd /S /Q "%%~I"
rem // Then delete any files located in the target directory:
del /Q /A "*.*"
Or this:
rem // Change into the target directory:
cd /D "C:\Data\release"
rem /* Try to remove the target directory and all its contents; since you are
rem in the target directory, it cannot be removed, although its contents can;
rem the `2> nul` portion suppresses the error message about access problems: */
rd /S /Q "." 2> nul
RM is not a windows cmd command. This should work:
IF EXIST "c:\data\" (
RD c:\data\release /S /Q
MD c:\data\release
MD c:\data\release\app
)

How to delete a specific sub-folder in a parent folder using a batch file?

I would like to create a batch file deleting or removing the folder C:\temp\root\students\type1, its subfolder and all the files.
The folders and files I do have are as follows:
C:\temp
C:\temp\root
C:\temp\root\students
C:\temp\root\tutors
C:\temp\root\students\type1
C:\temp\root\students\type2
C:\temp\root\tutors\type1
C:\temp\root\tutors\type2
C:\temp\root\students\type1\details.txt
C:\temp\root\students\type1\assignment1
C:\temp\root\students\type1\assignment1\results.txt
The folder C:\temp\root\students\type1\assignment1 is specified in the batch file.
I would like to go one folder/directory up and delete or remove C:\temp\root\students\type1 in my batch file (test.bat).
Please help me with this.
Use this:
rd /s /q C:\temp\root\students\type1
It will remove files and folders recursively, caution, without prompt. It's kind of a substitute for the good old deltree. Best.
EDIT: within your notepad, create your mygoodbatch.bat with this content:
md C:\temp
md C:\temp\root
md C:\temp\root\students
md C:\temp\root\tutors
md C:\temp\root\students\type1
md C:\temp\root\students\type2
md C:\temp\root\tutors\type1
md C:\temp\root\tutors\type2
md C:\temp\root\students\type1\details.txt
md C:\temp\root\students\type1\assignment1
md C:\temp\root\students\type1\assignment1\results.txt
rd /s /q C:\temp\root\students\type1
(md is the make dir dos command, so all the lines beginning with it are creating the folders - the last line is the one to del your level 4AA)
Save this batch on the root of your disk and then run it. I really wish this is what you want. What I yet didn't understand is: you are creating a directory structure and, at the same time, excluding level 4AA... is that it?
You can use a for loop to get the parent folder for a given folder:
set target_dir=C:\temp\root\students\type1.test\assignment1
for %%a in ("%target_dir%") do (
echo Removing %%~dpa%
rd /s/q %%~dpa%
)

How to Delete all folders inside a folder except one in windows batch script?

I tried to delete a files and folder inside a folder except one ,but so far I didn't get a perfect answer for it?
Can any one help on this above?
My folder structure seems like this:
I have ABC Folder under E:\ Inside that folder I have 4 files named A.txt,B.txt,C.txt & D.txt and I have 3 Folders in that they are AB , BC & CD ...I want to remove all files and folders in ABC folder except CD folder inside ABC folder..
Can any one please help on the above?
Test this - it should work.
pushd "d:\abc\cd" && rd /s /q "d:\abc" 2>nul
pushd "d:\abc\cd" && rd /s /q "d:\abc" 2>nul will delete all the files inside the folder cd.
Since you know exactly yout directory tree, you can just remove all element you don't need:
del e:\abc\ab\*.*
rmdir e:\abc\ab
del e:\abc\bc\*.*
rmdir e:\abc\bc
More general solution:
for /D %%d in (e:\abc\*) do if "%%d" neq "CD" rmdir "%%d"

Resources