Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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!

"delete images not matched"
Not clear what you mean.
=====================================
If at first you don't succeed, you're about average.M2

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.

"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.jpgand the \OLD contains:
1.jpg
2.jpgYou 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

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.

"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 EnableDelayedExpansionfor /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 EnableDelayedExpansionfor /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

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 EnableDelayedExpansionfor /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?

This is how it should look. The 'end paren' [)] on the last line [all by it's lonesome] is critical.
::===================================
@echo off
setLocal EnableDelayedExpansionfor /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 EnableDelayedExpansionfor /f "tokens=* delims= " %%a in (oldnames.txt) do (
del %%a
)
::=====================================
=====================================
If at first you don't succeed, you're about average.M2

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.

"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

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.

@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 byeftp -s:#.ftp
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

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