- Rename file from command line [CMD]
- Errors:
- FILE_RENAME_INFORMATION structure (ntifs.h)
- Syntax
- Members
- Remarks
- How to change or rename a file, folder, or directory
- How to rename in Microsoft Windows
- Method one
- Method two
- Method three
- Method four
- Renaming multiple files or folders at once
- How to rename in MS-DOS and the Windows command line
- Renaming a file
- Renaming a file with a space
- Renaming multiple files with one command
- Renaming a directory
- Rename in a batch file
- How to rename in macOS
- First recommendation
- Second recommendation
- Third recommendation
- How to rename in the Linux and Unix command line
- How to rename in Google Chrome OS
- First recommendation
- Second recommendation
Rename file from command line [CMD]
We can use the command rename to rename files from windows command prompt(CMD). Find below syntax of the command with examples.
Syntax of rename command:
After executing the above command we’ll have file2.doc in the folder d:\data
Ren is alias for rename , so both refer to the same command.
Errors:
- If the files is being used by a program, then rename command fails with below error.
- You also need to have sufficient privileges to rename the file.
- Rename changes just the file name, it does not convert a file from one type to another. For example, if you rename a file from mp4 to mp3 extension, it does not change the file format. Renaming a doc file ‘mydocument.docx’ to ‘mydocument.pdf’ does not make the file readable in Acrobat Reader.
Does the command have to be executed in the root directory, or can it be done in a subdirectory that is in the root directory? Also, can a directory be renamed (EXAMPLE: renaming Dell.dir to Newname.dir)?
yes, you can run the command (for that matter, any other windows command) from any folder, it need not be root folder. And yes, ren works for directories too. There’s a separate post for this – http://www.windows-commandline.com/rename-directory-from-command-line/
this command gives repeating of filename when there are more then 40 files in the folders ?
any solution for renaming 100s of file .
i have
4299999940_M_harsha.png these type of files in several sub folders, with similar name structure(42 as first part of name, 40_M_harsha as last part of name.. in between 6digits(which may contain 42 and 40 also.in this example it has 999999))
i have to rename it as 99999..i.e, remove first and last part of file name which is common for all files in the subfolder
another examples:
4292345640_M_harsha.png -> 923456
4291424040_M_harsha.png -> 914240
could u please help
how do I rename a file by keeping the last 20 characters. I have random file names however the last 20 characters are what I want to keep. They are dates and a document ID.
filename too long cannot rename Please now setup new rename
I use, Batch Rename Files Tool. You can easily found hier BatchRenameFiles.org that allows you to quickly rename all the files in a specified directory.
Simple stuff. Been there and done that but I have a mess of files that contain a “%20” or several in the filename. I need help to sort this out. My REXX program has produced a batch file containing lines like these:
1.JPG” corn_escaLator.jpg
ren “CHOCO%
1.JPG” choco_Lady.jpg
ren “CAT%20
4.JPG” cat_wet_getting_bathed.jpg
ren “CAT%20
3.JPG” cat_burrito.jpg
ren “CAT%20
2.JPG” cat_bra.jpg
ren “CAT%20
1.JPG” cat_bite_nose.jpg
ren “BOOTY%
1.JPG” booty_cake.jpg
ren “BIRD%2
2.JPG” bird_watersLide.jpg
ren “BIRD%2
1.JPG” bird_mouth_dog.jpg
ren “BIG%20
1.JPG” big_mac.jpg
ren “BANK%2
1.JPG” bank_cake.jpg
ren “ANGEL%
Suffice to say it isn’t working as intended. I get tons of
“The system cannot find the file specified.” messages.
I prefer not to use the powershell whether I have it or not in case I need to run this in a more primitive Windows. I have tried rename instead of ren and without double quotes. I have administrator privileges.
I am running Windows7 32-bit.
GOOD, IT WORKED FOR ME
I use windows 10, and I did the steps, even in the location but it ‘couldn’t find the file specified.’ Please help.
FILE_RENAME_INFORMATION structure (ntifs.h)
The FILE_RENAME_INFORMATION structure is used to rename a file.
Syntax
Members
Set to TRUE to specify that if a file with the given name already exists, it should be replaced with the given file. Set to FALSE if the rename operation should fail if a file with the given name already exists.
Flags for the rename operation. This field is only applicable when used with the FileRenameInformationEx information class.
Here are the possible values:
Value | Meaning |
---|---|
FILE_RENAME_REPLACE_IF_EXISTS 0x00000001 | If a file with the given name already exists, it should be replaced with the given file. Equivalent to the ReplaceIfExists field used with the FileRenameInformation information class. |
FILE_RENAME_POSIX_SEMANTICS 0x00000002 | If FILE_RENAME_REPLACE_IF_EXISTS is also specified, allow replacing a file even if there are existing handles to it. Existing handles to the replaced file continue to be valid for operations such as read and write. Any subsequent opens of the target name will open the renamed file, not the replaced file. |
FILE_RENAME_SUPPRESS_PIN_STATE_INHERITANCE 0x00000004 | When renaming a file to a new directory, suppress any inheritance rules related to the FILE_ATTRIBUTE_PINNED and FILE_ATTRIBUTE_UNPINNED attributes of the file. |
FILE_RENAME_SUPPRESS_STORAGE_RESERVE_INHERITANCE 0x00000008 | When renaming a file to a new directory, suppress any inheritance rules related to the storage reserve ID property of the file. |
FILE_RENAME_NO_INCREASE_AVAILABLE_SPACE 0x00000010 | If FILE_RENAME_SUPPRESS_STORAGE_RESERVE_INHERITANCE is not also specified, when renaming a file to a new directory, automatically resize affected storage reserve areas as needed to prevent the user visible free space on the volume from increasing. Requires manage volume access. |
FILE_RENAME_NO_DECREASE_AVAILABLE_SPACE 0x00000020 | If FILE_RENAME_SUPPRESS_STORAGE_RESERVE_INHERITANCE is not also specified, when renaming a file to a new directory, automatically resize affected storage reserve areas as needed to prevent the user visible free space on the volume from decreasing. Requires manage volume access. |
FILE_RENAME_PRESERVE_AVAILABLE_SPACE 0x00000030 | Equivalent to specifying both FILE_RENAME_NO_INCREASE_AVAILABLE_SPACE and FILE_RENAME_NO_DECREASE_AVAILABLE_SPACE. |
FILE_RENAME_IGNORE_READONLY_ATTRIBUTE 0x00000040 | If FILE_RENAME_REPLACE_IF_EXISTS is also specified, allow replacing a file even if it is read-only. Requires WRITE_ATTRIBUTES access to the replaced file. |
FILE_RENAME_FORCE_RESIZE_TARGET_SR 0x00000080 | If FILE_RENAME_SUPPRESS_STORAGE_RESERVE_INHERITANCE is not also specified, when renaming a file to a new directory that is part of a different storage reserve area, always grow the target directory’s storage reserve area by the full size of the file being renamed. Requires manage volume access. |
FILE_RENAME_FORCE_RESIZE_SOURCE_SR 0x00000100 | If FILE_RENAME_SUPPRESS_STORAGE_RESERVE_INHERITANCE is not also specified, when renaming a file to a new directory that is part of a different storage reserve area, always shrink the source directory’s storage reserve area by the full size of the file being renamed. Requires manage volume access. |
FILE_RENAME_FORCE_RESIZE_SR 0x00000180 | Equivalent to specifying both FILE_RENAME_FORCE_RESIZE_TARGET_SR and FILE_RENAME_FORCE_RESIZE_SOURCE_SR. |
A handle that IopOpenLinkOrRenameTarget uses to open the target directory.
If the file is not being moved to a different directory, or if the FileName member contains the full pathname, this member is NULL. Otherwise, it is a handle for the root directory under which the file will reside after it is renamed.
To perform two open operations that won’t cause a sharing conflict, you can open RootDirectory by requesting traverse | read-attribute. IopOpenLinkOrRenameTarget can then perform a relative open by requesting FILE_WRITE_DATA | SYNCHRONIZE. These two opens would not cause sharing conflict.
Length, in bytes, of the new name for the file.
The first character of a wide-character string containing the new name for the file. This is followed in memory by the remainder of the string. If the RootDirectory member is NULL, and the file is being moved to a different directory, this member specifies the full pathname to be assigned to the file. Otherwise, it specifies only the file name or a relative pathname.
Remarks
The FILE_RENAME_INFORMATION structure is used to rename a file. This operation can be performed in either of the following ways:
- Call FltSetInformationFile or ZwSetInformationFile, passing FileRenameInformation as the value of FileInformationClass and passing a caller-allocated buffer formatted as a FILE_RENAME_INFORMATION structure for the value of FileInformation. The FileHandle parameter specifies the file to be renamed.
- Create an IRP with major function code IRP_MJ_SET_INFORMATION.
File system minifilters must use FltSetInformationFile, not ZwSetInformationFile, to rename a file.
Renaming a file requires DELETE access to the file so that the directory entry may be removed from the current parent directory, as well as the appropriate access to create the new entry in the new parent directory file.
The file name string in the FileName member must be specified in one of the following forms.
- A simple file name. (The RootDirectory member is NULL.) In this case, the file is simply renamed within the same directory. That is, the rename operation changes the name of the file but not its location.
- A fully qualified file name. (The RootDirectory member is NULL.) In this case, the rename operation changes the name and location of the file.
- A relative file name. In this case, the RootDirectory member contains a handle to the target directory for the rename operation. The file name itself must be a simple file name.
General rules for rename operations:
- A file or directory can only be renamed within a volume. In other words, a rename operation cannot cause a file or directory to be moved to a different volume.
- A volume’s root directory cannot be renamed.
- If ReplaceIfExists is set to FALSE, and the target exists, the rename operation will fail.
- Even if ReplaceIfExists is set to TRUE, the rename operation will still fail if a file with the same name already exists and is a directory, a read-only file, or a currently executing file.
- A volume’s files and directories cannot be renamed if the volume is a read-only volume, such as a CDFS volume or a read-only NTFS volume.
Special rules for renaming open files:
- A file cannot be renamed if it has any open handles, unless it is only open because of a batch opportunistic lock (oplock) and the batch oplock can be broken immediately.
- A file cannot be renamed if a file with the same name exists and has open handles (except in the batch-oplock case described earlier).
- A directory cannot be renamed if it or any of its subdirectories contains a file that has open handles (except in the batch-oplock case described earlier).
Special rules for renaming NTFS data streams:
- The source handle cannot be opened with FILE_DIRECTORY_FILE.
- The source handle cannot be a directory opened without either FILE_DIRECTORY_FILE or FILE_NON_DIRECTORY_FILE.
- The new name for the stream must begin with a colon (:).
- A data stream can only be renamed within a file. In other words, a rename operation cannot cause a data stream to be moved to a different file.
- A stream on a directory cannot be renamed to the default data stream.
- If ReplaceIfExists is set to TRUE, the rename operation will succeed only if a stream with the same name exists and is a zero-length data stream.
- «Renaming» the default data stream is allowed, but this is not a true rename, because it leaves behind a zero-length default data stream.
The size of the FileInformation buffer passed to ZwSetInformationFile or FltSetInformationFile must be >= sizeof(FILE_RENAME_INFORMATION) plus the size in bytes of the FileName string.
How to change or rename a file, folder, or directory
Below are steps on how you can rename a file, shortcut, or directory. Click one of the following links to automatically navigate to the correct operating system.
You must have write or modify permissions to a file, folder, or directory to rename it. In some cases, you may need administrator privileges in the operating system to rename.
How to rename in Microsoft Windows
Windows users can rename their files and directories using one of the following methods. We’ve listed the following recommendations in what we believe to be the easiest methods of renaming a file.
This also works for removing spaces in a file name.
Method one
- Highlight the file or folder.
- Right-click the file with your mouse and select Rename from the menu that appears.
Method two
- Highlight the file or folder.
- Press the F2 key on the keyboard.
Method three
- Highlight the file or folder.
- Click File at the top of the window and select Rename from the list of available options.
Method four
- Highlight the file or folder you want to rename by single-clicking the file.
- Once highlighted, wait a few seconds and click the file name again. A box should appear surrounding the file or folder name, and you can rename the file.
If you don’t wait long enough and click the file or folder too fast, it can open the file or folder rather than allowing you to rename it.
Renaming multiple files or folders at once
- Open Explorer.
- In Explorer, select all the files you want to rename.
- How to select or highlight multiple files and folders.
- Once the files are selected, press F2 to edit the file name and type the new name for the files. For example, typing «test» renames the files to test, test(1), test(2), test(3), etc. If you have file extensions shown, make sure to also type the name of the file extension you’re renaming.
Microsoft Windows users can also rename any file using the Windows command line.
How to rename in MS-DOS and the Windows command line
MS-DOS and Windows command (CMD) line users can change the name of a file or directory using the ren or rename command. Below are examples of how this command can be used. Additional information about each of these commands is found by clicking the above command links.
Renaming a file
In the following example, this would rename the file test.txt to hope.txt.
If the test.txt file is not located in your current directory, you must specify the file’s path as a prefix to the file name. For example, if the file was in the «computer» directory, you would type a command similar to the following example.
Renaming a file with a space
Whenever dealing with a file or directory with a space, it must be surrounded with quotes. Otherwise, you’ll get the «The syntax of the command is incorrect.» error. To rename the file «computer hope.txt» to «example file.txt», your command would resemble the following example.
Renaming multiple files with one command
To rename multiple files at once, you must utilize some form of wild character. Below are examples of how this could be done.
The following example would rename all the files in the current directory that end with .rtf to .txt files.
In this next example, the command would rename a file with an unknown character in the file name to something that can be read. The «?» used in the following example is the wild character for an unknown character.
Renaming a directory
Renaming a directory in MS-DOS is much like renaming a file. Use the ren or rename command to rename the directory. Because you cannot have a file and directory of the same name, you won’t need to worry about mistakenly renaming a file instead of a directory. The only exception is if you’re using wild characters.
In the following example, this would rename the computer directory to hope.
Rename the directory «computer hope» to «example directory». Whenever dealing with a file or directory with a space, it must be surrounded with quotes. Otherwise, you’ll get the «The syntax of the command is incorrect.» error.
Rename in a batch file
To rename files in a batch file, you can use any of the rename commands or examples shown in the MS-DOS and Windows command line section.
How to rename in macOS
Apple macOS users can rename their files and directories using one of the following methods. We’ve listed the following recommendations in what we believe to be the easiest methods of renaming a file.
First recommendation
In the macOS Finder, select the file by clicking the file once and then press the ‘return’ key on the keyboard. After pressing return, you can type in the new name of the file.
Second recommendation
Select the file or icon you want to rename. Click and then hover over the file name until it is highlighted. Once highlighted, this indicates the file can be renamed.
Third recommendation
Using the Terminal, you can also rename any file. See our Linux and Unix users section for steps on renaming a file using the mv command.
How to rename in the Linux and Unix command line
For detailed information about renaming files in Linux, see the Linux mv command.
How to rename in Google Chrome OS
With the Google Chrome OS on a Chromebook, you can rename your files and directories using one of the following methods. We’ve listed the following recommendations in what we believe to be the easiest methods of renaming a file.
First recommendation
Highlight the file by clicking the file once. Press Ctrl + Enter on the keyboard and then type the new name of the file.
Second recommendation
Right-click the file by pressing two fingers on the touchpad at the same time. In the right-click menu, click Rename and then type the new file name.