Delete last lines of characters

hello I need to create a list of images in a txt file, but I need to delete the last character and remove duplicates

Example

SDFDFDF_1.jpg
SDFDFDF_2.jpg
THGGHFG_1.jpg
THGGHFG_2.jpg

and should stay that way on the txt list

SDFDFDF
THGGHFG

my code only removes .jpg

@echo off
if exist "sku.txt" del "sku.txt"
for /f "delims=" %%f in ('dir *.jpg /b') do echo %%~nf >> sku.txt
+4
source share
4 answers

If extensions are enabled, this should work:

@echo off
if exist "sku.txt" del "sku.txt"
setlocal enabledelayedexpansion
set list=
for /f "usebackq delims=" %%f in (`dir /b *.jpg`) do (
  set xx=%%~nf
  set candidate=!xx:~0,-2!
  set n=0
  for %%i in (!list!) do (
    if "!candidate!"=="%%i" (
      set n=1
    )
  )
  if !n! EQU 0 (
    set list=!list! !candidate!
    echo !candidate! >> sku.txt
  )
)

endlocal
+2
source

substrings

The package supports syntax for taking a substring from a string. The general syntax for this is

%string:~start,end%

Main use:

set test=test
echo %test:~2,3%
::st

How does this relate to your question? When a negative number is used as the ending index, the substring stops the set of characters to the end of the line. This behavior makes it easy to drop characters from the end of a line.

set test=test
echo %test:~0,-1%
::tes
+11
source
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "prev="
(
 FOR /f "tokens=1delims=_" %%a IN (
    'dir /b /o:n /a-d "%sourcedir%\*.jpg" '
  ) DO (
  CALL :unique %%a
 )
)>u:\newfile.txt

GOTO :EOF

:unique
IF /i "%prev%"=="%1" GOTO :EOF
SET "prev=%1"
ECHO(%1
GOTO :EOF

sourcedir .

u:\newfile.txt

( ) , %%a, delims (_)

unique (partial-filename) ( ), , prev echo es it.

0

. , , , ( ).

, ABC_12.jpg?

, ABC.jpg?

I assume that you want to deal only with files that look like * _n.jpg, where n is a digit.

You can use my JREN.BAT utility to get a list of all files matching the pattern and delete the last two characters plus the extension. You can link this with my JSORT.BAT utility to sort and delete duplicates. Both utilities are JScript / batch hybrid scripts that run initially on any Windows computer with XP and beyond.

jren ".{6}$" "" /list /rfm "_\d\.jpg$"|jsort /u /i >sku.txt
0
source

All Articles