Reputation: 1881
I wonder, if there is way to rename multiple files in visual studio code? I have tried to use find and replace, no luck.
Upvotes: 83
Views: 146589
Reputation: 4843
I've discovered a handy tool called Batch Rename. It's great for renaming files. However, if you're dealing with a large number of files and need to do it recursively, my favorite approach is the following:
You can use the find
command along with rename
in the terminal to achieve this. Here's a step-by-step guide:
In this case I want to rename all .js files to .jsx
Open a Terminal:
You can do this by searching for "Terminal" in the applications menu or by pressing Ctrl + Alt + T
.
Navigate to the main folder (Folder A):
Use the cd
command to change the directory. For example, if your folder A is in the home directory, you can navigate to it like this:
cd ~/A
Run the find
command to locate all .js
files:
Use the following command:
find . -type f -name "*.js"
This will list all .js
files recursively in the current directory and its subdirectories.
Rename the files using rename
:
find . -type f -name "*.js" -exec sh -c 'mv "$0" "${0%.js}.jsx"' {} \;
Explanation of the command:
find . -type f -name "*.js"
: Finds all files (not directories) with
the extension .js
.-exec sh -c 'mv "$0" "${0%.js}.jsx"' {} \;
:sh -c '...'
tells the shell to execute the following command.mv "$0" "${0%.js}.jsx"
: This renames the file from .js
to .jsx
.$0
refers to the file name found by find
."${0%.js}.jsx"
uses parameter expansion to replace the .js
extension with .jsx
.Verify the changes:
You can use ls
to list the files in the directories and verify that they have been renamed.
Remember to backup your data before running any commands that modify files, just to be safe.
Upvotes: 6
Reputation: 3283
Powertoys have PowerRename utility which works nice for renaming multiple files in windows
https://learn.microsoft.com/en-us/windows/powertoys/powerrename
Upvotes: 2
Reputation: 509
VS Code has no such type of facility yet or extension on it. But using vs code terminal as a cmd and run this command on the folder where you want to change all file names from one to another like I want to change my all view files from HTML to PHP.
rename *.html *.php
Upvotes: 2
Reputation: 959
There are a few Visual Studio Extensions that try to provide this functionality. The first two I tried did not appear to work. There is an extension called Batch Rename which worked for me: https://marketplace.visualstudio.com/items?itemName=JannisX11.batch-rename-extension.
Here is how the extension works.
Upvotes: 61
Reputation: 13759
Perhaps the easiest more detailed way is by using VSCode Terminal tab (Ctrl/Cmd + J) and selecting from the dropdown menu the Powershell option:
Based on Kin's answer and the resource Kin provided (2nd page), in order to look into the current and all sub-folders, these are some useful renaming possibilities:
ls -R *.txt | Rename-Item -NewName {[io.path]::ChangeExtension($_.name, "log")}
Selects txt files and renames their extensions to log.
Get-ChildItem -R *.txt | Rename-Item -NewName {$_.name.Replace('.txt','-text.log')}
Selects txt files and renames them to [ORIGINAL_NAME]-text.log.
Get-ChildItem -R *.txt | %{Rename-Item $_ -NewName ("NEW_NAME-{0}.log" -f $nr++)}
Selects txt files and renames them to [NEW_NAME]-NUMBER.log.
How it works:
-R
option allows the recursive lookup to happen;Upvotes: -3
Reputation: 104
Renaming multiple files with a single shot is also called batch renaming. This can't be done from within Visual Studio Code.
There are two ways to get what you want:
(A) Rename files one by one
(B) Batch rename multiple files using other tools
Upvotes: -4
Reputation: 1728
VS Code only supports single file name. On Windows, to do batch rename, you can use one of the following
[CMD]
// Change the extensions of all .doc files to .txt
ren *.doc *.txt
// Replace the first three characters of all files starting with 'abc' by 'xyz'
ren abc* xyz*
[PowerShell]
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name.Replace('.txt','.log') }
A comprehensive tutorial can be found here
Upvotes: 19
Reputation: 2150
...but in Sublime Text with the dired package you can enter rename mode with Shift + R.
This gives you a buffer with each file on its line:
D:\path\to\myfolder
first.file
second.file
third.file
...
umpteenth.file
Rename files by editing them directly, then:
Ctrl+Enter = apply changes
Ctrl+Escape = discard changes
While in rename mode you can use the full power of the text editor: edit all filenames at once with multiple cursors, transpose strings (to accomplish switch renames in one fell swoop), find and replace, the Text Pastry package can give you number ranges etc.
vscode-dired will not let you do this, renames are one by one.
Upvotes: 2
Reputation: 14814
Here is how you can do it on Mac. Right-click
(or ctrl+click
or click with two fingers simultaneously on the trackpad if you are using a MacBook) on the folder that contains the files that you want to have renamed. Then click Reveal In Finder
. Then from within finder select all files you want to rename, right-click
the selected files and choose Rename X items...
. Then you will see something like this:
Insert the string you want to find and the string with which you want to replace that found string and hit rename. Done 🔨
Upvotes: 87
Reputation: 9
You can not rename several files at the same time in vscode,. The simplest way I found is using the free "everything" utility, it takes seconds to rename a bunch of files in one or several folders.
Upvotes: 0
Reputation: 1
You can rename a statement "in all files" highlighting it and then pressing "CTRL+R" and "CTRL+R"(again). That will replace the selected word/statement in the entire file and (if you don't disable the tooltip checkbox) in all other files where it matches. I'm not sure if this answer your question, because this is for the text inside the files, not for the filenames.
Upvotes: -4