Dos command to delete specific directories - dos

cd to myFolder
I want to delete from myFolder all folders starting with tmp
how could I do it in dos command ?

rmtemp.bat:
FOR /F "tokens=*" %%G IN ('dir /B /O /AD temp*') DO rmdir %%G
works only if stored in a .bat but not if entered manually in cmd line.

Related

Windows Bat - The System Cannot Find the File Specified

I am using the following code in a .bat to cleanup a directory. It is to delete any directory with a time stamp older than 14 days. The thing is, this script works and deletes the appropriate directories. However it returns the error:
ERROR: The system cannot find the file specified I am unable to decipher the cause of this, and would like to get to the bottom of it.
FORFILES /S /D -14 /p %cd% /M "*" /C "cmd /c IF #isdir == TRUE rmdir #path /s /q"
As to the follow up question you asked:
Using a Windows batch file, find directories that do not contain any letters in their name. They can contain special characters and spaces. Delete the directories and their sub-folders w/o confirmation.
Put 1.bat in the directory you want to cleanup. Open a cmd window and run 1.bat.
Find all directories that do not contain any letters in their name and output their names to 1.txt. They can contain special characters and spaces.
Echo the directories to be removed. Do not remove them.
Remove comment tag to remove directories and sub-directories w/o confirmation.
1.bat
for /f "usebackq delims=|" %%a in ('DIR /b /ad ^| findstr /v /r "[a-Z]"') do echo "%cd%\%%a" will be removed without confirmation.
:: for /f "usebackq delims=|" %%a in ('DIR /b /ad ^| findstr /v /r "[a-Z]"') do rd /s /q "%cd%\%%a"

How do I find a Specific File Name in Batch and change to that directory?

I am coding a batch file that will get the directory of a specific file(curl_for_64bit.exe). I tried using the find command but it does not work. It basically gets the directory of the file, changes to that directory so that it can be copied.
you can traverse the directory tree with a FOR command checking for the existence of the required file until it's found
for /r /d %%a in (*) do (
if exist %%a\curl_for_64bit.exe (
pushd %%a
goto :eof
)
)
you can use command like #Ken White say, then use other code to get the file's directory.
here is my code
rem you should go to the specific root directory(like c d e etc.)
cd /d c:\
dir /s /a /b curl_for_64bit.exe >tmp.txt
set /P file_path=<tmp.txt
del tmp.txt
cd /d %file_path%\..
And if you just want to copy those files, why you want to go to the directory of file?
The following script searches the file curl_for_64bit.exe in the directory tree rooted at C:\ROOT\ and changes to the parent directory where the found file is actually located:
for /F "delims=" %%F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do (
cd /D "%%~dpF"
)
Or this command line to be typed directly into cmd:
for /F "delims=" %F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do #cd /D "%~dpF"
To learn what the ~dp modifier of the for variable %%F means and how it works, open a new command prompt window, type for /? and read the help text (see the last section in particular).

Windows Cmd line delete command to delete all file except one

I am looking for a single windows command where I need to delete all the files in a folder except one.
eg: Files listed under directory:
c:\users\admin\folder
abc
abcde.zip
abcdef
123.txt
Now, I want to delete all the files except "abcde.zip"
corresponding Linux command would be: rm -rf !(abcde.zip)
Is there anything like this on windows without using batch script, just a single line command?
Thanks in Advance!
Since Win's command interpreter doesn't have a rm -rf like command (that works on both files and dirs) we just need to modify the command that I placed in my 1st comment, actually split it in 2 commands (still in a single line: well, if the console is really really wide :) ), and execute them in a sequence (&):
one that takes care of the files (the /a:-d argument for dir), and launches del
one that takes care of the folders(the /a:d argument) and launches rmdir
Here's what it looks like (note that it does the job for the current directory):
(for /f "tokens=*" %f in ('dir /a:-d /b 2^>nul ^| findstr /v /b /e /c:"abcde.zip"') do (del /f "%f")) & (for /f "tokens=*" %f in ('dir /a:d /b ^| findstr /v /b /e /c:"abcde.zip"') do (rmdir /q /s "%f"))

Windows batch command to delete folders only

I have a folder, which has files and folders inside it like
C:/MyFolder
C:/MyFolder/File1.txt
C:/MyFolder/File2.txt
C:/MyFolder/File3.sql
C:/MyFolder/Folder1
C:/MyFolder/Folder1/File5.txt
What batch command do I need to use to delete all the folders and contents inside them without deleting files inside my folder. Example : Delete Folder1,Folder1/File5.txt but retain File1.txt,File2.txt and File3.sql?
This shows you the commands - if you are happy with them then remove the echo keyword and run it again.
#echo off
for /d %%a in ("C:\MyFolder\*") do echo rd "%%a" /q /s
pause
from command prompt:
for /f "tokens=* delims=" %a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%~fa"
from batch file:
for /f "tokens=* delims=" %%a in ('dir /b /a:d "C:\someDir"') do #rd /s /q "%%~fa"
For DOS/Command prompt use
for /d %F in ("path*") do rmdir /s /q "%F"
Use double % if you use it in a batch file.
for /d %%F in ("H:\EDGE-backup*") do rmdir /s /q "%%F"
I used this to backup the EDGE bookmarks and such, and since XCOPY always brings with it it's root directory subfolders i had to delete these after the copy.
The above worked for this. Result,only files remained in H:\EDGE-backup.

Find and delete desktop.ini files in every folder on a drive using a batch script

I want to find and delete every desktop.ini and Recycle Bin.BIN file on a network drive, H:, using a windows batch file. I currently have this:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
Which works but for any file in a sub-folder with a space in the name it fails with a "cannot find file" error. Any suggestions how to fix that?
solution that worked for me was
create bat file "delete_all_desktop_ini.bat"
with
del /s /q /f /a ".\desktop.ini"
put it in a folder and run it
it will delete all desktop inis in current directory and sub directories of this file.
i put it in a project folder that is in google drive
Give this a test:
I've altered the recycle bin name to what I see here in Windows 8.
The name changes with different versions of Windows.
#echo off
del /s /q /f /a "h:\desktop.ini"
del /s /q /f /a "h:\$Recycle.Bin\*.*"
The problem occurs because by default space is a delimiter for the for command, but you can change this using the delims option. If you pick a character that won't ever appear in a file path then it should work fine:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"
Try it, to make it working remove echo from the script.
del /s /q /f /a ".\desktop.ini"
it should works as charm
save file .bat
put it in any folder
it will delete ini files in folders and sub folders

Resources