Computing.Net > Forums > Programming > batch file to delete images

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file to delete images

Reply to Message Icon

Name: lotus5100
Date: September 7, 2008 at 21:19:38 Pacific
OS: Windows Home XP SP2
CPU/Ram: 1.00
Product: Compaq Presario
Comment:

Hi, I'm working on an online project which involves over 50,000 images all identified by unique numbers. When adding updates, I run a query in my database to match the old with the new. I know very little about writing batch files and need help in writing a batch file that will delete images not matched after I run my query.

Following, I need help in writing a batch file that copies images remaining in my folders to my specified folder structure (i.e. if the file remaining is 123456.jpg, I would like to copy that file to folder 456/ so the path would read 456/123456.jpg).

I would appreciate anyone's help.

Thanks in advance!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 8, 2008 at 00:51:05 Pacific
Reply:

"delete images not matched"

Not clear what you mean.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: lotus5100
Date: September 8, 2008 at 07:19:41 Pacific
Reply:

I will try to explain this better. Let's say for instance that I am running a bookstore and in the bookstore, I have images representing the books and they're all named by the isbn number (i.e. 1234567890.jpg). Each week I receive an updated version of (i.e. 1234567890.jpg). After importing the updated list of images to Microsoft Access, I run a query that will match the previous (i.e. 123456789.jpg) with the new (i.e. 1234567890.jpg), how can I create a batch file that will delete all of the images in the uploaded directory that were not matched?

And for the next question, let's say that I download 4 images and each of the images are named 1234567890.jpg, 1234000890.jpg, 1234567891.jpg, 1234000891.jpg - how do I write a batch file so that these images are placed in folders by the last 3 characters of the filename, for instance, placing file 1234567890.jpg and 1234000890.jpg into folder 890/?

I've been racking my brain trying to figure this out. I appreciate all help. Thanks.


0

Response Number 3
Name: Mechanix2Go
Date: September 8, 2008 at 10:20:44 Pacific
Reply:

"I run a query that will match"

I don't know what that means. But maybe it boils down to 'delete OLD files if they exist in the NEW folder'.

So if you have a \NEW folder and a \OLD folder, where the \NEW contains:

1.jpg
4.jpg

and the \OLD contains:
1.jpg
2.jpg

You ant to delete the .jpg and keep the 2.jpg in the \OLD folder.

Is this getting close?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: lotus5100
Date: September 8, 2008 at 10:47:30 Pacific
Reply:

Ok, here's another example... by the way, thanks for trying to help me..

Example: I have 10 files in a folder and their names are 1234000890.jpg, 1234001890.jpg, 1234002890.jpg, 1234003890.jpg, 1234004890.jpg,1234005890.jpg, 1234000891.jpg, 1234002891.jpg, 1234003891.jpg, 1234004891.jpg. I only want to keep the first 5 files, how do I delete the remaining files that I do not want using a batch file?

And the next question right after that is, supposing I didn't delete any of these files, how would I organize these files so that all files with common last 3 characters (i.e. 890.jpg or 891.jpg) would be sent to folders 890/ and 891/?

Thanks for your patience.


0

Response Number 5
Name: Mechanix2Go
Date: September 8, 2008 at 17:59:25 Pacific
Reply:

"I only want to keep the first 5 files"

Depends on how you define first. If you want to keep the five newest [seems reasonable] try this:

::=============================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* skip=5 delims= " %%a in ('dir/b/o-d *.jpg') do (
del %%a
)
::===================================

For the moving of files with same last three nums:

::===================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (dir/b *.jpg) do (
set FN=%%a
set FN=!FN:~7,3!
echo if not exist !FN! md !FN!
echo move %%a !FN!
::==============================

As written it does DO the move. To activate, edit out the ECHOs preceeding IF and MOVE.


=====================================
If at first you don't succeed, you're about average.

M2


0

Related Posts

See More



Response Number 6
Name: lotus5100
Date: September 8, 2008 at 18:36:52 Pacific
Reply:

Let me just say... thank you for taking the time to help me! I am such a newbie at programming so I have the following questions:

I copied and pasted the code below into notepad and saved as a .bat file into the same folder where the images were located. I double clicked on the .bat file and nothing happened. I took the words "echo" off. Do I include the path location of the folder in the code? and if so, where?

For the moving of files with same last three nums:

::===================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (dir/b *.jpg) do (
set FN=%%a
set FN=!FN:~7,3!
echo if not exist !FN! md !FN!
echo move %%a !FN!
::==============================

Also, in relation to deleting selected files from a folder. Instead of deleting the oldest files, how would I be able to delete just a short list of files that I no longer want without having to search and find one by one? For instance, deleting 1234567890.jpg, 1234567895.jpg, and 1234567899.jpg without deleting all files in that same folder?


0

Response Number 7
Name: Mechanix2Go
Date: September 8, 2008 at 18:53:12 Pacific
Reply:

This is how it should look. The 'end paren' [)] on the last line [all by it's lonesome] is critical.

::===================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (dir/b *.jpg) do (
set FN=%%a
set FN=!FN:~7,3!
if not exist !FN! md !FN!
move %%a !FN!
)
::==============================


##################################
To delete multiple files using a list, get the names into a txt file and try this:

::======================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (oldnames.txt) do (
del %%a
)
::=====================================


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 8
Name: lotus5100
Date: September 8, 2008 at 19:41:31 Pacific
Reply:

thank you! You rock!


0

Response Number 9
Name: lunar2150
Date: September 26, 2008 at 23:59:59 Pacific
Reply:

Hi,

Here's a follow-up question in relation to the same topic, what if I wanted to move the files to folders located on my server? I use Cute Ftp when transferring files from my pc to my server.. so if I wanted to move files: 9787897899900.jpg and 9787897778765.jpg to their respected folders 900/ and 765/ on my server without overwriting the existing folders, how would I alter the batch code previously written above?

I appreciate all help. Thanks in advance.


0

Response Number 10
Name: Mechanix2Go
Date: September 27, 2008 at 09:57:41 Pacific
Reply:

"respected folders 900/ and 765/ on my server without overwriting the existing folders"

Not clear how sending files would overwrite folders.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 11
Name: lunar2150
Date: September 27, 2008 at 10:09:49 Pacific
Reply:

Usually in the past, when I transfer a folder that is already existing on the server, it will ask me if I would like to overwrite the existing folder when all I wanted to do was just add new files into each respected folder.


0

Response Number 12
Name: Mechanix2Go
Date: September 27, 2008 at 11:45:59 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

> #.ftp echo o mysite.com
>> #.ftp echo username
>> #.ftp echo password
>> #.ftp echo bin
>> #.ftp echo cd /900
>> #.ftp echo put 9787897899900.jpg
>> #.ftp echo bye

ftp -s:#.ftp


=====================================
If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file to delete images

Batch File To Delete Line www.computing.net/answers/programming/batch-file-to-delete-line/15539.html

Batch file to delete files in a dir www.computing.net/answers/programming/batch-file-to-delete-files-in-a-dir/14599.html

batch file to delete www.computing.net/answers/programming/batch-file-to-delete/15614.html