What am I doing wrong with my .bat that deletes a folder? - batch-file

I have a folder named "Red" and in it is a jpeg. I want to delete this folder, and the picture inside, but I don't want it to ask me if i want to delete it, I just want the program to delete it and move on.
It's located on my desktop.
Here is the code I have to delete the folder and picture
del /Q "C:\Users\Chris\Desktop\Red"
When I run the .bat it doesn't do anything, it just opens the CMD and closes. Is there a problem with the syntax? Thanks!

The del command is not the right tool for deleting a folder and its contents. Rather you should use rmdir (or rd) instead, with the option to delete subfolders/files (and without prompting):
rmdir /s /q whatever
The del command will work quietly without prompting and it will descend through directory structures but it will only delete files in those structures. If you set up the following hierarchy:
xyzzy
|
+----- xyzzy.txt
|
+----- plugh
|
+----- plugh.txt
|
+----- twisty
|
+----- twisty.txt
and then run del /s /q xyzzy, all you will see is:
Deleted file - C:\Users\Pax\Documents\xyzzy\xyzzy.txt
Deleted file - C:\Users\Pax\Documents\xyzzy\plugh\plugh.txt
Deleted file - C:\Users\Pax\Documents\xyzzy\plugh\twisty\twisty.txt
and you'll be left with the tree untouched (but all the files gone):
xyzzy
|
+----- plugh
|
+----- twisty
If instead you use rmdir /s /q xyzzy, the whole tree (including the top level) will be removed.

Related

How to copy files inside a folder without other folders in batch?

I am having the following folder structure:
Folder1
-Folder2
-File1
-File2
-File3
I have tried the following in batch script to copy all inside the Folder1
echo d | xcopy "Folder1\*.*" "DestinationFolder"/f /s /y /r
But I need to copy only File1, File2, File3. Need to ignore Folder2. I don't have any idea about how to achieve this. Please help me resolve this.
If Folder2 is empty, /S will do,
but since you have /s in your command, so I guess it's not empty.
So use this:
echo \Folder2\>__tmp4Exclude__
echo d | xcopy "Folder1\*.*" "DestinationFolder" /f /s /y /r /EXCLUDE:__tmp4Exclude__
del __tmp4Exclude__
__tmp4Exclude__ is a temp file created to contain the list of files to exclude while copying.
From xcopy /?:
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
There is no need to use xcopy to copy just files! xcpoy is usually used to copy directory trees.
So, use copy instead. You can do:
copy "Folder1\*.*" "Destination\"
Probably you may need to use /(-)Y option:
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite an existing destination file.
From copy /? (copy help page)

How to Mass Delete Files in Windows using batch

I recently refreshed my PC and upon finally finishing, I realized a pesky file named thumbs.db had been left in every folder and directory. I'm trying to find a simple way to DELETE ALL thumbs.db from my ENTIRE C: drive.
Code I've tried (Only removed thumbs.db from desktop)
dir thumbs.db /a /b /s
del thumbs.db /a /s
PS: I'm open to any Power Shell Ideas Also.
You can achieve this by running:
C:
cd /
del /s /q /f /a:h Thumbs.db
The first line sets the drive you want to run it on.
The second line points to the root directory of the drive.
The third line force deletes (/f) hidden (/a:h) thumbs.db without prompting (/q) in all subfolders (/s)

How to delete all files and sub-folders in a folder except for 1 folder in a .bat script?

I have a folder that stores many log files and sub-folders. I would like to delete all files and sub-folders except 1 folder, named Excel_Export, which should not be deleted. I am using the following commands in my batch script:
move D:\ABC\Delete_Test\Retain_Folder D:\ABC
rd /s /q "D:\ABC\Delete_Test"
move D:\ABC\Retain_Folder D:\ABC\Delete_Test
However, after this script runs, even the 'Retain_Folder' is getting deleted except for the files inside it. What is it that I am doing wrong in the above command?
Also, is there a better way to do it?
NOTE:
All the other folders' names (that are to be deleted) starts with the '$' symbol.
This is untested - it deletes all normal files in d:\abc and then removes all folders beginning with $ in the same folder.
#echo off
del "d:\abc\*.*?"
for /d %%a in ("d:\abc\$*") do rd /s /q "%%a"
Your idea is probably the most efficient (fastest) way to accomplish your task, except you have one small bug. Your RD command removes the Delete_Test folder, so you must recreate it before you can move Retain_Folder back to where it belongs. You also probably want to redirect output to null when you move the folder - you don't need to see the move message.
move "D:\ABC\Delete_Test\Retain_Folder" "D:\ABC" >nul
rd /s /q "D:\ABC\Delete_Test"
md "D:\ABC\Delete_Test"
move "D:\ABC\Retain_Folder" "D:\ABC\Delete_Test" >nul
This strategy only works properly if you know that D:\ABC\Retain_Folder does not already exist before you start, or if it does exist, then it must be empty.

Error when delete files using bat

cd "D:\WWW\SEAP_P\Web\Sec.Seap.Application\prod\TSE\mailer_bin\sent"
echo y | del *.*
I want to delete all the files from sent directory,when i run the bat file.it delete all the file from my desktop.can i know what caused this?
first please choose your destination drive then set directory you want to delete. here is looks like
D:
cd "WWW\SEAP_P\Web\Sec.Seap.Application\prod\TSE\mailer_bin\sent"
echo y | del *.*
make sure D: without "
or
cd /d "D:\WWW\SEAP_P\Web\Sec.Seap.Application\prod\TSE\mailer_bin\sent"
echo y | del *.*
as suggested by #SomethingDark in comment (Thanks BTW),
Let me know if it works.

windows 7 batch file to delete and remove folders then copy entire folder (and subfolder)

It's been a long time since I've created any batch files.
What I'm trying to do is delete an entire folder, and then copy another folder (and it's sub folders and files) to the location where I deleted the original file (this is a backup process).
I've run into three problems:
the batch file prompts me when it runs the delete operation and this needs to happen "behind the scenes" so I can't be prompted.
the del "c:\my folder location\myfoldertodelete" only deletes the files within that folder
the copy "c:\my other folder location\myotherfolder" "c:\my folder location\" only copies the files within that folder, not the sub directories.
I assume I need xcopy or something but I've never used that. If anyone can help me jump through these three hoops, I'd greatly appreciate it.
edit: (updating with the copy command that isn't working
C:\Users\Chris>copy /s "C:\Users\Chris\Documents\Visual Studio 2010\Projects
\new project" "D:\VS 2010 projects\Projects\"
The syntax of the command is incorrect.
echo y | rd /s c:\directory.to.be.removed
copy /s c:\source c:\destination
echo y | rd /s C:\Users\user1\Documents\Dest
echo d | xcopy /s /y "C:\Users\user1\Documents\Src" "C:\Users\user1\Documents\Dest"
where:
"C:\Users\user1\Documents\Src" is the Source directory and
"C:\Users\user1\Documents\Dest" is the Destination directory.

Resources