Jump to content

How to obtain file name from torrent file?


Recommended Posts


See, I have thousands* of torrent files like [ fuzy msrooms.torrent ] downloading files in utorrent.

But the files are saved as [ fm.rar ] & now I donot know what file contains what.

To solve this issue I can make a script that will rename the file after it has been downloaded from:

[ fm.rar ] to [ fuzy msrooms.rar ]

But as you already figured I do not know how to obtain file name [ fm.rar ] from [ fuzy msrooms.torrent ] Programmatically.

I use autoitscript.

Can you guys get me started or are there any guides that explain how to retrieve data from torrent file?

Link to comment
Share on other sites

I read the FAQ & it has no info on it

Metainfo File Structure - read it also, but I do not know how to use it.

I mean there are no examples.

I mean I can convert the file into binary & then search for some binary values. Or should I convert string [ name ] into hex & search for it? But this is aint going to work, will it?

I mean I havent done this before so I dont know where to start or what to sear for.



k I figured it out, you can say thanx to Zinthose from autoitscript forums for _HexRead UDF.

Hire is the UDF to retrieve filename from torrents in your script folder & rename them:

NOTE This is safe script that does not rename anything only shows msbox, just in case some novice runs it! If you want to rename the files, Uncomment This line:

I made exe file + source code also: Note that exe file will rename files so make a backup first


ou make new folder, put all your downloaded files, torrent files and this program in this folder & start it. It will rename all files in this folder

sample before:

New folder:
..............Get File name from torrent file + rename.exe
..............Blue Mushrooms.torrent


New folder:
..............Get File name from torrent file + rename.exe
..............Blue Mushrooms.torrent
..............Blue Mushrooms.rar

FileMove($filename1,$filename) ; Uncomment this line to rename file Is dangerous

#cs ----------------------------------------------------------------------------

AutoIt Version:
Author: myName

Script Function: Get real_file_name.* from myfile*.torrent file then find real_file_name.* file in scriptdir & rename it to myfile*.*

UDF Source:
#ce ----------------------------------------------------------------------------
#include <WinAPI.au3>
#include <string.au3>

Global $_String_lenght = '100' ; lenght of the returned string if filename is too long



Func _START()

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.torrent")

; Check if the search was successful
If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")

While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop

$filename1 = _readfilename($file)

$filename = _Newfilename($file,$filename1) ;[ Comment out this line to return only file name inside the torrent file]

; FileMove($filename1,$filename) ; Uncomment this line to rename file Is dangerous

ConsoleWrite($filename1 & ' ' & $filename & @CRLF)
;~ MsgBox(0, 'Rename to:',$filename1 & ' ' & $filename & @CRLF)


Func _readfilename($file)

$data = _HexRead($file, '369', $_String_lenght)

$data = StringTrimLeft($data,2)

$data = _HexToString($data)

$data = StringSplit($data,'12:',1)
$data = $data[1]

Return $data

Func _Newfilename($file,$filename)

$get_file_extension = StringSplit($filename,'.',1)
$get_file_extension = $get_file_extension[$get_file_extension[0]]

$newfilename = StringReplace($file,'.torrent','')
$newfilename = $newfilename & '.' & $get_file_extension

Return $newfilename

Func _HexRead($FilePath, $Offset, $Length)
Local $Buffer, $ptr, $fLen, $hFile, $Result, $Read, $err, $Pos

;## Parameter Checks
If Not FileExists($FilePath) Then Return SetError(1, @error, 0)
$fLen = FileGetSize($FilePath)
If $Offset > $fLen Then Return SetError(2, @error, 0)
If $fLen < $Offset + $Length Then Return SetError(3, @error, 0)

;## Define the dll structure to store the data.
$Buffer = DllStructCreate("byte[" & $Length & "]")
$ptr = DllStructGetPtr($Buffer)

;## Open File
$hFile = _WinAPI_CreateFile($FilePath, 2, 2, 0)
If $hFile = 0 Then Return SetError(5, @error, 0)

;## Move file pointer to offset location
$Pos = $Offset
$Result = _WinAPI_SetFilePointer($hFile, $Pos)
$err = @error
If $Result = 0xFFFFFFFF Then
Return SetError(6, $err, 0)

;## Read from file
$Read = 0
$Result = _WinAPI_ReadFile($hFile, $ptr, $Length, $Read)
$err = @error
If Not $Result Then
Return SetError(7, $err, 0)

;## Close File
If Not $Result Then Return SetError(8, @error, 0)

;## Return Data
$Result = DllStructGetData($Buffer, 1)

Return $Result

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...