Link to home
Start Free TrialLog in
Avatar of finnstone
finnstone

asked on

DELETE IMAGES IN A FOLDER ACCORDING TO A LIST OF FILE NAMES

i have a list in col A of excel of files names. the file names are purely numbers, so maybe that makes it easier.

need to look in windows folder for those file names and delete files if they match the list.
Avatar of Bill Prew
Bill Prew

If this is a one time effort then a simple approach would just be to build a bunch of DEL statements in column B and then copy those into Notepad and save as a BAT file, and run it.  You could use a formula like below in column B to build the DEL statements, adjust path as needed and add extension if needed.

=CONCATENATE("del ""c:\temp\", A1,"""")


»bp
Do the cells contain just the name or do they include the file extension e.g .jpg?
Avatar of finnstone

ASKER

Hi Bill, sounds like the answer I need, but can you explain more?
Another approach would be to save the list from column A to a text file, let's say list.txt.  Then use a small BAT file as shown below to read that text file and delete the files specified from the specified base folder.  Save below as a .BAT file, adjust the BaseDir variable, place the list.txt file in the same folder, open a DOS command prompt and execute the named .BAT file.

@echo off
setlocal

set BaseDir=c:\temp
set ListFile=list.txt

for /f "usebackq tokens=*" %%A in ("%ListFile%") do (
    if exist "%BaseDir%\%%~A" del "%BaseDir%\%%~A"
)

Open in new window


»bp
Are they all in the same folder?
yes norie
Hi Bill, sounds like the answer I need, but can you explain more?

  1. Place that formula into B1 adjust the path from c:\temp to be your folder.
  2. Copy the formula down column B as far as the file names go in column A.
  3. Select all of column B and copy it.
  4. Open Notepad (or other text editor) and past the column B DEL statements there.
  5. Save as a .BAT file with a non common name, let's say EE29106674.BAT.
  6. Open Command Prompt from Start menu.
  7. Do a CD command to the folder where you saved EE29106674.BAT.
  8. At the prompt type EE29106674.BAT and press return.
  9. It should run each DEL statement and delete the file if it exists.


»bp
If your files have a common extension like .JPG then adjust the formula as:

=CONCATENATE("del ""c:\temp\", A1,".jpg""")


»bp
for this approach, do i need to specify extension in list.txt?

Another approach would be to save the list from column A to a text file, let's say list.txt.  Then use a small BAT file as shown below to read that text file and delete the files specified from the specified base folder.  Save below as a .BAT file, adjust the BaseDir variable, place the list.txt file in the same folder, open a DOS command prompt and execute the named .BAT file.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what is command in msdos to execute bat file?
it worked. wow!
awesome
Great, glad that was useful.


»bp