Jump to content

How to obtain file name from torrent file?


gaastra

Recommended Posts

Hi.

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.

EDIT:

===================================================================

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

http://www.megaupload.com/?d=CXBV3L7Y

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
..............bm.rar

After:

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

#AutoIt3Wrapper_Res_SaveSource=y
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.0.0
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:
http://www.autoitscript.com/forum/index.php?showtopic=84615&st=0#entry722139
#ce ----------------------------------------------------------------------------
#include <WinAPI.au3>
#include <string.au3>

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

_START()

Exit

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")
Exit
EndIf

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)
WEnd

FileClose($search)
EndFunc

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
EndFunc


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
EndFunc


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
_WinAPI_CloseHandle($hFile)
Return SetError(6, $err, 0)
EndIf

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

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

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

Return $Result
EndFunc

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...