move - Move files and folders

Maintained on

Moving specific files or folders to another location is a common task when working with the command prompt.

On this page, we cover the move command prompt and batch files, which are often used to manipulate folders on a PC. We explain how to delete files, from basic usage to setting options, in an easy-to-understand way. We also provide specific sample code.

What is the move command?

The move command allows you to move specified files or folders to another location.

The basic usage of the move command is as follows:

move <file or folder to be moved> <destination file or folder>

The most basic usage of the move command is to specify the file or folder to be moved and the destination file or folder, as shown above. However, more detailed usage is available as follows.

move [{/y|-y}] <file or folder to be moved> <destination file or folder>

By using the /y option, you can control whether or not to display a message when a file already exists in the destination folder.

When /y is specified, the confirmation message for overwriting files will not be displayed, even if the file already exists.

By default, a confirmation message is displayed, but even if you specify /-y, the confirmation message for overwriting files will still be displayed.

Examples of the move command

To move a file named test.txt that exists in the current directory to the folder C:\test, use the following command:

move test.txt C:\test

When you run the above code, the following output is displayed:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move test.txt c:\test
1 file(s) moved.
C:\users\user>

In the above example, since there is no file named test.txt in the destination folder c:\, the move is completed and the process ends.

If there is a file with the same name test.txt in the destination folder, the following output is displayed:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move test.txt c:\test
Do you want to overwrite c:\test\test.txt? (Yes/No/All):

If you enter No, the file will not be moved and the following output will be displayed:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move test.txt c:\test
Do you want to overwrite c:\test\test.txt? (Yes/No/All): No
0 file(s) moved.
C:\users\user>

If you enter Yes or All, the file will be moved and the following output will be displayed:

If the source is a folder and you enter All, the confirmation message for subsequent file movements will not be displayed.

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move test.txt c:\test
Do you want to overwrite c:\test\test.txt? (Yes/No/All): Yes
1 file(s) moved.
C:\users\user>

/y option

In the previous example, a confirmation message was displayed when a file with the same name existed in the destination folder.

By specifying the /y option, you can overwrite without displaying a confirmation message.

Regardless of whether or not there is a file with the same name in the destination folder, no confirmation message is displayed and the following output is displayed:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move /y test.txt c:\test
1 file(s) moved.
C:\users\user>

Moving folders

If you select a folder as the target to move, all files in the folder will also be moved.

Assume that there is a sample folder with the following structure:

└─sample
        sample01.txt
        sample02.txt
        sample03.txt
        sample04.txt

To move the sample folder to the C:\test folder, use the following command:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move sample c:\test
1 directory moved.
C:\users\user>

After execution, the C:\test folder will have the following structure:

C:\test

└─sample
        sample01.txt
        sample02.txt
        sample03.txt
        sample04.txt

Renaming with the move command

In the previous examples, we did not specify the file name of the destination.

If you do not specify a file name, the file name of the source will be used as the file name of the destination.

To change the file name of the source and destination, use the following command:

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move test.txt c:\test\sample.txt
1 file(s) moved.
C:\users\user>

The same applies to folders. If you specify a non-existent folder, the folder name of the source will be used as the folder name of the destination.

×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>move sample c:\test\disposable
1 directory moved.
C:\users\user>

After the move, the C:\test folder will have the following structure:

C:\test

└─disposable
        sample01.txt
        sample02.txt
        sample03.txt
        sample04.txt

If there is already a folder named C:\test\disposable, the structure will be as follows:

C:.

└─disposable

    └─sample
            sample01.txt
            sample02.txt
            sample03.txt
            sample04.txt

Exercise

Question 1

The following command was used to move a folder.

move from to

What happens if there is already a folder named to?

回答がサーバーに送信されることはありません
#command prompt #batch files #arguments #command line #command