Delete folders with wild cards from a batch file Windows 7 - batch-file

I know we can write programs to do it.
I know we can write other scripts (perl/vbscript/ etc.) to do it.
I am looking for a command prompt/batch file solution to delete all folders matching sub_* (like sub_1, sub_2 ... ) to be deleted. rmdir or rd doesn't support wildcards, and I'm not able to figure out how to pipe the output of dir sub_*/ad command to delete command one by one as well. Any loop syntax etc. I can use?

for /d %x in (sub_*) do rd /s /q "%x"
You need to double the % if used in a batch file:
for /d %%x in (sub_*) do rd /s /q "%%x"
Untested, make sure to first use echo or something else that doesn't immediately wipe the directories ;)

forfiles /P C:\where\my\dirs\at /M sub_* /C "cmd /c if #isdir==TRUE rmdir /s /q #file"

Related

How to remotely delete subfolders older than x days using windows command prompt?

How can I remotely delete subfolders older than x days? Was able to find a syntax like this, but it deletes all of the subfolders in xFOLDER. Any idea how I can modify the syntax and delete only subfolders in xFOLDER older than 7 days?
FOR /D %i IN (\\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\*) DO RD /S /Q "%i"
DEL /Q \\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\*.*
I manage to delete directory and subdirectory using this command
RD remove directory
/S use to remove directory tree
/Q quiet mode
forfiles /D -7 /C "cmd /c rd /s /q #path"
change the code accordingly to your needs.

Deletion Batch file but receive cannot find desktop.ini

I've created a simple batch file to delete files over 14 days which is a simple command as most of you probably know so it's doing the below
forfiles /p "C:\%userprofile%\Downloads" /s /m *.* /c "cmd /c Del #path" /d -14
but I keep receiving could not find C:\User\%userprofile%\downloads\desktop.ini
So I assume it's searching for the desktop.ini file but I have all folders and files unhidden. Is there a way to prevent it looking for that file and just doing as a I ask it?
Any help would be appreciated.
As Mike Nakis suggests, del is probably failing on desktop.ini because that file is typically set +ash (archive, system, and hidden). The easiest solution would be just to ignore it. It's harmless anyway.
forfiles /p "%userprofile%\Downloads" /s /m *.* /c "cmd /c Del #path 2>NUL" /d -14
If it really bothers you and you insist on deleting it, then remove the system attribute.
attrib -r -s -h -a "%userprofile%\Downloads\*"
forfiles /p "%userprofile%\Downloads" /s /m *.* /c "cmd /c del #path" /d -14
... but it'll probably just get re-created eventually anyway. I'd just ignore it.
What do you mean when you say that you have all folders and files unhidden? You have probably instructed the windows explorer to also show hidden files, but that does not mean that the files are not hidden.
You have two options: for each file that you are about to delete, either use the attrib command to make sure it is not hidden prior to deleting it, or play with the /A option of the del command to make it delete everything, even hidden files.

Nest if exist statement within for statement in batch file

Good morning guys. I have a batch file that searches network PC user folders for XML files, then copies those files to a network location. However, it copies folders for all users, regardless of whether they have the files in question. What I would like to do is only make a folder for that user if the directory in which those files are contained exists.
Here is what the code looks like:
for /d %%g in ("\\computer.domain\c$\users\*") do (
xcopy /d /q /c /i /y "%%g\AppData\Roaming\Program\Folder\*.xml" "\\server.domain\shared_folder\%%g"
)
It works perfectly but for creating a user folder for every user. What I would like to do is only create a folder for users where Appdata\Roaming\Program\Folder contains XML files in the first place. I can't figure out how to get an if exists statement to work within that for statement though.
Also, this creates a directory structure like computer.domain/c$/users/user. If there is a way to strip out the c$/users (so it's computer.domain/user/file.xml), that would be even more amazing.
Thanks in advance!!
(Update) Here is the full code the batch is using. It repeats for each computer:
net use k: \\computer.domain\c$\users
for /d %%g in ("k:\*") do (
forfiles /P "%%g\AppData\Roaming\Program\Folder" /m *.xml /D -7 /C "cmd /C del #path"
)
net use k: /delete
for /d %%g in ("computer.domain\c$\users\*") do (if exist "%%g\Appdata\Roaming\Program\Folder\*.xml" (
xcopy /d /q /c /y "%%g\AppData\Roaming\Program\Folder\*.xml" "\\server.domain\shared_folder\%%~nxg\")
)
forfiles /P /S "M:\MAS500\endicia-postback-archive" /m *.xml /D -60 /C "cmd /C del #path"
The entire functionality is to delete XML files a week or older, then copy the remaining files, then delete files from the server that are two months or older. That line is only at the end, but the other lines are repeated for each computer.
Perhaps there is a better way to go about it? When I run it this way, it seems like I don't get a folder for all users that meet the criteria.
Thanks again!
Test this after altering the paths:
Just commenting here that your use of the /d switch will limit the files being copied.
#echo off
for /d %%g in ("\\computer.domain\c$\users\*") do (
if exist "%%g\AppData\Roaming\Program\Folder\*.xml" (
xcopy /d /q /c /y "%%g\AppData\Roaming\Program\Folder\*.xml" "\\server.domain\shared_folder\computer.domain\%%~nxg\"
)
)

Batch that deletes multiple directories/folders containing spaces

I am trying to write a batch that deletes multiple directories/folders in a directory
I am using
for /D %f in (*) do rmdir %f /s /q
Now this works and deletes folder names that dont contain a space.
I recieve this message after running the above command for the folders that do.
d:\test\testworld\rmdir remove me /s /q
The system cannot find the file specified.
The system cannot find the file specified.
Most of the folders contain spaces. Is there anyway i could change the command to include folders with spaces?
use quotes.
rmdir remove me /s /q
fails, but
rmdir "remove me" /s /q
works.
The change to your command line would be for /D %f in (*) do rmdir "%f" /s /q
You can use " double quotation mark like rmdir "remove me" /s /q

How to delete *.* excluding some extensions?

I'm trying to make a batch file on Windows for deleting all the files in the current directory but excluding 4 file extensions (log, sdb, SDK, bat).
I have tried the Forfiles command on Windows but this delete everything on my current folder (even the bat file). My command is:
#ECHO OFF
FORFILES /M *.* /C "cmd /c IF NOT #ext=="sdb" (IF NOT #ext=="sbk" (IF NOT #ext=="log" (IF NOT #ext=="bat" DEL #FILE)))" /Q
How can I make it work?
internal quotes must be escaped with \
you probably want IF /I (case insensitive) option
you should use #ISDIR to exclude directories
DEL /Q option was after last quote, should be before last quote, but it isn't needed
parentheses are not needed
FORFILES /M option isn't needed since your mask is "all files"
This should work
#echo off
forfiles /c "cmd /c if #isdir equ FALSE if /i not #ext==\"sdb\" if /i not #ext==\"sbk\" if /i not #ext==\"log\" if /i not #ext==\"bat\" del #file"
But the above is very slow, and it sure is a lot to type.
The following is much simpler and faster.
#echo off
for /f "delims=" %%F in ('dir /b /a-d ^| findstr /vile ".sdb .sbk .log .bat"') do del "%%F"
If ROBOCOPY is available to you:
#ECHO OFF
MKDIR temporary_pit
ROBOCOPY . temporary_pit /XF *.sdb *.sbk *.log *.bat /MOV >NUL
RMDIR /S /Q temporary_pit
That is, you are creating a temporary subdirectory, moving the files that are to be deleted to it (which is fast because, as the destination directory is on the same drive, only file names are relocated, not the files' contents), then deleting the subdirectory.
Also, you can do something like this:
#echo off
attrib -r -s *.*
attrib +r +s *.sdb
attrib +r +s *.sbk
attrib +r +s *.log
attrib +r +s *.bat
del *.* /S /Q
attrib -r -s *.sdb
attrib -r -s *.sbk
attrib -r -s *.log
attrib -r -s *.bat
-- Mario
#echo off
setlocal EnableDelayedExpansion
set exclude=.log.sdb.sdk.bat.
for %%f in (*.*) do (
if /I "%exclude%" == "!exclude:%%~Xf.=!" del "%%f"
)
forfiles /s /c "cmd /c (if NOT #ext==\"dqy\" del /s /q #path)" /D -14
This is about as simple as I could get this script. They wanted to keep the macro files (.dqy) but recursively delete everything else older than 14 days.
Runs in the current directory (be careful when testing).
I ran across this topic searching for a way to delete hundres of files created by a virus.
Non of the solutions really worked for me, so I figured out how to do it from a command line. I needed only to keep 2 extensions (mail archive).
This did the trick:
for /R %f in (*) do if not %~xf==.ex1 if not %~xf==*.ex2 del "%f"
I use the /R to work recursive: look in all subfolders.
The %~xf looks at the extension only (for some reason it didn't work without it).
I use the quotes "%f" at the delete command to cover the windows long names with spaces (especially in folder names).
Also for some reason, adding spaces before and behing the == gave errors.

Resources