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

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

Related

How to auto create folder based on filename and move the file into it's folder using .BAT

I have already solved my question... What I haven't solved is how to do this if the .bat file is located in a parent folder and that it should work on all subfolders?
Right now, there's a limitation that it only create folders if the .bat file is located in the same folder as the files. It can't create folders if the files are inside a subfolder.
What I have is:
the filename of this .bat is :
organize.bat
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
How I do it right now:
I place the .bat file in a folder together with the files
When I click it, it will create folders with a name based on the files inside that folder
It will also move each files in those folders of the same name
What I need:
Place the .bat file in the main folder with many subfolders containing the files
Click it to perform the same tasks above
Apologies if my explanation is confusing... I hope it's still understandable.
Thank you in advance!
Your attempt is very close to working but beware the wrinkles of using a simple approach without checking each detail so, start here:-
#echo off & Title %~n0
REM I recommend using cd /d "%~dp0" to ensure you start from the known cmd file folder location not some system folder
cd /D "%~dp0"
REM add the /R switch to run through subdirs
for /R %%i in (*) do (
REM replace organize to %~n0 so as to aid renaming by other users
if not "%%~DPni" == "%~DPn0" (
REM to allow for nested paths we need a fuller DP location for N (check it works exactly as desired then remove the echos)
echo md "%%~DPni" && echo move "%%~i" "%%~DPni"
)
)
BEWARE files with double .dot.s such as cmd.exe.lnk so check those echo's first
md "C:\Users\me\Favorites\Links\cmd.exe"
move "C:\Users\me\Favorites\Links\cmd.exe.lnk" "C:\Users\me\Favorites\Links\cmd.exe"

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
)

Batch-file forcing script to remove folder inside subfolders

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"

Batch copy command doesn't reach subfolder

I have a batch command to copy and move file with a new name to another directory. When I added one more level of subfolder it does not copy but when I remove the added subfolder and move the file to the previous level it does copy. Here is it is:
cd /d dir "U:\Sourcing\Vendor Demand Planning\Customer CPFR\BBM\"
for /f "delims=" %%I in ('dir /b /o:-d "BBM Tool *.xlsx"') do (
copy "%%I" "..\Pricing Project\BBM Tool.xlsx"
exit /b
)
If I removed the BBM level and move the file into the Customer CPFR level it works. I edited this batch from another one that had the target file in the CPFR folder. The file I need to copy is in the BBM folder.
Change COPY to XCOPY and use /S
See XCOPY /?
I'd say that the "Pricing Project" directory is on the same level as "Customer CPFR" hence the target of the copy should be "..\..\Pricing Project...
(later edit)
Also dir in the CD line should be removed.

Resources