Jump to content

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


Recommended Posts

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?

Link to comment
Share on other sites

  • Moderators

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.

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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")

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • Moderators

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. 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

@William112792

Please use code tags, read wiki page here.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor 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 APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

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 * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

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 * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

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) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

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 *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"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: 2023-04-24

Link to comment
Share on other sites

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

  • Moderators

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..

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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
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...