Jump to content

Move all content from folderA to folderB


Go to solution Solved by javiwhite,

Recommended Posts

Hey David,

Ahh, sorry, i didn't realise there were subfolders.

I don't believe either function will move both folders and files.

This is a quick workaround though:

$source = 'C:\folderA'
$dest = 'C:\folderB'


FileMove($source & "\*.*", $dest, 1) ;Move all source folder files first
$hSearch = FileFindFirstFile($Source & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch) ;get next file name
    if @ERROR then exitloop ;No more files
    DirCreate($Dest & "\" & $hFilename) ;Recreate file in new location
    FileMove($Source & "\" & $hFilename & "\*.*", $Dest & "\" & $hFilename,1) ;move all contents to new locations, whilst creating the new folder
WEnd

thanks

Javi

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Link to comment
Share on other sites

Hey David,

Ahh, sorry, i didn't realise there were subfolders.

I don't believe either function will move both folders and files.

This is a quick workaround though:

$source = 'C:\folderA'
$dest = 'C:\folderB'


FileMove($source & "\*.*", $dest, 1) ;Move all source folder files first
$hSearch = FileFindFirstFile($Source & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch) ;get next file name
    if @ERROR then exitloop ;No more files
    DirCreate($Dest & "\" & $hFilename) ;Recreate file in new location
    FileMove($Source & "\" & $hFilename & "\*.*", $Dest & "\" & $hFilename,1) ;move all contents to new locations, whilst creating the new folder
WEnd

thanks

Javi

 

Hi Javi

Okay this time it came closer.

What it did:

- It moved all files from A to B

- It copied all subfolders and the files within them from A to B (but the subfolder still exists in A, without the files though)

- Folders + their files inside a subfolder where not copied/moved

Man I didn't realize that this would be so complicated in Autoit! :)

Link to comment
Share on other sites

  • Solution

It seems i might have overcomplicated the situation somewhat.

Try this:

$source = 'C:\folderA'
$dest = 'C:\folderB'


FileMove($source & "\*.*", $dest, 1) ;Move all source files first
$hSearch = FileFindFirstFile($Source & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    if @ERROR then exitloop ;No more files
    DirMove($source & "\" & $hFilename, $Dest,1);move subdir and all contents to new location
WEnd

Thanks

Javi

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Link to comment
Share on other sites

What about this:

Global $iSuccess = _XCopy_DirMove(@ScriptDir & "\Test", @ScriptDir & "\Test2")
ConsoleWrite("Success: " & $iSuccess & @LF)


Func _XCopy_DirMove($sSourcePath, $sTargetPath)
    Local $iErrorlevel = RunWait('xcopy "' & $sSourcePath & '" "' & $sTargetPath & '" /E /Y /R /H /K /I', "", @SW_HIDE)
    If $iErrorlevel = 0 Then ; Copying without error
        DirRemove($sSourcePath, 1)
    EndIf
    Return SetError($iErrorlevel, 0, $iErrorlevel == 0)
EndFunc   ;==>_XCopy_DirMove

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

It seems i might have overcomplicated the situation somewhat.

Try this:

$source = 'C:\folderA'
$dest = 'C:\folderB'


FileMove($source & "\*.*", $dest, 1) ;Move all source files first
$hSearch = FileFindFirstFile($Source & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    if @ERROR then exitloop ;No more files
    DirMove($source & "\" & $hFilename, $Dest,1);move subdir and all contents to new location
WEnd

Thanks

Javi

 

That worked!

Thank you so much Javi! :)

 

@ funkey

I'm sure that works too. Two different ways to accomplish the same thing :)

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