Page 1 of 1

Deleting files named "._*"

Posted: 01 Dec 2017 14:08
by batchcc
Hello everyone! I am trying to delete all files in a certain directory and its sub-directories that has the name "._*" but this is the code I have but it is giving me an error.

Code: Select all

@echo off

setlocal

set count=0
set pattern=._*

for /f %%i in ('dir /b /a-d %pattern% ^| find /c /v ""') do @call set count=%%i

del /s /q /f ._*.txt
echo  %count% files deleted

endlocal

Error Message:

Code: Select all

Could Not Find H:\directory\._*
This batch file is located in "H:\directory" and some example file names include:

Code: Select all

._random.txt
._Day 1.docx
._Screen Shot 2017-05-26 at 2.16.41 PM.png
._Workbook1.xlsx
._Zoomin.mov
._Zoomout.mov

Re: Deleting files named "._*"

Posted: 01 Dec 2017 14:24
by aGerman
Since you're already trying to count the files you can also use the loop to delete them.

Code: Select all

for /f "delims=" %%i in ('dir /b /a-d /s ^| findstr /rec:"\\\._[^\\]*"') do (del /f "%%~i" &&set /a "count+=1")
Steffen

Re: Deleting files named "._*"

Posted: 01 Dec 2017 15:54
by Squashman
You sure the files have a txt extension.

I had no problems using your code.

Code: Select all

H:\period>dir /b
._File1
._File2.txt

H:\period>del ._*.txt

H:\period>dir /b
._File1

H:\period>echo blah>._File2.txt

H:\period>dir /b
._File1
._File2.txt

H:\period>del ._*

H:\period>dir /b

H:\period>

Re: Deleting files named "._*"

Posted: 02 Dec 2017 09:56
by batchcc
aGerman wrote:
01 Dec 2017 14:24
Since you're already trying to count the files you can also use the loop to delete them.

Code: Select all

for /f "delims=" %%i in ('dir /b /a-d /s ^| findstr /rec:"\\\._[^\\]*"') do (del /f "%%~i" &&set /a "count+=1")
Steffen
The code is giving me this error and not deleting anything
Could Not Find H:\testing\._random.txt
Could Not Find H:\testing\._Day 1.docx
Could Not Find H:\testing\._Screen Shot 2017-05-26 at 2.16.41 PM.png

Re: Deleting files named "._*"

Posted: 02 Dec 2017 10:14
by aGerman
Are you trying to delete hidden files? Update the DEL command

Code: Select all

del /a /f "%%~i"
Steffen

Re: Deleting files named "._*"

Posted: 03 Dec 2017 17:06
by batchcc
Thanks aGerman this works great!