Michiel78 Posted November 11, 2010 Share Posted November 11, 2010 For a while I've been wondering whether it's possible or not (and if it is how) to refer to specific lines within a script. Basically this is what I mean:1. This line holds a code for function 1 and is different from line 102-9. These lines hold code for function 1, but it is the same code as in lines 11-1910. Code for function 2, different from line 111-19. Code for function 2, same code as lines 2-9In almost all cases I can simply prevent to have the same code in two functions, by using If statements or anything similar. But sometimes it it simply not possible or very difficult to merge two functions into one, while both share a big part of the code. So my question is if there is a way to (in my example) refer to lines 2-9 from line 11, without having to fully paste it there? Link to comment Share on other sites More sharing options...
JohnOne Posted November 11, 2010 Share Posted November 11, 2010 I dont know of a case where functions cannot be used with different paramaters or flags of some sort. Please create an example in code. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Michiel78 Posted November 11, 2010 Author Share Posted November 11, 2010 The code below is my most recent example. The first part of the script renames all pdf-files within the subfolders of a chosen directory ($PDFfoldername) and the second part renames the ones that are in the chosen directory itself. Even though the replacement part is the same in both parts, I cannot merge them into óne working part. This is because the first search and filemove are based on subdirectories with files, while the second search and filemove are based on files directly in the chosen directory. That is why I wondered if I could refer to the code that remains the same, but perhaps if anyone does know a way to merge them I don't even need to refer. expandcollapse popup$PDFfoldername = "C:\Testfolder" Func pdf renamer() ;the part for renaming all files in subdirectories starts here $search = FileFindFirstFile($PDFfoldername & "/" & "*.*") 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 If @extended Then $hFile = FileOpen("replacements.txt", 0) If $hFile = -1 Then MsgBox(0, "Error", "Could not find textfile") Exit EndIf $sPDFOriginalFile = FileFindFirstFile($PDFfoldername & "\" & $file & "\*.pdf*") If $sPDFOriginalFile = -1 Then MsgBox(0, "Error", "No directory has been chosen or no files could be found.") Exit EndIf While 1 $sPDFOriginalFileName = FileFindNextFile($sPDFOriginalFile) If @error Then ExitLoop $sNewFilename = "" If StringLen($sPDFOriginalFileName) = 2 Then For $i = 1 To 9 If $i = 1 Then $sLetter = StringMid($sPDFWithoutExtension, $i, 1) $iIndex = Asc(StringUpper($sLetter)) - 64 $sNewFilename &= FileReadLine($hFile, $iIndex) Endif Next Endif If $sNewFilename <> "" Then FileMove($PDFfoldername & "\" & $file & "\" & $sPDFOriginalFileName, $PDFfoldername & "\" & $file & "\" & $sNewFilename & ".pdf") Endif WEnd FileClose($hfile) Endif WEnd ;the part for renaming all files in the current directory starts here $hFile = FileOpen("replacements.txt", 0) If $hFile = -1 Then MsgBox(0, "Error", "Could not find textfile") Exit EndIf $sPDFOriginalFile = FileFindFirstFile($PDFfoldername & "\*.pdf*") If $sPDFOriginalFile = -1 Then MsgBox(0, "Error", "No directory has been chosen or no files could be found.") Exit EndIf While 1 $sPDFOriginalFileName = FileFindNextFile($sPDFOriginalFile) If @error Then ExitLoop $sNewFilename = "" If StringLen($sPDFOriginalFileName) = 2 Then For $i = 1 To 9 If $i = 1 Then $sLetter = StringMid($sPDFWithoutExtension, $i, 1) $iIndex = Asc(StringUpper($sLetter)) - 64 $sNewFilename &= FileReadLine($hFile, $iIndex) Endif Next Endif If $sNewFilename <> "" Then FileMove($PDFfoldername & "\" & $sPDFOriginalFileName, $PDFfoldername & "\" & $sNewFilename & ".pdf") Endif WEnd FileClose($hfile) MsgBox(0, "Completed", "The rename process has finished.") FileClose($search) EndFunc ; pdf rename() Link to comment Share on other sites More sharing options...
MrMitchell Posted November 11, 2010 Share Posted November 11, 2010 (edited) You can have your function take a parameter? Like: Func pdf_renamer($fSubs) ;Call function with either a (0) or (1) If $fSubs Then ;Do stuff for subdirectories here Else ;Just do the toplevel directory EndIf EndFunc It's a bit of a weak example but you get where I'm going with it? Edited November 11, 2010 by MrMitchell Link to comment Share on other sites More sharing options...
JohnOne Posted November 11, 2010 Share Posted November 11, 2010 Have you just put them into one function now? because I cannot see any subfolders being selected AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Michiel78 Posted November 11, 2010 Author Share Posted November 11, 2010 (edited) @MrMitchell: thanks for your fast reply! I have tried something similar to your idea before, but I couldn't really match it with the goal I would like to achieve. This is because I would like the script to: - Rename files in the toplevel directory only when there are no subdirectories - Rename files in the toplevel as well as in subdirectories if there are any - Rename files in subdirectories only when there are no files in the toplevel directory When I make the function take a parameter like you suggested, I keep ending up with above 3 points as If statements, which all have a part of script that is the same. This is why I am wondering if it is possible to refer to a part of the script, to prevent unnecessary copied script. I'm sure there must be another solution then this for my problem, but I haven't found it yet . @JohnOne: the script I've posted is not the merged version, but the original one. The line "If @extended Then" is selecting any subfolders found with $search. Edited November 11, 2010 by Michiel78 Link to comment Share on other sites More sharing options...
JohnOne Posted November 11, 2010 Share Posted November 11, 2010 (edited) Example, I found 5 parts of your code all doing something similar. So made one function for it, with the 3 different flags. expandcollapse popup$PDFfoldername = "C:\Testfolder" Func pdf renamer() ;the part for renaming all files in subdirectories starts here $search = FileFindFirstFile($PDFfoldername & "/" & "*.*") _errorcheck($search,1);### Check for the error with flag 1 for error msg While 1 $file = FileFindNextFile($search) If @error Then ExitLoop If @extended Then $hFile = FileOpen("replacements.txt", 0) _errorcheck($hFile,3) $sPDFOriginalFile = FileFindFirstFile($PDFfoldername & "\" & $file & "\*.pdf*") _errorcheck($sPDFOriginalFile,2) While 1 $sPDFOriginalFileName = FileFindNextFile($sPDFOriginalFile) If @error Then ExitLoop $sNewFilename = "" If StringLen($sPDFOriginalFileName) = 2 Then For $i = 1 To 9 If $i = 1 Then $sLetter = StringMid($sPDFWithoutExtension, $i, 1) $iIndex = Asc(StringUpper($sLetter)) - 64 $sNewFilename &= FileReadLine($hFile, $iIndex) EndIf Next EndIf If $sNewFilename <> "" Then FileMove($PDFfoldername & "\" & $file & "\" & $sPDFOriginalFileName, $PDFfoldername & "\" & $file & "\" & $sNewFilename & ".pdf") EndIf WEnd FileClose($hFile) EndIf WEnd ;the part for renaming all files in the current directory starts here $hFile = FileOpen("replacements.txt", 0) _errorcheck($hFile,3) $sPDFOriginalFile = FileFindFirstFile($PDFfoldername & "\*.pdf*") _errorcheck($sPDFOriginalFile,2) While 1 $sPDFOriginalFileName = FileFindNextFile($sPDFOriginalFile) If @error Then ExitLoop $sNewFilename = "" If StringLen($sPDFOriginalFileName) = 2 Then For $i = 1 To 9 If $i = 1 Then $sLetter = StringMid($sPDFWithoutExtension, $i, 1) $iIndex = Asc(StringUpper($sLetter)) - 64 $sNewFilename &= FileReadLine($hFile, $iIndex) EndIf Next EndIf If $sNewFilename <> "" Then FileMove($PDFfoldername & "\" & $sPDFOriginalFileName, $PDFfoldername & "\" & $sNewFilename & ".pdf") EndIf WEnd FileClose($hFile) MsgBox(0, "Completed", "The rename process has finished.") FileClose($search) EndFunc ;==>pdf renamer Func _errorcheck($test,$flag) If $test = -1 Then switch $flag case 1 MsgBox(0, "Error", "No files/directories matched the search pattern") Exit Case 2 MsgBox(0, "Error", "No directory has been chosen or no files could be found.") Exit Case 3 MsgBox(0, "Error", "Could not find textfile") Exit EndSwitch EndIf EndFunc This dosent save to many lines of code, but you should get the jist, and knowung better what your code is doing, be able to make similar changes. Edited November 11, 2010 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now