Computing.Net > Forums > Programming > Editing a txt file, renaming files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Editing a txt file, renaming files

Reply to Message Icon

Name: pball
Date: February 23, 2008 at 11:41:47 Pacific
OS: xp pro
CPU/Ram: 2 gigs
Product: homade
Comment:

First off I'm trying to use a command line md5 hash finder to get the md5 hash of a picture then rename the picture to the md5 hash using batch.

When I get a txt file with the picture names and md5 there is also some extra lines of information put in by the md5 program. I would like to be able to remove those lines from the txt. How would I start doing that.

After only the names and md5 of the pictures are left in the txt i'd like to rename the file with the md5.

here is an example of the txt without the extra lines
1188454269070.jpg 721fbfd2682daea798e7dafe0f5c0e92
1190221107488.jpg 014a19a63a4028245fdc65a2c9819beb
1190224549110.jpg bea46ead775f3fbfae81c4418c5bc5b1
1190502027485.jpg e49a35c2581cc85c4e17e277c602b0f5

Is there a way to rename the file to the md5 that is assigned to the file name in the txt.

This is what I have so far for the batch file

for /f "tokens=*" %%x in ('dir /b *.jpg') do ("e:\pics\md5sums.exe" -n -b "%%x" >>%cd%\log.txt)

and the current output

MD5sums 1.2 freeware for Win9x/ME/NT/2000/XP+
Copyright (C) 2001-2005 Jem Berkes - http://www.pc-tools.net/

1188454269070.jpg 721fbfd2682daea798e7dafe0f5c0e92

MD5sums 1.2 freeware for Win9x/ME/NT/2000/XP+
Copyright (C) 2001-2005 Jem Berkes - http://www.pc-tools.net/

1190221107488.jpg 014a19a63a4028245fdc65a2c9819beb

MD5sums 1.2 freeware for Win9x/ME/NT/2000/XP+
Copyright (C) 2001-2005 Jem Berkes - http://www.pc-tools.net/

1190224549110.jpg bea46ead775f3fbfae81c4418c5bc5b1

MD5sums 1.2 freeware for Win9x/ME/NT/2000/XP+
Copyright (C) 2001-2005 Jem Berkes - http://www.pc-tools.net/

1190502027485.jpg e49a35c2581cc85c4e17e277c602b0f5

note file name and md5 are on the same line in the txt file



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: February 24, 2008 at 01:46:45 Pacific
Reply:

I don't have a command line md5. [Maybe you can post a link to one.] But I suppose it works much like a CRC utility. If so, try this:

::==


@echo off
setLocal EnableDelayedExpansion

for /f "tokens=1-2 delims= " %%a in ('crc32.exe my.jpg ^|find "MY.JPG"') do (
cls
echo %%b %%a >> %%a.txt
echo ren %%b %%a.jpg
)



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: IVO
Date: February 24, 2008 at 01:56:01 Pacific
Reply:

@echo off
for /f "tokens=*" %%i in ('dir /b *.jpg') do (
for %%j in ('E:\pics\md5sums -n -b "%%i" ^| Find /I "jpg"') do (
ren "%%i" %%j
)
)


0

Response Number 3
Name: pball
Date: February 24, 2008 at 09:38:58 Pacific
Reply:

I tried both and could not get them to work so I'm gonna try some more.

Here is the link to the md5 program I am using http://www.pc-tools.net/win32/md5sums/


0

Response Number 4
Name: IVO
Date: February 24, 2008 at 11:49:22 Pacific
Reply:

@echo off
for /f "tokens=*" %%i in ('dir /b *.jpg') do (
for /f "tokens=2" %%j in ('E:\pics\md5sums -n -b "%%i" ^| find /I ".jpg"') do (
ren "%%i" %%j.jpg
)
)



0

Response Number 5
Name: pball
Date: February 24, 2008 at 14:28:42 Pacific
Reply:

Thanks IVO
that works great, only problem is if the file name has a space it will rename the file to that second part, but that isn't a big deal. I can just run it a second time.


Thanks again


0

Related Posts

See More



Response Number 6
Name: IVO
Date: February 24, 2008 at 14:56:14 Pacific
Reply:

In Italy now time is late night, so I'll answer (I hope to achieve what you want) tomorrow. Just I don't understand the md5 renaming issue as the script looks for .jpg files and ... there are md5.jpg archives in the directory?

good night (for now)


0

Response Number 7
Name: pball
Date: February 24, 2008 at 15:16:41 Pacific
Reply:

Forget about that confusing stuff i said
if the original name is the same as what it wants to rename it to there is no problem so i'll just leave it

As I said earlier the only real problem is if the name has a space in it such as
one two three.jpg the batch script would rename it to two.jpg since that is the second word there. I have no clue how to fix that since i'm the one asking for help as it is.


0

Response Number 8
Name: IVO
Date: February 25, 2008 at 02:17:07 Pacific
Reply:

The following script is an improved version of my original one, faster and renaming correctly files with embedded spaces.

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /b *.jpg') do (
for /f "skip=3 delims=" %%j in ('E:\pics\md5sums -n -b "%%i"') do (
set file=%%j
set file=!file:*.jpg=!
for %%k in (!file!) do ren "%%i" "%%k.jpg"
)
)


0

Response Number 9
Name: pball
Date: February 25, 2008 at 13:31:30 Pacific
Reply:

thanks once again it works much better now


0

Response Number 10
Name: pball
Date: April 20, 2008 at 17:05:03 Pacific
Reply:

I'm playing with this batch file again and was wondering if someone could tell me how to have it process files in the folders as the same directory as the script.

So if I have the script in c:\ it'd only work on files in c:\folder and every other folder in c:\

also if there is a duplicate file and is there an easy way to delete the file that was trying to be renamed. Is there an error code returned when "A duplicate file name exists, or the file cannot be found." is returned since the file name is already used so you could say delete when that happens?


0

Response Number 11
Name: pball
Date: May 8, 2008 at 12:29:10 Pacific
Reply:

I'd like to bump this once and see if anyone can help with my last question.


0

Sponsored Link
Ads by Google
Reply to Message Icon

How I can handle Window e... 2 questions, 1 post



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Editing a txt file, renaming files

batch to edit a txt file www.computing.net/answers/programming/batch-to-edit-a-txt-file/15327.html

Editing a txt file with batch files www.computing.net/answers/programming/editing-a-txt-file-with-batch-files/19270.html

Read a txt file in Vb www.computing.net/answers/programming/read-a-txt-file-in-vb/7871.html