SaidAkh
SaidAkh

Reputation: 1881

How to rename multiple files in vscode (visual studio code)?

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

Answers (12)

Umanda
Umanda

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

  1. Open a Terminal:

    You can do this by searching for "Terminal" in the applications menu or by pressing Ctrl + Alt + T.

  2. 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
    
  3. 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.

  4. 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.
  5. 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

Jernej Novak
Jernej Novak

Reputation: 3283

Powertoys have PowerRename utility which works nice for renaming multiple files in windows PowerRename

https://learn.microsoft.com/en-us/windows/powertoys/powerrename

Upvotes: 2

heySushil
heySushil

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

harleybl
harleybl

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.

  1. You highlight the files in the explorer, right-click, and select Batch Rename
  2. The extension creates a text file with the names of the files you want to rename, one each line. Update the text file with the new names
  3. Save the temporary text file from step 2 and the extension performs the rename.

Upvotes: 61

CPHPython
CPHPython

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:

Terminal/shell options

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:

Extensions rename

ls -R *.txt | Rename-Item -NewName {[io.path]::ChangeExtension($_.name, "log")}

Selects txt files and renames their extensions to log.

Name+extension rename

Get-ChildItem -R *.txt | Rename-Item -NewName {$_.name.Replace('.txt','-text.log')}

Selects txt files and renames them to [ORIGINAL_NAME]-text.log.

NEW_NAME+NUMBER+extension rename (original names are discarded)

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:

  • ls, an alias of the Get-ChildItem command (equivalent in Powershell), lists current directory files;
  • -R option allows the recursive lookup to happen;
  • listing result is piped into a multiple Rename-Item commands invocation.

Upvotes: -3

wedeluxe
wedeluxe

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

  1. Go to the Explorer view in VS Code's Side Bar.
  2. Select a file you want to rename.
  3. Press F2 or choose Rename from that file's context menu.
  4. Continue with step 2 as long as there are files you want to process.

(B) Batch rename multiple files using other tools

  1. Go to the Explorer view in VS Code's Side Bar.
  2. Select a file you want to rename.
  3. Press Alt+Ctrl+R or choose Open Containing Folder from that file's context menu.
  4. This brings up the file explorer of your operating system.
  5. Batch rename the files from there. How this is done in detail is beyond the scope of this answer (most of the time, it is just selecting all files to process and starting the rename-tool).

Upvotes: -4

Kin
Kin

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

Leeroy
Leeroy

Reputation: 2150

Not an option for Visual Studio Code (yet)...

...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

J. Hesters
J. Hesters

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:

enter image description here

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

AngelLopez
AngelLopez

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.

  1. Open "everything" and filter the file list.
  2. Select the files you want to change
  3. pick "Change Name" with rButtom
  4. Popup will display with old list, the new list, the old names and the new names, if you change the new name, the new list will change accordingly.

Upvotes: 0

Jed Richards
Jed Richards

Reputation: 12435

brew install rename
rename s/foo/bar/g **/*

Upvotes: 6

charly989
charly989

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

Related Questions