Jump to content

Copy File from 1 location to another by using VB script


Recommended Posts

Hello 

Trying to add below code in my main script but this is not functioning can you advise what else i need to do?

 

	Dim fn As String
    Const myDir As String = "C:\AR\" '<- alter here
    Const newFolder As String = "C:\AR1\" '<- alter here
    fn = Dir(myDir & "AR_*.xlsm")
    Do While fn <> ""
        FileCopy myDir & fn, newFolder & Replace(fn, "AR", "AI")
        fn = Dir
    Loop

 

Edited by sumandevadiga
I did some error while pasting code
Link to comment
Share on other sites

So are you requesting help in regards to how to get an autoit script to work or a VB script?  If it is the latter, I am afraid you have the wrong forums...  However, if it is the former, please let me know, and I would be glad to help.

Link to comment
Share on other sites

One way...line by line conversion from vbscript to autoit

#include <File.au3>
#include <FileConstants.au3>

;Dim fn As String
Local $fn
;Const myDir As String = "C:\AR\" '<- alter here
Const $myDir = "C:\AR\"
;Const newFolder As String = "C:\AR1\" '<- alter here
Const $newFolder = "C:\AR1\"
;fn = Dir(myDir & "AR_*.xlsm")
$fn = _FileListToArrayRec($myDir, "AR_*.xlsm", $FLTAR_FILES, $FLTAR_RECUR)
;Do While fn <> ""
For $sFile in $fn
;    FileCopy myDir & fn, newFolder & Replace(fn, "AR", "AI")
    FileCopy($myDir & $sFile, $newFolder & StringReplace($sFile, "AR", "AI"), $FC_OVERWRITE + $FC_CREATEPATH)
;    fn = Dir
;Loop
Next

 

Edited by spudw2k
Link to comment
Share on other sites

4 hours ago, MattHiggs said:

So are you requesting help in regards to how to get an autoit script to work or a VB script? 

Good point...sorry we jumped ahead

Link to comment
Share on other sites

Just now, spudw2k said:

Good point...sorry we jumped ahead

No worries man.  From the syntax that was posted, it looked more like Visual basic than Autoit, which is what prompted the question.  However, I don't have....well....any.....experience with Visual basic, so I wouldn't have been able to help if that were the case.  But at least he has the answer now if he needed help with autoit:P:P

Link to comment
Share on other sites

@sumandevadiga,

You need to put the filename to "true" to work with your code "objFSO.CopyFile" will not work with wildcards. Except you have it this way:

Dim fso As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim objFSO, objFolder, strFile
    
  FromPath = "C:\AR\"
  ToPath = "C:\AR1\"
  
   If fso.FolderExists(FromPath) = False Then
     MsgBox "File doesn't exist. Not found!"
    Exit Sub
   End If
  
  For Each strFile In objFolder.Files
    If objFSO.GetExtensionName(strFile.Name) = "xlsm" Then
        objFSO.CopyFile strFile.Path, ToPath
    End If
  Next

Not tested, from my old VBscript. Hope you can get some idea and hope it helps.

If you want to use AutoIt, then you must follow Subz and spudw2k's posted above.

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Hello Kickstarter15,

Sorry for the delay reply, this is in VB script not Autoit, however i have tried your suggested code, this is not giving any output or result.

Dim fso As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim objFSO, objFolder, strFile
    
  FromPath = "C:\AR\"
  ToPath = "C:\AR1\"
  
   If fso.FolderExists(FromPath) = False Then
     MsgBox "File doesn't exist. Not found!"
    Exit Sub
   End If
  
  For Each strFile In objFolder.Files
    If objFSO.GetExtensionName(strFile.Name) = "xlsm" Then
        objFSO.CopyFile strFile.Path, ToPath
    End If
  Next

 

Link to comment
Share on other sites

@sumandevadiga,

Have you tried changing it? the logic is already in that script. Anyway, I have it this way:^_^

Set objFSO = CreateObject("Scripting.FileSystemObject")

    FromPath = "C:\AR\"
    ToPath = "C:\AR1\"

  Set objFolder = objFSO.GetFolder(FromPath)

  Set colFiles = objFolder.Files

  For Each objFile In colFiles
  strFileName = objFile.Name

      If objFSO.GetExtensionName(strFileName) = "xlsm" Then
        objFSO.CopyFile FromPath & objFile.Name, ToPath
      End If
  Next

NOTE: As what 232Showtime stated above, this is not a vbscript forum. So better to ask what does the forum is for.;)

Now, for AutoIt code this is how it was done:

#include <FileConstants.au3>
FromPath = "C:\AR\"
ToPath = "C:\AR1\"
FileCopy(FromPath & "*.xlsm", ToPath, $FC_OVERWRITE + $FC_CREATEPATH)

 

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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

×
×
  • Create New...