?
Solved

Search & Delete a specific file in the whole Hard Drive ?

Posted on 2007-10-16
6
Medium Priority
?
567 Views
Last Modified: 2008-01-09
hi there,

i want to create a batch file that will search all of the drives (ex. c:\ d:\ e:\ ... etc) for a specific file and delete it. Most of the answers here only for one drive , i want it for the Complete HD ... and how to log the succesful deleted ones to a central point (network shared text file) ???

thankx
0
Comment
Question by:xmachine
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20085047
That's pretty simple -

1. What's the Operating System?
2. Are you interested in deleting the file for all drives (network drives) or merely all partitions on a particular hard disk?
3. If it's the latter, what would you have the script do if the computer has 2 or more hard drives?
0
 
LVL 15

Author Comment

by:xmachine
ID: 20085535
1) XP & 2003 ...

2) basically, all partitions and if we can include network drives ... this will be better ....

3) no, only one hard ...

 
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20085782
how bout this vbscript...


strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile")

For Each objFile in colFiles
    If InStr(1,objFile.FileName,"FileNameToSearch") > 0 Then
          WScript.Echo objFile.FileName & " found"
    End If
Next
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20085866
This will do as you asked.  Note I have placed a REM state in front of the 'del' command, you'll need to remove this in order for the deletion to actually hapen -

:: [SCRIPT]
@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/

if "%1"=="" (
      echo # ERROR - please supply a file name to search for and delete when found
      goto :END
) else (
      set FILE=%*
      set tFILE=!FILE:\=!
      set FILE=!tFILE::=!
      if not "!FILE!"=="%*" (
            echo # ERROR - supply only the file's name, not its path
            goto :END
      )
)

echo + Working ...

:: Determine available drives (fixed and network only)
fsutil fsinfo drives >"%TEMP%\%~n0.$$$"
if errorlevel 1 (
      echo # ERROR - unable to determine drive letters
      goto :END
)

for /f "tokens=1 delims=\" %%d in ('type "%TEMP%\%~n0.$$$" ^| find "\"') do (
      fsutil fsinfo drivetype %%d | findstr /i "Fixed Network" >nul
      if not errorlevel 1 (
            echo/
            echo   + searching for '%FILE%' on drive '%%d' ...
            call :SEARCH %%d
      )
)

echo/
echo - Done

goto :END

:SEARCH
pushd %*
for /f "tokens=*" %%f in ('dir "%FILE%" /s/a/b 2^>nul') do (
      echo     + deleting '%%f'
      rem del "%%f" /f
      if not errorlevel 1 (
            echo       - DELETED
      ) else (
            echo       # unable to delete file
      )
)
goto :EOF

:END
:: [/SCRIPT]
0
 
LVL 15

Author Comment

by:xmachine
ID: 20086630
MSE-dwells: thankx dude ... but it's not deleting anything inside C:   ?????
see ...

---------------------------------------------------------------

C:\Documents and Settings\3aed>"C:\Documents and Settings\3aed\Desktop\del.bat"
wako.xxx

+ Working ...

  + searching for 'wako.xxx' on drive 'C:' ...     <----------------- ?

  + searching for 'wako.xxx' on drive 'D:' ...
    + deleting 'D:\wako.xxx'
      - DELETED
    + deleting 'D:\amigoz\wako.xxx'
      - DELETED

  + searching for 'wako.xxx' on drive 'E:' ...
    + deleting 'E:\wako.xxx'
      - DELETED

  + searching for 'wako.xxx' on drive 'G:' ...
    + deleting 'G:\downloads\wako.xxx'
      - DELETED
    + deleting 'G:\FreeNac-VM\wako.xxx'
      - DELETED

- Done

----------------------------------------------
0
 
LVL 9

Accepted Solution

by:
MSE-dwells earned 500 total points
ID: 20086837
Hehe ... nod, I see why.  Since you ran the script from the C drive, the current directory acted as the base of the search on that drive.  To correct this oversight, search for -

pushd %*

... on a line by itself and replace with -

pushd %*\

... that should resolve it.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question