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

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"

Related

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.

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"

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

copy of files in windows cmd batch

I really cannot find anything to this problem:
I have a folder C:\dir1\dir2 I get this directory via reading a file so I don't know the name of dir1 or dir2 upfront. I want to copy dir2 into a target directory C:\target. At the end I want to have C:\target\dir2 and that dir2 in target shall have all the files which have been in the source of dir2. When I try xcopy with /s switch it copies the files in the source dir2 directly into target without creating dir2 in target. How can I make sure that this directory is created?
I must achieve that dir2 is created automatically.
EDIT:
for /F "tokens=*" %%i in (%maindir%\mydir.txt) do call :process2 %%i
:process2
set sourcefile=%*
xcopy /s "%sourcefile%" target
is basically the part of my batch file in question. Where in mydir.txt I get some directory like C:\dir1\dir2 which contains files file1 file2 etc. In target I have now
target\file1
target\file2
but I want target\dir2\file1 and target\dir2\file2
Hope that makes it a bit clearer.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir\with space"
SET "destdir=c:\destdir"
FOR /f "delims=" %%a IN ("%sourcedir%") DO (
XCOPY /s "%sourcedir%" "%destdir%\%%~nxa\"
)
GOTO :EOF
This should get the job done for you. I'll presume you've already got sourcedir set from your file – I just used a directory I had which happens to contain spaces in its name. Possibly you'd want to append >nul to the XCOPY line to suppress messages.
Your question is a little unclear, but if I understand it correctly, you should have a look at the mkdir command. That will make your directory - only if the directory doesn't already exist. The syntax is:
MKDIR [drive:]path
For example, mkdir \2014\photos\beach makes the directory "2014" in the current directory ( use CD) with the sub folders "photos" and "beach".
That being said, if you have proper syntax for xcopy, it should create the new directory for you IF it doesn't already exist. Have a look again at xcopy.
If the directory does exist, this is a pretty "sure-fire" way to remove it before trying to make it again:
if exist {directory} rd /s /q {directory}

Replace multiple files in sub directories with file from a different directory

I would like to take a file named test1.hfl from one directory and replace all the existing test1.hfl files inside the sub directories of the folder runs in my c drive.
I have started the batch file with the following code:
FOR /R C:\Users\----\Documents\Train\Runs\ %%I IN (*test1.hfl) DO COPY /Y C:\Users\----\Documents\test1.hfl %%~fI
But it doesn't work.
Please let me know if you can see something wrong.
try this:
cd /d "C:\Users\----\Documents\Train\Runs"
FOR /D /R \ %%a IN (*) do if exist "%%~a\test1.hfl" echo copy /y "test1.hfl" "%%~a"
Look at the output and remove the word echo if it looks good.
Accepting an answer - how does it work?

Resources