Jump to content

script that will copy and past files


Recommended Posts

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

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
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

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

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

;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?

Link to comment
Share on other sites

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

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

@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?

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

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

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

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

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

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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.

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

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