Matthew Hansen
Matthew Hansen

Reputation: 23

Why doesn't this program echo any string?

Context- I'm in the process of sorting out, backing up, and archiving a load of my grandmother's pictures. I've just come across a set of folders where all of the file extensions have some junk numbers at the end (100_105.jpg_25_1025, 100_106.jpg_26_1026, ...)
I'm trying to make a batch program that will identify the file name parts, delimited by the period ("."), and then remove from the second part any junk part which will be defined as the underscore ("_") and anything after it. BUT, I've hit an obstacle early on. I can't get the program to spit out the second part of the file name.
Here's what I've got going on...

for %%G in (*.jpg*) do (
for /f "tokens=* delims=." %%a in (%%G) do echo %%b)

There aren't any errors. There's absolutely no output. My echos look like this...

G:\Project>(for /F "tokens=* delims=." %a in (100_0661.jpg_25_1026) do echo %b

If I replace (%%G) with (100_0661.jpg_25_1026), then it gives the expected output of "jpg_25_1026".

What's wrong with this? Is there a better solution to this problem? I know almost no programming lingo so go easy on me please.

Upvotes: 2

Views: 68

Answers (3)

Jool
Jool

Reputation: 1785

I recommend a gui freeware utility called Rename Master . It can do what you want and much more, and it provides a preview of what the result will be before running it.

Upvotes: 0

Matt Gibson
Matt Gibson

Reputation: 38238

From what I can see, you don't need to script a whole program for that. Try:

rename *.jpg* *.jpg

Given your example filenames, 100_105.jpg_25_1025 and 100_106.jpg_26_1026, you'll end up with 100_105.jpg and 100_106.jpg.

This kind of wildcard replacement has been an often-overlooked feature of rename since the DOS days...

Upvotes: 2

npocmaka
npocmaka

Reputation: 57252

for %%G in (*.jpg) do (
  for /f "tokens=1* delims=." %%a in ("%%G") do echo %%b
)

Set %%G in quotes process it as a string.Otherwise with will try to read the %%G as text file.

But there's easier way to rename pictures.Save this as bat in the same directory as the pictures (%%~nG will expand the picture to its name without the extension):

@echo off

    for %%G in (*.jpg_*_*) do (
        ren "%%~nxG"  "%%~nG.jpg" 
    )

It will not search in sub-directories though.And will process only the files that apply the mask *.jpg_*_* . Its possible to process also sub-folders but its not mentioned in the question .

Upvotes: 1

Related Questions