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:

water_200101_01.img  
water_200101_09.img  

files from number 10 to number30 are named:

water_200101_10.img
water_200101_30.img

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

water_200102_01.img  
water_200102_09.img  

files from number 10 to number30 are named:

water_200102_10.img
water_200102_30.img 

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

water_1
water_2
...till...
water_700

 Answers

1

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
 
2

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"
done

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
 
lechup
 
5

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
 
HamidR
 
4
@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"
)
popd

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
 
Xavio
 
5

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 :