Jump to content
Tuts 4 You

DOS Batch Command


Recommended Posts

Hi all,

Does anyone know what the syntax would be in a dos bat file to do the following.

On any pc, the bat must run silently and do the following on ALL available drives.

Find all instances of a file called thumbs.db - then delete it.

What is the bat command to get it to search all drives.. and all directories??

Cheers..

Bax! ;)

Link to comment

Use /S as a parameter i.e.

To find where they are:

dir /S thumbs.db

To delete them

del /S thumbs.db

Dunno about the 'all drives' thing off the top of my head though......

Link to comment
Dunno about the 'all drives' thing off the top of my head though......

Thx br0.. but thats the only thing i need to know! ;)

Here's a test one people can try... it says it works.. but it doesnt delete anything??

:: --- BATCH SCRIPT START ---

:bof

@echo off & setlocal enableextensions enabledelayedexpansion

:init

:: Configure the file name supplied

if "%1"=="" (

echo Please specify the name of the file to search and delete

goto :eof

) else (

set file=%*

set ffile=!file:\=!

set file=!ffile::=!

if not "!file!"=="%*" (

echo supply file's name only, not the path

goto :eof

)

)

:: Determine available fixed drives

set drives=

if exist "Drives.txt" del /F /S "Drives.txt"

for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (

if /i "%%a" NEQ "Drives:" (

set "drives=!drives! %%a"

echo:%%a >> Drives.txt

) ELSE (

set "drives=!drives! %%b"

echo:%%b >> Drives.txt

)

)

for /f "tokens=*" %%d in ('type "Drives.txt"') do (

fsutil fsinfo drivetype %%d | find /i "Fixed" >nul

if not errorlevel 1 (

echo searching for "%file%" on drive "%%d" ...

call :SEARCHIT %%d

)

)

:: Search and delete the file if found

:SEARCHIT

pushd %*\

for /f "tokens=*" %%f in ('dir "%file%" /b /s /a-d 2^>nul') do (

echo del "%%f" /f

if not errorlevel 1 (

echo "%%f" deleted

) else (

echo unable to delete "%%f"

)

)

goto :eof

:eof

:: --- BATCH SCRIPT END ---

This searches all drives for a file called drives.txt.... it says it has found and deleted the files... it lies... anyone got any ideas??

command to get it to run is... drives.bat drives.txt

.

Edited by Baxter
Link to comment

Not tested but are thumbs.db files system files or something?

Might have to remove readonly/system settings:

attrib -r -h -s thumbs.db

Not sure how you integrate that in the recursive search but sure you'll find a way ;)

Link to comment
Peter Ferrie

Thumbs.db has the system attrib set, and attrib does not work recursively.

You can make a list of the files to delete, but then you need something to parse the list and delete the files.

A script can do that easily, but I think that a batch file cannot.

Link to comment

Conquered!! :D

It was missing one line...

Thx and respect to Chip4brains at Zorboard.

Here is the script that does the job - perfectly!

:: --- BATCH SCRIPT START ---

:bof

@echo off & setlocal enableextensions enabledelayedexpansion

:init

:: Configure the file name supplied

if "%1"=="" (

echo Please specify the name of the file to search and delete

goto :eof

) else (

set file=%*

set ffile=!file:\=!

set file=!ffile::=!

if not "!file!"=="%*" (

echo supply file's name only, not the path

goto :eof

)

)

:: Determine available fixed drives

set drives=

if exist "thumbs.db" del /F /S "thumbs.db"

for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (

if /i "%%a" NEQ "Drives:" (

set "drives=!drives! %%a"

echo:%%a >> thumbs.db

) ELSE (

set "drives=!drives! %%b"

echo:%%b >> thumbs.db

)

)

for /f "tokens=*" %%d in ('type "thumbs.db"') do (

fsutil fsinfo drivetype %%d | find /i "Fixed" >nul

if not errorlevel 1 (

echo searching for "%file%" on drive "%%d" ...

call :SEARCHIT %%d

)

)

:: Search and delete the file if found

:SEARCHIT

pushd %*\

for /f "tokens=*" %%f in ('dir "%file%" /b /s /a-d 2^>nul') do (

echo del "%%f" /f

del "%%f" /f

if not errorlevel 1 (

echo "%%f" deleted

) else (

echo unable to delete "%%f"

)

)

goto :eof

:eof

:: --- BATCH SCRIPT END ---

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...