Jump to content

Renaming a file


jben
 Share

Recommended Posts

Hey everyone. I'm going through a hard time with getting some code to work at the moment.

Is it possible to rename a file in AutoIT. What i'm trying to achieve is to rename anything that has the .zip extension to File.zip....Presumably using something like *.zip

There will only ever be 1 zip file in the folder..."c:\something\myzip.zip"

However the name of this zip file could possibly change over time, therefore I'm just wondering if theres a way to rename the file so its always the same when I use my autoIT program

If this is possible then i'm thinking of running the code at the beginning of the project so this will ensure that the filename is correct before proceeding.

Edited by jben
Link to comment
Share on other sites

Hey everyone. I'm going through a hard time with getting some code to work at the moment.

Is it possible to rename a file in AutoIT. What i'm trying to achieve is to rename anything that has the .zip extension to File.zip....Presumably using something like *.zip

There will only ever be 1 zip file in the folder..."c:\something\myzip.zip"

However the name of this zip file could possibly change over time, therefore I'm just wondering if theres a way to rename the file so its always the same when I use my autoIT program

If this is possible then i'm thinking of running the code at the beginning of the project so this will ensure that the filename is correct before proceeding.

Have a look at FileFindFirstFile and FileFindNextFile, I think they will be useful for your problem.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Hey everyone. I'm going through a hard time with getting some code to work at the moment.

Is it possible to rename a file in AutoIT. What i'm trying to achieve is to rename anything that has the .zip extension to File.zip....Presumably using something like *.zip

There will only ever be 1 zip file in the folder..."c:\something\myzip.zip"

However the name of this zip file could possibly change over time, therefore I'm just wondering if theres a way to rename the file so its always the same when I use my autoIT program

If this is possible then i'm thinking of running the code at the beginning of the project so this will ensure that the filename is correct before proceeding.

With the caveat that this example is distinctly specific to your comment that There will only ever be 1 zip file in the folder, here's a relatively short way to handle it:

#include <File.au3>
$targetDir = "C:\Test\"

$aZipList = _FileListToArray($targetDir,"*.zip",1)
If IsArray($aZipList) Then
    FileMove($targetDir &$aZipList[1],$targetDir &"File.zip")
EndIf

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Link to comment
Share on other sites

Hey...Got another question regarding the code.

Maybe you can see the problem. Basically this works fine. The only issue is the last folder which is the date. This is the only hurdle I need to over come. Is there any way of checking at C:\TEMP\2005724 for possible .zip files in any directorys within this folder.

The code is ideal in the respect that it works very well. Its just that last folder that changes (as you can see its the date)..

Any ideas?...Thanks

$targetDir = "C:\TEMP\2005724\14_04_2008" 
$aZipList = _FileListToArray($targetDir,"\*.zip",1)
If IsArray($aZipList) Then
    FileMove($targetDir &$aZipList[1],"C:\TEMP\2005724\14_04_2008\DATA_CHILD.zip")

EndIf

Edited by jben
Link to comment
Share on other sites

Go to this page and about half way down you will find __FldrListToArray()

Folder Funcs

Call it like so.

$F_Array = __FldrListToArray("C:\TEMP\2005724", 0)

Then you can use the code you already have for getting the files in each folder or use

$rFile = ""
For $I = 1 To Ubound($F_Array) -1
   If StringRight($F_Array, 1) <> "\" Then $F_Array[$I] &= "\"
   $sFile = FileFindFirstFile($F_Array[$I], "*.zip")
   If Not @Error Then
      $nFile = FileFindNextFile($sFile)
      If @Error Then ExitLoop
      $rFile &= $F_Array[$I] & $nFile & "|"
   EndIf
Next
 If $rFile Then $File_List = StringSplit(StringTrimRight($rFile, 1), "|")

That will give you an array of all the files that you want to rename using FileMove()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thanks. Seem to have a little bit of an issue, because I can't get this to work..

just tried this:

$F_Array = __FldrListToArray("C:\TEMP\29944M_10386\", 0)


$targetDir = $F_Array

$aZipList = _FileListToArray($targetDir,".zip",0)
If IsArray($aZipList) Then
    FileMove($targetDir &$aZipList[1],$targetDir &"DATA_CHILD.zip")
EndIf

Sorry if i'm being silly about this, just getting myself all confused...

Edited by jben
Link to comment
Share on other sites

Because __FldrListToArray returns (just like it indicates) an array

$F_Array = __FldrListToArray("C:\TEMP\29944M_10386\", 0)

For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I]

$aZipList = _FileListToArray($targetDir,".zip",0)
If IsArray($aZipList) Then
    FileMove($targetDir &$aZipList[1],$targetDir &"DATA_CHILD.zip")
EndIf

Next

Edit: I forgot to change one part of your syntax

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

hmm interesting.

i get this error ==> Unknown function name.:

do i need to create an au3 file and include this at the start of the code, not sure?

thanks

just copy the function into your script but make sure that you have the function name correct. There are 2 underscores at the beginning of the function name. If you still have a problem then post back the entire error message. Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

ahhh i solved it...this bit of code was in-correct

$aZipList = _FileListToArray($targetDir,"*.zip",0)

the * was missing.

Thanks....One last question. Is there any way I can keep your code external to my project. Trying to keep it as simple as possible..

Or maybe its possible to just use a section of your code that I require. Because as it stands I've just pasted the whole lot which is rather beafy (pardon my wording)...

Edited by jben
Link to comment
Share on other sites

This is what I pasted into my project

;===============================================================================
; Function Name:    __FldrListToArray()
; Description:    Recursivly find the folders in a given path
; Syntax:          __FldrListToArray($szRoot, $nFlag = 1 )
; Parameter(s):  $szRoot - The base folder
;                  $iRecurs - If 1 (default) search is recursive
; Requirements:
; Return Value(s):  Success - An array of the folders with paths
;                  Failure - None
; Author(s):        George (GEOSoft) Gedye
; Notes:
;===============================================================================

Func __FldrListToArray($szRoot, $iRecurs = 1 )
    If StringRight($szRoot, 1) <> '\' Then $szRoot &= '\'
    Local $szReturn = '',  $szBuffer = '', $szPathlist = '*', $oRoot = $szRoot & '*'
        $Hfile = FileFindFirstFile ($szRoot &'*')
        If $Hfile >= 0 Then
            $szBuffer = FileFindNextFile ($Hfile)
            While NOT @Error
                If Not StringInStr($szBuffer, '.') Then $szPathlist &= $szRoot & $szBuffer & "\*"
                $szBuffer = FileFindNextFile ($Hfile)
            Wend
            FileClose ($Hfile)
        EndIf
        $szReturn = $szPathList

    If $iRecurs = 1 Then
        $szPathList = StringTrimLeft($szPathlist, 1)
        $szRoot = StringLeft($szPathList, StringInStr($szPathlist, '*') -1)
        While 1
            $hFile = FileFindFirstFile ($szRoot & '*')
            If $hFile >= 0 Then
                $szBuffer = FileFindNextFile ($Hfile)
            While NOT @Error
                If Not StringInStr($szBuffer, '.') Then
                    $szPathlist &= $szRoot & $szBuffer & "\*"
                    $szReturn &= $szRoot & $szBuffer & "\*"
                EndIf
                $szBuffer = FileFindNextFile ($Hfile)
            Wend
            FileClose($hFile)
            $szPathList = StringReplace($szPathList, $szRoot, '')
            EndIf
            If $szPathList == '*' Then ExitLoop
            $szPathlist = StringTrimLeft ($szPathlist, 1)
            $szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\"
            $szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1)
        Wend
    EndIf
    If StringLeft($szReturn, 1) = '*' Then $szReturn = StringTrimLeft($szReturn, 1)
    $szReturn = StringReplace($szReturn, '\\', '\')
    If StringRight($szReturn, 1) = '*' Then $szReturn = StringTrimRight($szReturn,1)
    $szReturn = StringSplit($oRoot & $szReturn,'*')
    If $szReturn = '*' or $szReturn = '' Then Return 0
; _ArraySort($szReturn);; << uncomment and #include <array.au3> to return a sorted array
    Return $szReturn
EndFunc  ;<==> __FldrListToArray()

;;<<==  Helper functions  ==>>
;;
Func _FldrDate($Dt, $Ext_Date, $Mth_AsText, $T_Txt)
   Local $Tday = 0
   If $Ext_Date = 0 Then Return $Dt
   Local $Disp_Mon = StringSplit('January|February|March|April|May|June|July|August|September|October|November|December', '|')
   Local $y = StringLeft($Dt, 4)
   Local $m = StringMid($Dt,5,2)
   Local $d = StringMid($Dt, 7, 2)
   If $y = @Year AND $m = @Mon AND $d = @Mday Then $Tday = 1
   Local $t = StringMid($Dt, 9)
   $t = StringLeft($t,2) & ':' & StringMid($t, 3,2) & ':' & StringRight($t, 2)
   If StringLeft( $t, 2) < 12 Then
      $t &= ' am'
   Else
      $t = StringLeft($t, 2) - 12 & StringMid($t, 3)
      $t &= ' pm'
   EndIf
   If $Tday = 1 AND $Mth_AsText = 1 AND $T_Txt = 1 Then Return 'Today at ' & $t
   If $Mth_AsText = 1 Then $m = $Disp_Mon[$m]
   Return $m & ' ' & $d & ' ' & $y & ' at ' & $t
EndFunc   ;<===> _FldrDate()
Link to comment
Share on other sites

just pasted the code in, thats solved the error problem

Only issue I have now is that the .zip file is not renamed....just going to have a look..

Use a MsgBox to check your paths.

$F_Array = __FldrListToArray("C:\TEMP\29944M_10386\", 0)

For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I]

$aZipList = _FileListToArray($targetDir,".zip",0)
If IsArray($aZipList) Then
MsgBox(0, "TEST PATH", $targetDir &$aZipList[1] & @CRLF & $targetDir &"DATA_CHILD.zip")
FileMove($targetDir &$aZipList[1],$targetDir &"DATA_CHILD.zip")
EndIf

Next
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Is it possible for me to cut down the function code I pasted into my project, or do I need all of that?.

Not sure what you mean about messagebox....For error checking I guess..Just wasn't sure if your also referring to cutting the function code down by doing this..

Link to comment
Share on other sites

Just trying to keep this as simple as possible...Not sure if I need the whole of that function code, but to be honest I have no idea how it works :-)

Can I just include the relevant parts of the function?..

it works now though, which is cool...much appreciated..

thanks

Edited by jben
Link to comment
Share on other sites

Is it possible for me to cut down the function code I pasted into my project, or do I need all of that?.

Not sure what you mean about messagebox....For error checking I guess..Just wasn't sure if your also referring to cutting the function code down by doing this..

The MsgBox is only for error checking of the folder paths as I showed in the last post.

That function can be trimmed but you will not gain much (if anything) by doing so. You can remove the following If statement but you won't be able to use the function recursivly later on.

If $iRecurs = 1 Then
    $szPathList = StringTrimLeft($szPathlist, 1)
    $szRoot = StringLeft($szPathList, StringInStr($szPathlist, '*') -1)
    While 1
        $hFile = FileFindFirstFile ($szRoot & '*')
        If $hFile >= 0 Then
        $szBuffer = FileFindNextFile ($Hfile)
        While NOT @Error
            If Not StringInStr($szBuffer, '.') Then
                $szPathlist &= $szRoot & $szBuffer & "\*"
                $szReturn &= $szRoot & $szBuffer & "\*"
            EndIf
            $szBuffer = FileFindNextFile ($Hfile)
        Wend
        FileClose($hFile)
        $szPathList = StringReplace($szPathList, $szRoot, '')
        EndIf
        If $szPathList == '*' Then ExitLoop
        $szPathlist = StringTrimLeft ($szPathlist, 1)
        $szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\"
        $szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1)
    Wend
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Got it sorted....Thanks alot for your help :-)

glad to hear. You could go back to that page and just click download at the bottom of the page but I would wait until tomorrow because right now I have a function to add and another to update.

I just got it updated so you can download the whole UDF anytime.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...