gear Posted January 23, 2007 Posted January 23, 2007 expandcollapse popup;=============================================================================== ; ; Function Name: RenameAndCombFileWithFirstLine ; Description:: Rename the files with its first line, if the first line is only spaces or blank, find the next line. ;if there are some spaces between words, all the spaces except one single space will be removed. ;Remove all the lines that are only spaces or blank line ; Parameter(s): $Folder - A folder name, the files within it will be renamed ; $FileExtention - The exstention of the file to be renamed, default is *.txt ; $NumberOnHeadOrEnd - Number, it determines where the file number will be placed in the file name, if it is 1, at the head of file name, else at the end of name . defaule is 1. ; Requirement(s): #include <File.au3>,#include <Array.au3>,#include <Misc.au3> ; Author(s): Gear ;Notes: If there is(are) files is failed to be renamed or combed, the file name will be wrote to console and the Beep sound will remind program runner. ; Example: RenameFileWithFirstLine("c:\3") ; ;=============================================================================== ; #include <File.au3> #include <Array.au3> #include <Misc.au3> RenameFileWithFirstLine("c:\3") Func RenameFileWithFirstLine($Folder, $FileExtention = "*.txt", $NumberOnHeadOrEnd = 1) $FileList = _FileListToArray($Folder, $FileExtention, 1) $FileEx = StringReplace($FileExtention, "*", "") If IsArray($FileList) And $FileList[0] >= 1 Then; when there is no file within $Folder, $FileList is not a array. $FileRenameNumber = 0;count how many files are renamed within a folder ;so $FileList[0] leads to error For $j = 1 To $FileList[0] Dim $FileContentNoBlank[1] = [0];for saving the combed file lines one by one Dim $FileContent $CurrentFile = $Folder & "\" & $FileList[$j] $fileOpen = _FileReadToArray($CurrentFile, $FileContent) If $fileOpen Then $NotFound = 1;the tag for if the line for file name is found For $WhichLine = 1 To $FileContent[0];extract the line one by one from the array $linecontOringinal = $FileContent[$WhichLine] $TheUnWelcomeCharacterInFileName = '<|>|?|:|*|\|/|"| | | | ';these are the unacceptable characters in file name $AfterStripName0 = _StringRemove($linecontOringinal, $TheUnWelcomeCharacterInFileName, "|") $AfterStripName1 = _StringRemove($AfterStripName0, " | ", "|") $Len0 = StringLen($AfterStripName0) $Len1 = StringLen($AfterStripName1) While $Len1 <> $Len0;it can remove all the white space betwwen strings but still ramians the single one betwwen strings if there is(are) spaces originally. $Len0 = $Len1 $AfterStripName1 = StringReplace($AfterStripName1, " ", " ") $AfterStripName1 = StringReplace($AfterStripName1, " ", " ") $Len1 = StringLen($AfterStripName1) WEnd If ($AfterStripName1 <> "" And $AfterStripName1 <> " " Or $AfterStripName1 <> " ") Then $NotFound = 0;the line for file name is found ExitLoop EndIf Next If $NotFound == 0 Then;only when the line for file name is found then continue to comb the file line by line $FileRenameNumber = $FileRenameNumber + 1 Select;in order to let the renamed files can be arranged according its original sequence, you can put the number ahead of file name Case $FileRenameNumber >= 0 And $FileRenameNumber < 10 $FileRenameNumberString = "000" & String($FileRenameNumber) Case $FileRenameNumber >= 10 And $FileRenameNumber < 100 $FileRenameNumberString = "00" & String($FileRenameNumber) Case Else $FileRenameNumberString = "0" & String($FileRenameNumber) EndSelect If $NumberOnHeadOrEnd = 1 Then;the number is ahead $NumberHead = $FileRenameNumberString & " " $NumberEnd = "" Else $NumberHead = "";the number is at the end $NumberEnd = " " & $FileRenameNumberString EndIf $name = $Folder & "\" & $NumberHead & $AfterStripName1 & $NumberEnd & $FileEx For $WhichLine = 1 To $FileContent[0] $linecontOringinal = $FileContent[$WhichLine] $linecont = StringReplace($linecontOringinal, " ", "") $linecont = StringReplace($linecont, " ", "") If $linecont <> "" Then _ArrayAdd($FileContentNoBlank, $linecontOringinal);if the line is not only spaces, then add the original one to array, without any change $FileContentNoBlank[0] = $FileContentNoBlank[0] + 1;$FileContentNoBlank[0] saves the total line of the new file EndIf Next If $name <> $CurrentFile Then $IfWriteGood = _FileWriteFromArray($name, $FileContentNoBlank, 1) If $IfWriteGood Then $IfDel = FileDelete($CurrentFile) Else $FileRenameNumber=$FileRenameNumber-1 Beep(200, 100) ConsoleWrite("Fail to Rename and comb file: " & $CurrentFile & @CRLF) EndIf EndIf If $name == $CurrentFile Then;when the old name and new name is the same, delete the old file will also delete the new file $WholeOriginalFile = FileRead($CurrentFile); ready for the situation that if fails to write the ccombed file If @error <> 1 Then;check if the file is read out successfully $IfDel = FileDelete($CurrentFile);in order to avoid later delete the two files which are with the same name, so here delete the old file first $IfWriteGood = _FileWriteFromArray($name, $FileContentNoBlank, 1) If $IfWriteGood == 0 And $IfDel == 1 Then;if delete the old file but fail to write the new file $FileRenameNumber=$FileRenameNumber-1 $file = FileOpen($CurrentFile, 10);try to rewrite the old file to disk $IfRewriteGood = FileWrite($file, $WholeOriginalFile) If $IfRewriteGood == 0 Then;fail to rewrite the old file to disk so this file is lost Beep(500, 1000) ConsoleWrite("Attention!" & $CurrentFile & " is deleted and lost!" & @CRLF) Else;old file is rewrote to disk Beep(500, 200) ConsoleWrite("Attention! Fail to Rename and comb file: " & $CurrentFile & @CRLF) EndIf EndIf If $IfWriteGood == 0 And $IfDel == 0 Then;fail to delete the old file and fail to write the new one to disk $FileRenameNumber=$FileRenameNumber-1 Beep(500, 200) ConsoleWrite("Attention! Fail to Rename and comb file: " & $CurrentFile & @CRLF) EndIf If $IfWriteGood == 1 And $IfDel == 0 Then;write the new file to disk but fail to delete the old file Beep(500, 200) ConsoleWrite("Attention! Fail to delete the old file: " & $CurrentFile & @CRLF) EndIf Else;fail to open the old file and stop to write the new one just for the consideration of file security $FileRenameNumber=$FileRenameNumber-1 Beep(500, 200) ConsoleWrite("Attention! Fail to Rename and comb file: " & $CurrentFile & @CRLF) EndIf EndIf Else;fail to find the line to be the file name and stop renaming and combing this file Beep(500, 200) ConsoleWrite("Attention! Fail to Rename and comb file: " & $CurrentFile & @CRLF) EndIf Else;fail to open the file to read its content Beep(500, 200) ConsoleWrite("Attention! Fail to Rename and comb file: " & $CurrentFile & @CRLF) EndIf Next Else;no file ConsoleWrite("There is no file within " & $Folder & @CRLF) EndIf ;if there is(are) sub-folder within $Folder, try to recursively rename and comb the file within them $FolderList = _FileListToArray($Folder, "*", 2);when within folder there is no file and folder, $FolderList is not an array. If IsArray($FolderList) And $FolderList[0] >= 1 Then;when within folder there is(are) files but no child folder, For $i = 1 To $FolderList[0]; $FolderList is an array but $FolderList[0] is nothing(but not 0), so here must use $FolderList[0] >= 1 RenameFileWithFirstLine($Folder & "\" & $FolderList[$i], $FileExtention, $NumberOnHeadOrEnd) Next Else;no sub-folder ConsoleWrite("There is no child folder within " & $Folder & @CRLF) EndIf EndFunc ;==>RenameFileWithFirstLine ;=============================================================================== ; ; Function Name: _StringRemove ; Description:: Removes certain characters or phrases from a string ; Parameter(s): $s_string - String ; $s_remove - Characters to be removed (separated by $s_delim) ; $s_delim[optional] (delimiter '|' by default) ; Requirement(s): None ; Return Value(s): On Success - String with removed characters ; On Failure - 0 ; Author(s): RazerM ; Notes: Only useful when removing lots of characters ; Example: MsgBox(0,0,_StringRemove("102.3040506.0700...80900.", "0|.")) ; ;=============================================================================== ; Func _StringRemove($s_string, $s_remove, $s_delim = "|") $s_remove = StringSplit($s_remove, $s_delim) If @error Then Return 0 For $i = 1 To $s_remove[0] $s_string = StringReplace($s_string, $s_remove[$i], " ") If @error Then Return 0 Next Return $s_string EndFunc ;==>_StringRemove
Uten Posted January 23, 2007 Posted January 23, 2007 Probably a nice UDF when you need it. Should have been posted in Scripts though. If you feel the urge to study other ways of doing it, StringRegExpReplace is a good place to start.. Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling
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