Jump to content

File move


Recommended Posts

Hi all,

Is there a way of forcing 'FileFindFirsFile' to ignore a folder. See below, I need files of various types to be identified, but not folders. If I specify a file type, it works fine, however the *.* wildcard identifies the folder and the process obviously fails.

Thanks for any help

$sPath = "\\Sever\scripts\logs\FileTest\"
$sPath2 = "\\Server\scripts\logs\FileTest\Moved\"

;============================
;       Check for file      =
;============================

While 1
    $search = FileFindFirstFile("\\rpris001\scripts\logs\FileTest\*.*")
    $sFile = FileFindNextFile($search)
    _FileMove()
WEnd

;============================
;       Move file           =
;============================

Func _FileMove()
    FileMove($sPath & $sFile, $sPath2)
    Local $n = 1
    Do
        $F = FileExists($sPath & $sFile)
        $F1 = FileExists($sPath2 & $sFile)
        If $F1 = 1 Then
            $sFile2 = StringSplit($sFile, ".")
            $renamed = $sFile2[1] & "_Duplicated" & $n & "." & $sFile2[2]
            ;MsgBox(0, "", $renamed)
            FileMove($sPath & $sFile, $sPath2 & $renamed)
        EndIf
        $n = $n + 1
    Until $F = 0
EndFunc   ;==>_FileMove

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Hi there,

Try _FileListToArray instead.

Cheers

Old Scriptology

Visual Ping 1.8 - Mass Ping Program with export to txt delimited.

Desktop 2 RGB and YMCK - Pick a color in the desktop and get the RGB and YMCK code.

Desktop 2 RGB - Pick a color in the desktop and get the RGB code.

ShootIT 1.0 - Screen Capture full and partial screen

[font="'Arial Black';"]Remember Remember The Fifth of November.[/font]

Link to comment
Share on other sites

Thanks for this, it addresses one issue, however it creates another. When the file is moved, $search[2] is cleared, so on the next execution of the loop an error 'Array variable has incorrect number of subscripts or subscript dimension range exceeded' occurs and the scripts ends. Any ideas?

;============================
;       Check for file      =
;============================

While 1
$search = _FileListToArray($sPath)
$sFile = $search[2]
_FileMove()
WEnd

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Insert an error check in the loop

While 1
   $search = _FileListToArray($sPath)
   If IsArray($search) Then
        $sFile = $search[2]
        _FileMove()
   EndIf
WEnd

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thanks BrewMan. Unfortunately, it still returns the same error once the item is cleared ie when the last file is moved :unsure:

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Thanks BrewMan. Unfortunately, it still returns the same error once the item is cleared ie when the last file is moved :unsure:

This will identify whether it's a directory or it is not;

#include <file.au3> ;to use _FileListToArray() you need to include this UDF
$search = _FileListToArray($sPath)
 For $i=1 to $search[0]
   If Not StringInStr(FileGetAttrib($sPath & $search[$i]),"D") Then
       ;here goes your move function
   EndIf
 Next
Edited by sahsanu
Link to comment
Share on other sites

Or you could try this instead:

$search = _FileListToArray($sPath, "*", 1)

This returns just the file names and no folder information. Without the ", 1" parameter, you're also getting any folder names in the path statement which will mess up your FileMove. Another thing, if you only have 1 file left in the folder, it will also give you an error, you should be using $sFile = $search[1] instead of [2].

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

firstly, thanks all for your time, some great ideas and assistance.

Unfortunately, I'm still hitting the same brick wall, that is - as soon as the file is moved, the array item is cleared and I get the error " ==> Subscript used with non-Array variable.:"

is there a way to check whether $array[1] exists without exiting the loop if it doesn't?

I need to check for any file appearing, then move the file but continue checking for any further files that may appear.

Sorry if I'm being feeble, but I'm struggling with this :unsure:

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Try this, it worked for me, plus I added a hotkey to exit the script at any time using ALT + ESC. I substituted a local folder name on my system in place of yours for my script, but should work for you. If you run this from SciTE the console output will tell you the name(s) of the file(s) its trying to move, I added this as a way to check to make sure it's actually doing something.

#include <file.au3>
$sPath = "\\Sever\scripts\logs\FileTest\" 
$sPath2 = "\\Server\scripts\logs\FileTest\Moved\" 
HotKeySet("!{ESC}", "_Exit")
;============================
;               Check for file          =
;============================
While 1
    $search = _FileListToArray($sPath, "*", 1)
    If IsArray($search) Then
        $sFile = $search[1]
        _FileMove()
    EndIf
WEnd
;============================
;               Move file                       =
;============================
Func _FileMove()
    FileMove($sPath & $sFile, $sPath2)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath & $sfile = ' & $sPath & $sFile & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Local $n = 1
    Do
        $F = FileExists($sPath & $sFile)
        $F1 = FileExists($sPath2 & $sFile)
        If $F1 = 1 Then
            $sFile2 = StringSplit($sFile, ".")
            $renamed = $sFile2[1] & "_Duplicated" & $n & "." & $sFile2[2]
            ;MsgBox(0, "", $renamed)
            FileMove($sPath & $sFile, $sPath2 & $renamed)
        EndIf
        $n = $n + 1
    Until $F = 0
EndFunc   ;==>_FileMove
Func _Exit()
    Exit
EndFunc   ;==>_Exit

EDIT: I corrected several path errors in the file move and detection code that I had included by accident. I had added the final "\" after the path name that you already had in your path statements.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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