Archive: moving files


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.


Make sure the first file exists and the second doesn't.


how would you do it if they both exist?


You must first delete or move away the second file.


wait dose it matter if there already is a folder for the second one or dose that have to be made to?


The folder for the second path must exist. But in your case, if that one doesn't exist, the first one doesn't either.


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.


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

You can check using IfFileExists or LogicLib's ${If} ${FileExists} and act according to the file existence.


Originally posted by pkid
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}

Originally posted by PoRtAbLe_StEaLtH
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
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)