Jump to content
William112792

HELP - Trying to Find, Move, and Rename a File

Recommended Posts

William112792

Ok so I'm trying to find a file after it is generated. The file will look like MERGEFILE052.CSV however the 3 numbers 052 will change so I need to find the file with a wildcard like MERGEFILE*.CSV and I saw something about single wildcards which would make it MERGEFILE???.CSV and the file will be located at the root of the C:\ drive

Once the file has been found I wanted to move it into a new folder within a Reports folder so the location would look like C:\Reports\Reports052515-053115\ with a new name so the end result will be C:\Reports\Reports052515-053115\BV.CSV

So far this is what I have (excluding the instructions to run the program needed to generate the file):

;Set Include files for AutoIT
#include <GuiToolbar.au3>
#include <GuiListBox.au3>
#include <GuiConstantsEx.au3>

Local $QS1ClientClass = "[CLASS:ThunderRT6MDIForm]"
Local $QS1handle = WinGetHandle($QS1ClientClass)
Local $hToolbar

Local $strUserID = "UserName"
Local $strUserPassword = "Password1"
Local $strDate = "052515-053115"
Local $strABB = "BV"

 ;Change and move Mergefile name
FileMove( "C:\MERGEFILE*.CSV", "C:\Reports\Reports" & $strDate & "\" & $strABB & ".CSV")

I don't understand how to find the file or if the FileMove will even do what I'm trying to get it to do. Could someone help me please?

Share this post


Link to post
Share on other sites
JLogan3o13

How many of these MergeFiles will exist? Can you do something like this?

#include <File.au3>

Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.csv", $FLTA_FILES, True)

If there is only 1 of this type, then $aFiles[1] will be your full path to the file.

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
William112792

There will only be 1 at a time, as long as it finds the file and moves it.

Share this post


Link to post
Share on other sites
JLogan3o13

Something like this, then:

#include <File.au3>

Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.csv", $FLTA_FILES, True)
    If $aFiles[0] <> 0 Then FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & "\" & $strABB & ".CSV")

 

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
William112792

That looks like what I'm looking for but what is the $FLTA_FILES for and wouldn't I need a variable set for it?

Share this post


Link to post
Share on other sites
JLogan3o13

That is a constant that tells the FileListToArray function to look at files only, not folders. Look at the function in the help file for more information. 

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
William112792

Thank you so much!

Share this post


Link to post
Share on other sites
William112792

I get the response Line 118

If $aFiles[0] <> 0 Then FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & $strABB & ".CSV")
If $aFiles^ ERROR

Do I need to remove [0] and [1] every time I try using that in the code I get errors or am I missing something else?

Sorry I'm not too good at coding If Then Statements.

Share this post


Link to post
Share on other sites
JLogan3o13

It is stating $aFiles doesn't exist. How about showing us your full code?

You can use this syntax instead:

#include <File.au3>

Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.csv", $FLTA_FILES, True)
    If IsArray($aFiles) Then FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & "\" & $strABB & ".CSV")

Just realize, if you have multiple MERGEFILES in the directory, you could get some unwanted behavior.

Edit: You could always check for both:

#include <File.au3>

Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.csv", $FLTA_FILES, True)
    If IsArray($aFiles) Then
        If $aFiles[0] = 1 Then
            FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & $strABB & ".CSV")
        Else
            MsgBox(0, "", "There are multiple MERGEFILES in the directory!")
        EndIf
    EndIf

 

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
William112792

The entire code is:

;Set Include files for AutoIT
#include <GuiToolbar.au3>
#include <GuiListBox.au3>
#include <GuiConstantsEx.au3>
#include <File.au3>


Local $QS1ClientClass = "[CLASS:ThunderRT6MDIForm]"
Local $QS1handle = WinGetHandle($QS1ClientClass)
Local $hToolbar

Local $strUserID = "UserName"
Local $strUserPassword = "Password1"
Local $strDate = "052515-053115"
Local $strABB = "BV"

;Login to QS/1 Bakersville

Do
 if FileExists("C:\QS1\Qs1com.exe") Then
  Run("C:\QS1\Qs1com.exe", "C:\QS1")
 ElseIf FileExists("D:\QS1\Qs1com.exe") Then
  Run("D:\QS1\Qs1com.exe", "D:\QS1")
 Else
  MsgBox(0,"QS/1 Reporter Error", "Unable to find QS/1 Client.")
  Exit
 EndIf
 WinWait("QS/1")
 WinWait("Connect")

 ControlCommand("Connect", "", "ListBox1", "SelectString", 'Bakersville')
 Send("!s")
 Sleep(1000)
 WinWait("QS/1: ")

Until WinActive ("QS/1: ")

 $strLoginControlString = ""
 While ($strLoginControlString <> "NRX")
  ControlSetText("QS/1: ", "", "Edit1", "NRX")
  $strLoginControlString = ControlGetText("QS/1: ", "", "Edit1")
 WEnd
 ControlSetText("QS/1: ", "", "Edit1", "")
 ControlSend("QS/1: ", "", "Edit1", "NRX{ENTER}")
 Sleep(1000)
 WinWaitNotActive("QS/1: ", "", 5)


 If WinExists("QS/1 Workstation Updates") Then
  WinActivate("QS/1 Workstation Updates")
  Sleep(250)
  ControlClick("QS/1 Workstation Updates", "", "Button3")
  Sleep(250)
  ControlClick("QS/1 Workstation Updates", "", "Button1")
 EndIf

 WinWait("NRx Security")
 ControlSetText("NRx Security", "", "ThunderRT6TextBox8", $strUserID)
 ControlSetText("NRx Security", "", "ThunderRT6TextBox7", $strUserPassword)
 ControlClick("NRx Security","","ThunderRT6CommandButton4")
 WinWaitClose("NRx Security")

 ;Set Printer to Print Screen
 WinWait("NRx")
 WinActivate("NRx")
 Sleep(1000)
 Send("!fs")
 Sleep(1000)
 WinWait("Print Setup")
 ControlCommand("Print Setup", "", 1228, "Check", "")
 ControlClick("Print Setup", "", "Button1")
 Sleep(1000)

 ;Open Reports and open RX-NET EXPORT
 WinActivate("NRx")
 Sleep(1000)
 Send("!rzs")
 Sleep(1000)
 ControlSetText("NRx", "", "ThunderRT6TextBox", "RX-NET EXPORT")
 Sleep(1000)
 Send("RX-NET EXPORT")
 Sleep(1000)
 Send("{ENTER}")
 Sleep(1000)
 Send("{f3}")

 ;Set required Values
 WinActivate("NRx")
 Sleep(1000)
 Send("{TAB}")
 Sleep(1000)
 Send("{TAB}")
 Sleep(1000)
 Send("{ENTER}")
 Sleep(1000)
 Send($strDate)
 Sleep(1000)
 Send("{ENTER}")
 Sleep(1000)
 ;Select Next on Select Options
 ControlClick("NRx", "","ThunderRT6CommandButton13")
 Sleep(1000)
 ;Select Finish on Sort Options
 ControlClick("NRx", "","ThunderRT6CommandButton13")
 Sleep(6000)
 ;Select Ok on Change Printed Fields
 ControlClick("NRx", "","ThunderRT6CommandButton17")
 Sleep(6000)
 ;Select Finish on Print Option Values
 ControlClick("NRx", "","ThunderRT6CommandButton13")
 Sleep(3000)
 ;Select Finish to run the RX-NET EXPORT Report
 ControlClick("NRx", "","ThunderRT6CommandButton13")
 Sleep(10000)
 WinClose( "NRx")

 ;Change and move Mergefile name
Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.CSV", $FLTA_FILES, TRUE)
 If $aFiles[0] <> 0 Then FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & "\" & $strABB & $strDate & ".CSV")

This better?

Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites
mLipok

@William112792

Please use code tags, read wiki page here.

  • Like 1

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-03-17

Share this post


Link to post
Share on other sites
William112792

I tried using the If Then Else you posted and I get no response. Did I mistype something? Or could it be a problem with Autoit on my computer?

---[Edit]----

;Change and move Mergefile name
Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.CSV", $FLTA_FILES, TRUE)
 If IsArray($aFiles) Then
  If $aFiles[0] = 1 Then
   FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & "\" & $strABB & $strDate & ".CSV")
  Else
   MsgBox(0, "", "There are multiple MERGEFILES in the directory!")
  EndIf
 EndIf

 

Edited by William112792

Share this post


Link to post
Share on other sites
JLogan3o13

Try this...

#include <File.au3>

Local $aFiles = _FileListToArray("C:\", "MERGEFILE*.csv", $FLTA_FILES, True)
    If IsArray($aFiles) Then
        If $aFiles[0] = 1 Then
            FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & $strABB & ".CSV")
        Else
            MsgBox(0, "", "There are multiple MERGEFILES in the directory!")
        EndIf
    Else
        MsgBox(0, "", "No MERGEFILE files found!")
    EndIf

 

Edited by JLogan3o13
Forum software mucked up my code block..

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
William112792

That worked perfectly. Thank you so much!

Share this post


Link to post
Share on other sites
William112792

Yeah I see that now, thank you everyone for the help and JLogan3o13 for being patient with me to figure out how to do this.

Final Code:

;Set Include files for AutoIT

#include <GuiToolbar.au3>
#include <GuiListBox.au3>
#include <GuiConstantsEx.au3>
#include <File.au3>

Local $QS1ClientClass = "[CLASS:ThunderRT6MDIForm]"
Local $QS1handle = WinGetHandle($QS1ClientClass)
Local $hToolbar



Local $strUserID = "UserName"
Local $strUserPassword = "Password1"
Local $strDate = "052515-053115"
Local $strABB = "BV"

;Login to QS/1 Bakersville



Do
    if FileExists("C:\QS1\Qs1com.exe") Then
        Run("C:\QS1\Qs1com.exe", "C:\QS1")
    ElseIf FileExists("D:\QS1\Qs1com.exe") Then
        Run("D:\QS1\Qs1com.exe", "D:\QS1")
    Else
        MsgBox(0,"QS/1 Reporter Error", "Unable to find QS/1 Client.")
        Exit
    EndIf

    WinWait("QS/1")
    WinWait("Connect")

    ControlCommand("Connect", "", "ListBox1", "SelectString", 'Bakersville')
    Send("!s")
    Sleep(1000)
    WinWait("QS/1: ")

Until WinActive ("QS/1: ")

    $strLoginControlString = ""
    While ($strLoginControlString <> "NRX")
        ControlSetText("QS/1: ", "", "Edit1", "NRX")
        $strLoginControlString = ControlGetText("QS/1: ", "", "Edit1")
    WEnd

    ControlSetText("QS/1: ", "", "Edit1", "")
    ControlSend("QS/1: ", "", "Edit1", "NRX{ENTER}")
    Sleep(1000)
    WinWaitNotActive("QS/1: ", "", 5)


    If WinExists("QS/1 Workstation Updates") Then
        WinActivate("QS/1 Workstation Updates")
        Sleep(250)
        ControlClick("QS/1 Workstation Updates", "", "Button3")
        Sleep(250)
        ControlClick("QS/1 Workstation Updates", "", "Button1")
    EndIf



    WinWait("NRx Security")
    ControlSetText("NRx Security", "", "ThunderRT6TextBox8", $strUserID)
    ControlSetText("NRx Security", "", "ThunderRT6TextBox7", $strUserPassword)
    ControlClick("NRx Security","","ThunderRT6CommandButton4")
    WinWaitClose("NRx Security")

    ;Set Printer to Print Screen
    WinWait("NRx")
    WinActivate("NRx")
    Sleep(1000)
    Send("!fs")
    Sleep(1000)
    WinWait("Print Setup")
    ControlCommand("Print Setup", "", 1228, "Check", "")
    ControlClick("Print Setup", "", "Button1")
    Sleep(1000)

    ;Open Reports and open RX-NET EXPORT
    WinActivate("NRx")
    Sleep(1000)
    Send("!rzs")
    Sleep(1000)
    ControlSetText("NRx", "", "ThunderRT6TextBox", "RX-NET EXPORT")
    Sleep(1000)
    Send("RX-NET EXPORT")
    Sleep(1000)
    Send("{ENTER}")
    Sleep(1000)
    Send("{f3}")

    ;Set required Values
    WinActivate("NRx")
    Sleep(1000)
    Send("{TAB}")
    Sleep(1000)
    Send("{TAB}")
    Sleep(1000)
    Send("{ENTER}")
    Sleep(1000)
    Send($strDate)
    Sleep(1000)
    Send("{ENTER}")
    Sleep(1000)
    ;Select Next on Select Options
    ControlClick("NRx", "","ThunderRT6CommandButton13")
    Sleep(1000)
    ;Select Finish on Sort Options
    ControlClick("NRx", "","ThunderRT6CommandButton13")
    Sleep(6000)
    ;Select Ok on Change Printed Fields
    ControlClick("NRx", "","ThunderRT6CommandButton17")
    Sleep(6000)
    ;Select Finish on Print Option Values
    ControlClick("NRx", "","ThunderRT6CommandButton13")
    Sleep(3000)
    ;Select Finish to run the RX-NET EXPORT Report
    ControlClick("NRx", "","ThunderRT6CommandButton13")
    Sleep(10000)
    WinClose( "NRx")

    ;Create Directory
    DirCreate("C:\Reports\Reports" & $strDate & "\")

    ;Change and move Mergefile name
    ;The i is not present in the filename upon creation so filename is MERGEFLE???.CSV
Local $aFiles = _FileListToArray("C:\", "MERGEFLE*.csv", $FLTA_FILES, True)
    If IsArray($aFiles) Then
        If $aFiles[0] = 1 Then
            FileMove($aFiles[1], "C:\Reports\Reports" & $strDate & "\" & $strABB & $strDate & ".CSV")
        Else
            MsgBox(0, "", "There are multiple MERGEFLES in the directory!")
        EndIf

    Else
        MsgBox(0, "", "No MERGEFLE files found!")
    EndIf

 

Edited by William112792

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

×