Home > Enterprise >  batch file delete xxx.lrf if xxx.jpg exists
batch file delete xxx.lrf if xxx.jpg exists

Time:09-23

Trying to run a batch file to clean up files recorded to a microSD card from a mavic 3 drone. It creates an .mp4 file (full resolution video) and a .lrf file (low resolution video). I also break up sessions of videos with a simple photo - which creates a .jpg and a .lrf. My batch file works great to move all the .mp4 files to a new "full" folder and then renames the .lrf files to .mp4.

The issue is that when it comes to the .jpg/.lrf combo, I am looking for different behavior. After moving all of the .mp4 files, I'm trying to delete the .lrf files that have a matching .jpg. I feel like I'm close using:

for /r d:\DCIM\100MEDIA\ %%I in (*.jpg) do del %%~nI.lrf 2>nul

but it's not deleting the the matching .lrf file. This does work when I run it in a command line: for /r %I in (*.jpg) do del %~nI.lrf, but I'm looking to run this as a simple batch.

After deleting the .lrf that has an associated .jpg, I rename the low-res .lrf files to .mp4 (which for my purposes is all I really need).

A simple file structure:

1.lrf
1.mp4
2.lrf
2.mp4
3.jpg
3.lrf
4.lrf
4.mp4

The desired end result:

full (folder containing the full high-res videos)
1.mp4
2.mp4
3.jpg
4.mp4

My full batch file:

@echo off
mkdir d:\DCIM\100MEDIA\full
for /r d:\DCIM\100MEDIA\ %%m in (*.mp4) do move "%%m" d:\DCIM\100MEDIA\full
for /r d:\DCIM\100MEDIA\ %%I in (*.jpg) do del %%~nI.lrf 2>nul
for /r d:\DCIM\100MEDIA\ %%l in (*.lrf) do ren "%%l" *.mp4

CodePudding user response:

...del %%~dpnI.lrf 2>nul

you need the dp to add the drive and path, else it attempts to delete the filename in the current directory.

Similarly,

...ren "%%~dpnl.lrf" %%~nl.mp4

...BUT - this would generate a .mp4 file which would be re-processed by this procedure on the next run. You may or may not intend this to happen. (1.lrf becomes 1.mp4; the following run will attempt to overwrite the original 1.mp4)

CodePudding user response:

What about this approach:

forfiles /m *.jpg /C "cmd /c del /q @fname.lrf"

You go for every "*.jpg" file, and when you find one, you delete it in a quiet way (not asking for confirmation). The @fname variable is the name of your file, without extension.

  • Related