5

Is there a way to search for ALL .txt files in a project and automatically replace/rename them to .js?

E.g user.txt to user.js

It seems I can't search for a file format in VS Code.
If you know how, please share!

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Rene
  • 105
  • 2
  • 5

4 Answers4

4

You can use a combination of:

For example, given these files/folders:

VS Code screenshot of initial file/folders

Steps:

  1. Start by clicking on the EXPLORER panel and filtering to just display .txt files

    VS Code screenshot with filtered .txt files

  2. Select/highlight all the files, then right-click on any selected file, then select "Batch Rename"

    VS Code screenshot of context menu with "Batch Rename" highlighted

  3. That would open a text file ".Batch Rename.txt" with all the selected files

    VS Code screenshot with Batch Rename.txt

  4. Do a regular find-and-replace to change .txt to .js

    VS Code screenshot with Batch Rename.txt with .txt changed to .js

  5. Note that the ".Batch Rename.txt" is unsaved. It's like a preview of what the changes would look like

  6. Save ".Batch Rename.txt" and it will automatically close

  7. The EXPLORER panel should now be empty since it's filtered on .txt

    VS Code screenshot with empty EXPLORER panel

  8. Remove the filter and the files should now be renamed

    VS Code screenshot with .txt changed to .js

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
3

You could just do it in the terminal.

mv *.txt *.js
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
1

You can use find to do this in a terminal recursively.

find . -iname "*.txt" -exec rename .txt .js '{}' \;

refer Find multiple files and rename them in Linux

mv works if you want to just do it in a folder itself.

Gautham Santhosh
  • 797
  • 1
  • 10
  • 21
0

Old question, I know, but for you or anyone else coming in, if you're okay with using a non-VS Code solution, there's a GUI Windows tool called RegexRenamer that is so well-named, you already know what it does.

It gives you a preview of what the renamed files will look like, and has options to rename everything in subfolders or only the current folder, ignore/include file extensions in the search, and apply the rename only to folders/files or both.

What do you think?

daddy7860
  • 313
  • 1
  • 9
  • Couldn't install this one, it gives me an error during installation wizard. – ArturBalestro Mar 03 '23 at 20:20
  • @ArturBalestro and what does that error say? (I've only used this program on Windows, btw) – daddy7860 Mar 10 '23 at 01:17
  • @D@ddy7860 The error is: Error downloading file: File Not found (404), when trying to install .NET 2.0 – ArturBalestro Mar 22 '23 at 22:22
  • @ArturBalestro So I'm assuming the step-by-step process you followed to reach this point is 1. Downloading the regexrenamer installer from the official linked website 2. Running the installer on Windows 3. Seeing this error pop up during the installation? Or did you do something else? – daddy7860 Mar 24 '23 at 05:21
  • @D@ddy7860 The steps you mentioned is exactly what I did. The moment I start the installation, the error mentioned above pops up – ArturBalestro Apr 11 '23 at 14:06
  • @ArturBalestro well I have no idea how that's happening then. Have you tried looking for & using an older installer, or a portable version? – daddy7860 Apr 13 '23 at 04:22
  • I did, but I couldn't make it work anyway. I've since given up on this and done the manual way. – ArturBalestro May 05 '23 at 12:34