Sign in to follow this  
Followers 0
bluerein

filewrite all files in folders/subfolders

5 posts in this topic

I have around 4000 txt files in a directory with subdirectorys .. I need to remove the last line of each file and replace with a new line....

Whats the best way to go about it??

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have around 4000 txt files in a directory with subdirectorys .. I need to remove the last line of each file and replace with a new line....

Whats the best way to go about it??

Thanks

_FileListToArray, and then run through the array with a For...Next loop. For each file in the loop, you'd have to count the lines in the file and then do a _FileWriteToLine to delete the last line. In summation, read the help file topics for _FileListToArray, For...Next, _FileCountLines and _FileWriteToLine.

Edited by Airwolf

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt

Share this post


Link to post
Share on other sites

Thanks got it!!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Or use FileFindFirstFile/FileFindNextFile to step through the directory structure recursively.

$sPath = "C:\my\Txt\Directory"
RecurseDirs($sPath, "txt")

Func RecurseDirs ($sPath, $sExt, $sSpacer = " ")
    Local $hSearch, $sFile, $sFilePath
    $hSearch = FileFindFirstFile($sPath & "\*")
    If $hSearch = -1 Then Return
    
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
        $sFilePath = $sPath & "\" & $sFile
        If StringInStr(FileGetAttrib($sFilePath), "D") <> 0 Then
            ConsoleWrite($sSpacer & $sFilePath & @LF)
            RecurseDirs($sFilePath, $sExt, $sSpacer & "  ")
        Else
            If StringRegExp($sFile, "(?i)\." & $sExt & "\z", 0) Then
                ; Add File Read/Write processing here
                ConsoleWrite($sSpacer & $sFilePath & @LF)
            EndIf
        EndIf
    WEnd
EndFunc
Edited by zorphnog

Share this post


Link to post
Share on other sites

I may be mistaken but I believe if you tell _filewritetoline to write to line -1 it automatically writes to the last line in the file.


Giggity

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0