Batch script to conditionally delete files

6,286

Create a batch file. Start here: Loop command: against the results of another command and here: Command Line arguments (Parameters)

For /F "tokens=*" %%G in ('dir /b *_pro.jpg') do (
   echo %%~dpnG
)

Now you could change echo %%~dpnG to

   if exist "%%~dpnG__highres.jpg" echo del "%%~dpnxG"

or even to

   if exist "%%~dpnG__highres.jpg" (
        echo del "%%~dpnxG"
   ) else (
        echo retain %%~dpnxG
   )

Thoroughly check the output (better twice) and substitute echo del with del.

Add /s switch to dir command as follows: dir /b /s *_pro.jpg to treat files in subfolders as well.

Share:
6,286

Related videos on Youtube

Nano
Author by

Nano

Updated on September 18, 2022

Comments

  • Nano
    Nano over 1 year

    Owners of Nokia Lumia 1020 know it can be set to store captured photos at full resolution, but the reduced size photos are also stored. I would like to have a script to automatically delete all the low resolution versions.

    The filenames are kind of the following:

    WP_20150303_17_20_38_Pro.jpg
    WP_20150303_17_20_38_Pro__highres.jpg
    WP_20150303_17_21_21_Pro.jpg
    WP_20150303_17_21_21_Pro__highres.jpg
    WP_20150303_17_21_32_Pro.jpg
    WP_20150303_17_21_32_Pro__highres.jpg
    WP_20150303_17_22_47_Pro.jpg
    WP_20150303_17_22_47_Pro__highres.jpg
    WP_20150303_17_28_14_Pro.jpg
    WP_20150303_17_28_14_Pro__highres.jpg
    

    I could delete *_pro.jpg, but then there is a risk that what if there is no high resolution version at all. How can I write a script that would first check if there is a highres file present and only then delete lowresolution version?