Sign in to follow this  
Followers 0

script that will copy and past files

15 posts in this topic

Posted

I am looking for autoit script, that will copy and paste files from a folder

into another folder. Does anyone have a script for this?

Share this post


Link to post
Share on other sites



Posted

Hi,

have look at helpfile or have a search over forum:

FileCopy ()

DirCopy ()

_FileListToArray ()

Try start coding. If you still have problems, come back with some code snippet.

;-))

Stefan

Share this post


Link to post
Share on other sites

Posted (edited)

I am trying to take all of the mp3 files from one folder, and put them in another

would this work.

$programfiles = ("c:/program files/ copyall ".mp3")

$programfilesmusic = ("c:/program files/music/ pastall ".mp3")

$copy = $programfiles

$past = $programfilesmusic

copyfile(($copy))

pastfile(($past))

I know this isn't the right code. But does anybody see what I am wanting to do?

Edited by program builder

Share this post


Link to post
Share on other sites

Posted (edited)

You want to be looking at something more like

$programfiles = "c:/program files/"

$programfilesmusic = "c:/program files/music/"

create a list of files of type ".mp3" inside $programfiles

loop through each of the files in the list of files found while copying them from $programfiles each filename into $programfilesmusic each filename

Edited by Yoriz

Share this post


Link to post
Share on other sites

Posted (edited)

This is just intended to get you started down the right path.

First of all it's backslash "\" in a folder path, not forward slash "/". It also helps to look up the functions in the help file to see what is actually happening.

$sCopyFrom = "C:\Some\Folder\"
$sCopyTo = "C:\Some\Other\Folder\"

$hSearch = FileFindFirstFile($sCopyFrom & "*.mp3")
If $hSearch <> -1 Then
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @Error Then ExitLoop
        If StringInStr(FileGetAttrib($sCopyFrom & $sFile), "D") Then
            DirCopy($sCopyFrom & $sFile, $sCopyTo & $sFile)
        Else
            FileCopy($sCopyFrom & $sFile, $sCopyTo & $sFile)
        EndIf
    WEnd
EndIf
Edited by GEOSoft

Share this post


Link to post
Share on other sites

Posted

;I tryed this, but it didn't work. I tryed it with txt

;I put 2 text files into the text 1 and tryed to copy and

;past them into the text 2 folder

;here is the code I used

$sCopyFrom = "C:\program files\text 1\"

$sCopyTo = "C:\program files\text 2\"

$hSearch = FileFindFirstFile($sCopyFrom & "*.txt")

If $hSearch <> -1 Then

While 1

$sFile = FileFindNextFile($hSearch)

If @Error Then ExitLoop

If StringInStr(FileGetAttrib($sCopyFrom & $sFile), "D") Then

DirCopy($sCopyFrom & $sFile, $sCopyTo & $sFile)

Else

FileCopy($sCopyFrom & $sFile, $sCopyTo & $sFile)

EndIf

WEnd

EndIf

;could someone tell me why it isn't working, when I run it?

Share this post


Link to post
Share on other sites

Posted (edited)

You could also use this one:

$sTarget = @ScriptDir & "\" & "temp"
$sSource = @ScriptDir
$sExt    = "txt"

MoveFiles($sSource, $sTarget, $sExt)

Func MoveFiles($sSource, $sTarget, $sExt = ".*")
  If Not StringRegExp($sSource, "\\\z") Then $sSource &= "\" ; Append missing '\' to $sSource if needed
  If Not StringRegExp($sTarget, "\\\z") Then $sTarget &= "\" ; Append missing '\' to $sTarget if needed
  FileChangeDir($sSource)
  $hSearch = FileFindFirstFile("*.*")
  $hFile = FileFindNextFile($hSearch)
  While Not @error
    If @extended Then ; $hFile is a directory
      MoveFiles($sSource & $hFile, $sTarget, $sExt) ; Scan the subdirectory
    Else              ; $hFile is a file
      FileChangeDir($sSource)
      If StringRegExp($hFile, "(?i)\." & $sExt & "\z") Then FileCopy($hFile, $sTarget, 8+1) ; Copy $hFile to $sTarget (create directory if needed and overwrite existing files -- flag 8+1)
    EndIf
    $hFile = FileFindNextFile($hSearch)
  WEnd
  FileClose($hSearch)
EndFunc

It is recursive -- it also moves files from subdirectories. If you don't want this, simply comment this line:

MoveFiles($sSource & $hFile, $sTarget, $sExt) ; Scan the subdirectory
Edited by dani

Share this post


Link to post
Share on other sites

Posted (edited)

Another way.

#Include <File.au3>

_FileCopyEx('C:\Program Files\text 1', 'C:\Program Files\text 2', '*.txt')

Func _FileCopyEx($sSrc, $sDest, $sMask = '*', $fReplace = 0)

    Local $FileList

    $FileList = _FileListToArray($sSrc, $sMask, 1)
    If Not @error Then
        For $i = 1 To $FileList[0]
            If (Not $fReplace) And (FileExists($sDest & '\' & $FileList[$i])) Then
                ContinueLoop
            EndIf
            If Not FileCopy($sSrc & '\' & $FileList[$i], $sDest & '\' & $FileList[$i], 9) Then
                Return 0
            EndIf
        Next
    EndIf
    $FileList = _FileListToArray($sSrc, '*', 2)
    If Not @error Then
        For $i = 1 To $FileList[0]
            If Not _FileCopyEx($sSrc & '\' & $FileList[$i], $sDest & '\' & $FileList[$i], $sMask, $fReplace) Then
                Return 0
            EndIf
        Next
    EndIf
    Return 1
EndFunc   ;==>_FileCopyEx
Edited by Yashied

Share this post


Link to post
Share on other sites

Posted

@dani

Using StringRegExp() the way you did to check for trailing backspaces seems like overkill ans abuse of StringRegExp(). A better method would be.

Func MoveFiles($sSource, $sTarget, $sExt = ".*")
  $sSource = StringRegExpReplace($sSource, "(.+)\\?$", "$1\\")
  $sTarget = StringRegExpReplace($sTarget, "(.+)\\?$", "$1\\")

Also did you test that code? I didn't but it looks all wrong, at least at a glance.

@program builder

Did you test to make sure the folders actually exist and that the script "sees" files in the source folder?

Hint: MsgBox(0, "Result", $sFile)

While 1

$sFile = FileFindNextFile($hSearch)

If @Error Then ExitLoop

MsgBox(0, "Result", $sFile)

WEnd

Did you check the help file for the functions I used to see what flags can be used with those functions?

Did you bother to look for Dir*() functions in the help file?

Did you look at the example code for the functions in the help file?

Share this post


Link to post
Share on other sites

Posted (edited)

@dani

Using StringRegExp() the way you did to check for trailing backspaces seems like overkill ans abuse of StringRegExp(). A better method would be.

Func MoveFiles($sSource, $sTarget, $sExt = ".*")
  $sSource = StringRegExpReplace($sSource, "(.+)\\?$", "$1\\")
  $sTarget = StringRegExpReplace($sTarget, "(.+)\\?$", "$1\\")

Also did you test that code? I didn't but it looks all wrong, at least at a glance.

Hi Geo. Why is it abuse of StringRegExp? I don't really follow, as your StringRegExpReplace essentially does the same, but replaces it at the same time. It might be nicer as it's one command, but I am really not sure if it is more efficient seeing how your code always does a string replacement, while mine only does that if needed. My regular expression is easier, thus faster I think? Not sure how it's an overkill :mellow:

Anyway, of course I tested it, works perfectly. It's a bit different from the examples in the Help File, I tend to use this While Not @error instead of If @error Then ExitLoop etc. Also, @extended is a bit shorter than your StringInStr combined with FileGetAttrib to test whether the file is a directory.

Edited by dani

Share this post


Link to post
Share on other sites

Posted (edited)

What I meant by overkill was that If StringRight($s_Source, 1) <> "\" Then $s_Source &= "\" does the same as you did with the SRE (and probably faster). The other was just an example. Of course in this case we are talking milli-micro-seconds difference which is virtually no difference at all, I never worry about time differences that are shorter than the blink of an eye unless they are being called in a loop.

What didn't look right had nothing to do with @Extended, it had to do with the positioning of the FileFindNextFile() line which is generally inside the loop.

EDIT: of course mine wasn't right either because I forgot to close the search handle and yours was correct. I was just brain dead.

Edited by GEOSoft

Share this post


Link to post
Share on other sites

Posted (edited)

I don't get this. The guy is asking for code to copy mp3 files fom one location to some other (nothing more) and you are posting some uber-almost-cooking-coffee-recursive-300-level-fancy scripts.

My advice to program builder is to read FileCopy related page in the help file, check for his permisions in the destination folder and change the nick name.

Edited by trancexx

Share this post


Link to post
Share on other sites

Posted (edited)

I would even go one steps further as far as an simple advice:

Just open the helpfile for a change.

For somebody that has shown much attitude it the past we can expect better than this .... right?

Edited by Jos

Share this post


Link to post
Share on other sites

Posted

I would even go one steps further as far as an simple advice:

Just open the helpfile for a change.

Hmmmm, where have I read that before?

Did you check the help file for the functions I used to see what flags can be used with those functions?

Did you bother to look for Dir*() functions in the help file?

Did you look at the example code for the functions in the help file?

As we know, that's where most of the answers are if people would take the time to look.

Share this post


Link to post
Share on other sites

Posted (edited)

True. I have once said most of the topics in General Help and Support would be obsolete if people would actually read the Help file. And I'm not talking about reading through it in 2 minutes but actually take the time, read some tutorials and practice some.

Nevertheless; @program builder: you have enough input now so I'd recommend to either

a | take one of the scripts provided and make sure to fully understand what they do and learn from them

or

b | take trancexx' suggestion and code something yourself.

Btw I wouldn't call the scripts posted an overkill as trancexx does, they do only copy files from one directory to another so .. :mellow: would be nice if they would make my cofee!

Edited by dani

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

  • Recently Browsing   0 members

    No registered users viewing this page.