简体   繁体   中英

Delete lines of text file if they reference a nonexistent file

I have a text file ( images1.txt ) with lists of .jpg names and I have a folder ( Bones ) with .jpg images. All image names are exactly 42 characters (including the file extension), and each is on a separate line containing the name and some information about the image. For example:

OO75768249870G_2018051_4A284DQ0-011628.jpg,1A4502432KJL459265,emergency
OO75768249870G_2018051_4A284DQ0-011629.jpg,1A451743245122,appointment

where everything after .jpg is my own personal notes about the photos. Bones contains many of the 4,000+ images named in images1 but not all. Using either the command prompt or python, how would I remove the lines from images1 which correspond to images not present in my Bones folder?

Thanks!

In python:

import os

LEN_OF_FILENAME = 42

with open('images1.txt', 'r') as image_file:
    with open('filtered_images1.txt', 'w') as filtered_image_file:
        for line in image_file:
            image_name = line[:LEN_OF_FILENAME]
            path_to_image = os.path.join('Bones', image_name)
            if os.path.exists(path_to_image):
                filtered_image_file.write(line)

Assuming images1.txt and Bones are in the same folder, if you run the above Python script in that folder you will get filtered_images1.txt . It will only contain lines that has a corresponding image in Bones .

This code will read the lines from image1.txt and create an image2.txt with the lines where the file exists in the bones directory.

@ECHO OFF
IF EXIST image2.txt (DEL image2.txt)
FOR /F "tokens=1,* delims=," %%f IN ('TYPE "image1.txt"') DO (
    IF EXIST "bones\%%~f" (ECHO %%f,%%g >>"image2.txt")
)
EXIT /B

I think the easiest way is to use the findstr command :

rem /* Search for lines in file `images1.txt` in a case-insensitive manner that literally begin
rem    with a file name found in the directory `Bones` which in turn matches the naming pattern;
rem    then write all matching lines into a temporary file: */
dir /B /A:-D "Bones\??????????????_???????_????????-??????.jpg" | findstr /LIBG:/ "images1.txt" > "images1.tmp"
rem // Overwrite original `images1.txt` file by the temporary file:
move /Y "images1.tmp" "images1.txt" > nul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM