Asked  1 Year ago    Answers:  5   Viewed   4 times

I have over 700 files in one folder named as: files from number 1 to number9 are named for the first month:


files from number 10 to number30 are named:


And so on for the second month: files from number 1 to number9 are named:


files from number 10 to number30 are named:


How can I rename them without making any changes to the files. just change the nams, for example




file.rename will rename files, and it can take a vector of both from and to names.

So something like:

file.rename(list.files(pattern="water_*.img"), paste0("water_", 1:700))

might work.

If care about the order specifically, you could either sort the list of files that currently exist, or if they follow a particular pattern, just create the vector of filenames directly (although I note that 700 is not a multiple of 30).

I will set aside the question, "why would you want to?" since you seem to be throwing away information in the filename, but presumably that information is contained elsewhere as well.

Wednesday, June 2, 2021

For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.

The following would do and does not require the system to have the rename program (although you would most often have this on a system):

for file in *.html; do
    mv "$file" "$(basename "$file" .html).txt"

EDIT: As pointed out in the comments, this does not work for filenames with spaces in them without proper quoting (now added above). When working purely on your own files that you know do not have spaces in the filenames this will work but whenever you write something that may be reused at a later time, do not skip proper quoting.

Tuesday, June 1, 2021

Windows command prompt: (If inside a batch file, change %x to %%x)

for /r %x in (*.html) do ren "%x" *.htm

This also works for renaming the middle of the files

for /r %x in (website*.html) do ren "%x" site*.htm
Sunday, July 11, 2021
@echo off
pushd "pathToYourFolder" || exit /b
for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do (
  for /f "tokens=1* eol=_ delims=_" %%A in ("%%~nF") do ren "%%F" "%%~nB_%%A%%~xF"

Note: The name is split at the first occurrence of _. If a file is named "part1_part2_part3.jpg", then it will be renamed to "part2_part3_part1.jpg"

Thursday, July 29, 2021

This has been fixed with the latest update to VS 15.

Sunday, November 7, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :