KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Renaming files
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: June 20, 2002

Compatibility: 6.5.x , 6.7.x, and 6.8.x

In 4th Dimension, you can rename a file using the command MOVE DOCUMENT. However, it can operate only one file at a time. The following is a method that will perform batch processing on one or more files located within the same folder.



` Project Method: mChangeName

` Description: Change the name of a single or multiple files

`

` $1 - Path to a file or folder

` $2 - String to be replaced

` $3 - String to replace

`

` Author: Add Komoncharoensiri - 4D, Inc.



C_TEXT($1;$2;$3;$Target;$orgString;$newString;$sourceDocument;$destinationDocument)

$Target:=$1

$orgString:=$2

$newString:=$3



C_LONGINT($pathType;$MaxSize;$FoundAt;$i)

$pathType:=Test path name($Target)



Case of

 : ($pathType=0) ` pathname refers to an existing directory or folder

 ARRAY TEXT($DocumentList;0)

 DOCUMENT LIST($Target;$DocumentList)

 $MaxSize:=Size of array($DocumentList)

 For ($i;1;$MaxSize)

   If (Position($orgString;$DocumentList{$i})#0)

     $sourceDocument:=$Target+$DocumentList{$i}

     $destinationDocument:=$Target+Replace string($DocumentList{$i};$orgString;$newString;1)

     MOVE DOCUMENT($sourceDocument;$destinationDocument)

   End if

 End for

: ($pathType=1) ` pathname refers to an existing document

 $FoundAt:=Position($orgString;$Target)

 If ($FoundAt#0)

 C_TEXT($separator;$destinationPath)

 C_LONGINT($vlPlatform;$vlSystem;$vlMachine)

 PLATFORM PROPERTIES($vlPlatform;$vlSystem;$vlMachine)

 If ($vlPlatform=3)

   $separator:="\"

 Else

   $separator:=":"

 End if

 $i:=1

 While ($i<=Length($Target))

   If ($Target[[$i]]=$separator)

     $FoundAt:=$i+1

   End if

    $i:=$i+1

 End while

  $destinationDocument:=Substring($Target;$FoundAt)

 $destinationPath:=Delete string($Target;$FoundAt;32000)

 $destinationDocument:=Replace string($destinationDocument;$orgString;$newString;1)

 $destinationDocument:=Insert string($destinationPath;$destinationDocument;$FoundAt)

 MOVE DOCUMENT($Target;$destinationDocument)

End if

Else

  ALERT("The given path doesn't exist.")



End case