Skip to content
⌘ NSIS Forum Archive

moving files

11 posts

Potz#

moving files

Well this is turning out to be harder than I thought it should be... I have been trying to move a file by using rename

rename "$PLUGINSDIR\portwarn\pw.gif" "$PLUGINSDIR\pw.gif"

I've tried using the whole extension instead of $PLUGINSDIR... I did read somewhere that if the folder is already created it doesn’t work? I'm not sure but I thought it would work.
Potz#
wait dose it matter if there already is a folder for the second one or dose that have to be made to?
kichik#
The folder for the second path must exist. But in your case, if that one doesn't exist, the first one doesn't either.
Potz#
Ok as or right now I have both that exist... I was just wondering if I had to have it create a folder also. For some reason mine isn't working when I try to move the file. It is really frustrating because I can't seem to find a solution. I think the way I have it is correct but for some reason it just isn't working.
Guest#
Originally posted by kichik
You must first delete or move away the second file.
Is there no way to use rename if the second file exists? I am trying to use rename to rename a file if it is in use so that copyfiles can copy the file to the install directory.

The logic would be:

Try to copyfile from temp folder to $instdir
if error then
rename file in $instdir (e.g. from test.dll to test.bak)
try the copyfiles command again
end if
kichik#
You can check using IfFileExists or LogicLib's ${If} ${FileExists} and act according to the file existence.
PoRtAbLe_StEaLtH#
Originally Posted by pkid View Post
Is there no way to use rename if the second file exists? I am trying to use rename to rename a file if it is in use so that copyfiles can copy the file to the install directory.

The logic would be:

Try to copyfile from temp folder to $instdir
if error then
rename file in $instdir (e.g. from test.dll to test.bak)
try the copyfiles command again
end if
If a file is in use.. it can't be renamed, moved, etc.
You'd have to kill the process that has it open
LockedList by AfrowUK works great.

As for moving vs. copying:
i try to move first, as i find it's faster, but if source dir & destination dir are on different drives, it will fail.. simply add a test. If it's a file.. you can rename across different drives.

    ${GetRoot} `${_SOURCE}` $5
    ${GetRoot} `${_DESTINATION}` $6
    ${If} $5 == $6
    Rename `${_SOURCE}` `${_DESTINATION}`
    ${Else}
    CreateDirectory `${_DESTINATION}`
    CopyFiles /SILENT `${_SOURCE}\*.*` `${_DESTINATION}`
    ${EndIf} 

If you want to add another check for errors:
    ${GetRoot} `${_SOURCE}` $5
    ${GetRoot} `${_DESTINATION}` $6
    ${If} $5 == $6
    ClearErrors
    Rename `${_SOURCE}` `${_DESTINATION}`
    IfErrors 0 +3
    CreateDirectory `${_DESTINATION}`
    CopyFiles /SILENT `${_SOURCE}\*.*` `${_DESTINATION}`
    ${Else}
    CreateDirectory `${_DESTINATION}`
    CopyFiles /SILENT `${_SOURCE}\*.*` `${_DESTINATION}`
    ${EndIf} 
Anders#
Originally Posted by PoRtAbLe_StEaLtH View Post
If a file is in use.. it can't be renamed, moved, etc.
Not true, some files can be renamed even when they are in use.
Originally Posted by PoRtAbLe_StEaLtH View Post
As for moving vs. copying:
i try to move first, as i find it's faster, but if source dir & destination dir are on different drives, it will fail.. simply add a test. If it's a file.. you can rename across different drives.
NTFS junctions/mount-points probably breaks this ${GetRoot} trick (Not a problem if you check for errors and fall back to a copy)