Delete file and move to next one

October 31, 2020 at 20:12:47
Specs: Windows 7
Someone has kindly written this batch file which does the following:
Deletes 2 files at the same time, with the same name but different extension.
For example, there are the files "example.jpg" and "example.pdf", and if the Image file is deleted, then the PDF file will also be deleted automatically.

It's written specifically for use with an image viewer called Irfanview. The batch script is saved anywhere and then called up in Irfanview by going to: Options, Miscellaneous, and setting as an External editor by typing in the path, such as.
C:\Users\PC\Desktop\Stuff\TestBatchFile.bat
To delete the user goes to: File, Open with external editor.
This deletes bot the Image file and PDF file, so it works.

I was looking to add another functionality:
When the file set is deleted, it would move to the next one automatically.
In Irfanview, when a file is deleted normally using the "Delete" key, you can go to the next file via: Options, Properties/Settings, File Handling, check the box "Jump to the next file after moving or deleting".

Could this be done using coding?
Or is there a reference which may help me to do this?


@echo off
rem Delete the picture itself (the full file path passed in directly from IrfanView)
del %1
rem Delete the PDF file (extract the drive letter, path and base file name and add the extension)
del "%~d1%~p1%~n1.pdf"

message edited by batch_y


See More: Delete file and move to next one


#1
October 31, 2020 at 21:09:50
I don't know enough to be able to answer your question,
but I can ask some questions that might help.

What is "the next file"? Is it the next file in the directory?
The next file alphabetically? The next file in the current
ordering? (Alphabetical, reverse alphabetical, date,
reverse date, size, reverse size)

Can the next file be any filetype? Must it be the same
filetype as the first deleted file? Can it be any file that
IrfanView is able to read? Or just a specific filetype, such
as .jpg or .png?

What happens when the program finds the next file?
Does it go ahead and delete the file, or ask if you want
to delete it?

Does it keep on going and delete all the files in the directory?
When does it stop?

-- Jeff, in Minneapolis

message edited by Jeff Root


Reply ↓  Report •

#2
October 31, 2020 at 21:55:03
Thanks for assisting. The responses are as follows.

- "The next file" is basically the next file that would be viewed. Typically the view is sorted alphabetically. So "example2" would be viewed after "example1". I did not think about other sorting methods but sorting in typical alphabetical order would be OK. I'm guessing adding this as a variable would over-complicate things but I could be wrong.

- The next file type can be any file type. In Irfanview there is a setting which allows you to scroll through the extensions you choose. I have input this in Properties/Settings, Extensions, Load custom file types:
JPG|JPEG|PNG|TIF|TIFF|

- When the program finds the next file, nothing happens, you just view it. It tells me that the previous file was deleted by the batch script and I'm now looking at the next file.
If I'm not on the next file, I get the feeling that the deletion did not occur, this is the main reason for the query.
At the moment, when the deletion occurs, you still see the image, as if nothing happened, I should have explained this better.

- It doesn't delete all the files, just the [image] file in view and the partner PDF file with the same name but differet extension.
And then supposed to view the next file

Cheers.

message edited by batch_y


Reply ↓  Report •

#3
November 1, 2020 at 07:46:11
batch_y wrote:

> - When the program finds the next file, nothing happens, you just view it.

Oh, of course! I didn't get that. Makes perfect sense. And getting
feedback as to whether the file has been deleted makes sense, too!
I've used IrfanView for decades, but have almost never used it to
delete files, so I don't remember what feedback it gives when that
big X is clicked. Back when I wrote programs myself, giving the user
good feedback was my very highest priority. More important to me
than getting the job done on time, or even getting the right answer!

Unfortunately I don't know enough about batch programming to
help further. Somebody else here should be able to do this, easy!

-- Jeff, in Minneapolis


Reply ↓  Report •

Related Solutions

#4
November 1, 2020 at 18:28:53
That would be great then.
Thanks for your help.

Reply ↓  Report •

#5
November 1, 2020 at 21:34:31
Here's a feeble attempt at prototype:
@echo off & setlocal
set types=*.jpg *.jpeg *.png *.tif *.tiff
pushd %~dpn1
rem echo %cd%
rem pause
for /f "tokens=*" %%a in ('dir /b /on %types%') do call :xx "%%a"&&echo "%%a" deleted||echo "%%a" not deleted
exit /b

:xx
:: view?
start "" "%~dpnx1"
set kill=
set err=1
set /p kill="d to delete %1: "
if /i "%kill%" neq "d" goto :xit
echo testing:  del "%~1"
echo testing:  del "%~n1.pdf"
set err=0
:xit
pause
exit /b

message edited by nbrane


Reply ↓  Report •

#6
November 1, 2020 at 21:44:28
Thanks for having a go at this nbrane.
What happened is this:
- It didn't delete the file.
- It opened the first file in the folder as well. So now there are 2 files open.
- There is a Command Prompt window that says "The system cannot find the filepath specified. d to delete FILE.jpg".

Also I should mention that not all image files will have a partner PDF (that is, the same name but different extension). I tested the code with and without a partner PDF file, the result is the same.


Reply ↓  Report •

#7
November 2, 2020 at 20:24:55
Jeff's comments/questions are appreciated, these helped.
This:
echo testing:  del "%~1"
echo testing:  del "%~n1.pdf"

does not delete the files, just shows what would be deleted, as a safety.
I don't know where "FILE" comes from, but probably from "FILE NOT FOUND".

I see the problem with two files, my bad. I started running from the first file in the directory (as default sorted by name), rather than the one following the name provided by irfanview. Not having irfanview, I'll try to work on this. If you can, post the input from irfanview to the batch (C:\Users\PC\Desktop\Stuff\TestBatchFile.bat), add a line:
echo %1
pause
at beginning of that batch script, to let us know format of incoming target.

I figure I'm not much help and out of my league on this one, without running irfanview in testing. But the concept would be to start from the file After the one delivered buy irfanview, and proceed from there with a query "view,delete/skip" thru the rest of the files.


Reply ↓  Report •

#8
November 2, 2020 at 20:45:46
nbrane, thanks for following up.
- With "FILE" you refer to above: This may be just the name of the file, as in "FILE.jpg".
- With your query "post the input from irfanview to the batch ... to let us know format of incoming target":
I won't be able to do this really. Do you mean to the original code or your code in post #5?
Is this what I add to the beginning?

echo %1
pause


Reply ↓  Report •

#9
November 3, 2020 at 18:49:40
Yes. I only want to see what is being delivered (via argument #1) to the batch script, and how it's formatted. I figured it was just a path+filename+extension. I didn't think there would be a "FILE.jpg", so I thought that was an anomaly. (Oddly enough, most files are not named "FILE", ha ha!)
And yes, add that line to ...testfilebatch.bat. That will show what is incoming from irfanview to the batch script testfilebatch.bat.
I should also stress: batch is NOT GUI. The GUI handoff to the script testfilebatch implies that any further action occurs in the cmd window (the notorious "black screen").
If you need GUI interface, you'll need more than batch.

Reply ↓  Report •

#10
November 3, 2020 at 18:59:36
OK thanks for clarifying. Please see below for output.
With respect to the file name, I'll change that to something else so it's not confusing.
I understand a little with respect to the GUI but I didn't think that it would be needed, as the only thing that happens is that the next file is viewed. Am I missing something here?
Thanks.


C:\Users\PC\Desktop\Stuff>echo "C:\Users\PC\Desktop\Stuff\R2D6210.jpg"
"C:\Users\PC\Desktop\Stuff\R2D6210.jpg"

C:\Users\PC\Desktop\Stuff>pause
Press and key to continue ...


Reply ↓  Report •

#11
November 4, 2020 at 13:27:07
Does the file in IrfanView need to be closed before it
can be deleted? If so, I suggest these steps:

In IrfanView, while viewing an image, you click
File / Open with external editor, then choose one of
the up to three programs you have specified.

The batch program looks for the file currently being
displayed in that instance of IrfanView.

If the file is found, a second file is looked for with the
same filename and a .pdf extension.

If one or more files are found, a message is printed
on screen asking the user to confirm whether it / they
should be deleted.

If the response from the user is affirmative, the file in
IrfanView is closed and the file(s) is / are deleted.

A message is printed on screen either saying which files
were deleted, or indicating that no files were deleted.

If the file was closed, then the next file in the current
search order is looked for and opened in that instance
of IrfanView.

Control is returned to IrfanView.

-- Jeff, in Minneapolis

message edited by Jeff Root


Reply ↓  Report •

#12
November 4, 2020 at 19:00:47
Jeff, I would prefer that there are no messages or dialogue prompts. Simply just to move to the next file.

This is the setting I have in Irfanview:
Options, Properties/Settings, File Handling. Uncheck "Ask me before deleting files". Check "Jump to the next file after deleting/moving".


Reply ↓  Report •

#13
November 4, 2020 at 21:08:05
I suggested a message in part because you said that the
lack of feedback was the main reason for your query. The
suggestion did not include a pause, so it might just flash by
on the screen very quickly. But of course it can be left out.

I suggested the user confirmation because it is so easy to
click on something you didn't mean to click on. When doing
so deletes files, that is when I ask for user confirmation.
You might not need it if you depend on the Recycle Bin, or
if you never click on anything by mistake.

You can guess that I have "Ask me before deleting files"
checked, but I'm somewhat surprised to see that I don't
have "Jump to the next file after deleting/moving" checked.
Maybe it is such a long time since I used that function that
I didn't see a need for it last time I installed IrfanView.

-- Jeff, in Minneapolis


Reply ↓  Report •

#14
November 4, 2020 at 21:16:43
Jeff, I understand what you mean.
Basically the feedback required was to move to the next file, just to visually see this.
The aspect of the Recycle Bin is important actually but I didn't bring it up because it may have complicated the query. As I understand the files are completely gone when deleted using the external editor, but if there was an option to send to the Recycle Bin, it would be beneficial.

Reply ↓  Report •

#15
November 6, 2020 at 01:22:35
Do you think this was going to be possible?
If it's too difficult then just leave it as it's taking up a lot of member's time.

message edited by batch_y


Reply ↓  Report •

#16
November 6, 2020 at 03:23:56
It seems to me that it is pretty straightforward. If not nbrane,
somebody here should be able to do it. I don't know how much
the things I suggested depend on communication between
IrfanView and the batch program, or how that is done in batch
programs.

I may be overly timid about deleting files without user confirmation
because I once wrote a very, very (meaning "too") simple batch
program to automate deletion of old files on my computer. I failed
to use "ifexist". It wiped half the hard drive before I realized what
was happening. That was about 20 years ago.

-- Jeff, in Minneapolis


Reply ↓  Report •

Ask Question