Move or rename file or folder (2024)

Move or rename file or folder

collapse all in page

Syntax

movefile source

movefile source destination

movefile source destination f

status = movefile(___)

[status,msg]= movefile(___)

[status,msg,msgID]= movefile(___)

Description

example

movefile source movesthe file or folder source to the current folder. movefile doesnot preserve the archive attribute of source.

example

movefile source destination moves source to the file or folder destination. If source and destination are in the same location, then movefile renames source to destination. To rename a file or folder when moving it, make destination a different name from source and specify only one file or folder for source.

If source is a folder, then destination must be a folder. If source is a folder or is capable of specifying multiple files and destination does not exist, then movefile creates destination.

example

movefile source destination f performs the move, even when destination is not writable. The state of the read/write attribute for destination does not change. This syntax will overwrite read-only files.

example

status = movefile(___) moves the specified file or folder and returns a status of 1 if the operation is successful. Otherwise, movefile returns 0. You can use this syntax with any of the input argument combinations in the previous syntaxes.

example

[status,msg]= movefile(___) also returns the message text for any warning or error that occurs.

example

[status,msg,msgID]= movefile(___) additionally returns the message ID for any warning or error that occurs.

Examples

collapse all

Move Files to Current Folder

Move files and folders to the current folder by omitting the destination input.

Create two folders: the first, myfiles, containing the file myfile1.m, and the second, myotherfiles, containing the file myfile2.m.

mkdir myfilesmovefile myfile1.m myfilesmkdir myotherfilesmovefile myfile2.m myotherfiles

Move myfile1.m to the current folder. Since a destination is not specified, MATLAB® assumes the destination is the current folder.

movefile myfiles/myfile1.m

Set the current folder to myfiles. Move myotherfiles and its contents to the current folder.

cd myfilesmovefile ../myotherfiles

Move Files and Folders to New Folder

Open Live Script

Move files and subfolders whose names begin with my from the current folder to the folder newFolder, where newFolder previously does not exist.

movefile my* newFolder

Rename Folder

Open Live Script

Create the folder myoldfolder, and then rename it to mynewfolder.

mkdir myoldfoldermovefile myoldfolder mynewfolder

Move File to Read-Only Folder

Open Live Script

Move the file myfile1.m from the current folder to the read-only folder restricted.

Create the read-only folder restricted.

mkdir restrictedfileattrib restricted -w

Move the file myfile1.m. A status of 0 shows the copy was unsuccessful.

status = movefile('myfile1.m','restricted');status
status = logical 0

Move the file myfile1.m using the 'f' option to override the read-only status of the destination folder. A status of 1 and an empty message and messageID confirm the copy was successful.

[status,message,messageId] = movefile('myfile1.m','restricted','f');status
status = logical 1
message
message = 0x0 empty char array
messageId
messageId = 0x0 empty char array

Input Arguments

collapse all

sourceFile or folder to move
character vector | string scalar

File or folder to move, specified as a character vector or string scalar. To move multiple files or folders, use wildcards (*).

source can be an absolute or relative path when moving local files or folders. However, to move files and folders at a remote location, source must contain a full path specified as a uniform resource locator (URL). For more information, see Work with Remote Data.

Note

If source is a string, enclose all the inputs in parentheses. For example, movefile("myfile.m","newfolder").

destinationFile or folder destination
character vector | string scalar

File or folder destination, specified as a character vector or string scalar. destination cannot include wildcards (*).

If destination is local, it can be specified as an absolute or relative path. If destination is remote, it must contain a full path specified as a URL. For more information, see Work with Remote Data.

Note

If destination is a string, enclose all the inputs in parentheses. For example, movefile("myfile.m","newfolder").

Output Arguments

collapse all

status — Move status
0 | 1

Move status, indicating if the attempt to move the file or folder is successful, returned as 0 or 1. If the attempt is successful, the value of status is 1. Otherwise, the value is 0.

Data Types: logical

msg — Error message
character vector

Error message, returned as a character vector. If an error or warning occurs, msg contains the message text of the error or warning. Otherwise, msg is empty, ''.

msgID — Error message identifier
character vector

Error message identifier, returned as a character vector. If an error or warning occurs, msgID contains the message identifier of the error or warning. Otherwise, msgID is empty, ''.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced before R2006a

expand all

Starting in R2020a, on UNIX platforms, the wildcard expression *.* no longer matches folders or files without an extension. In previous releases, the expression matches folders or files regardless of extension, including files without an extension. This change of behavior does not apply to Microsoft® Windows platforms.

See Also

cd | delete | dir | mkdir | copyfile | rmdir

Topics

  • Manage Files and Folders
  • Specify File Names
  • Work with Remote Data

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Move or rename file or folder (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Move or rename file or folder (2024)

References

Top Articles
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5789

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.