Bhanu Chawla
Bhanu Chawla

Reputation: 1148

Bulk rename files containing underscores in Command Prompt

I want to rename a batch of files with the names example_P1.csv to example.csv in a folder in command prompt.

How do I achieve this?

I can't get any success with the following command:

ren *_P1.csv *.csv

I want to remove _P1 from all those files.

Any help?

Upvotes: 0

Views: 1162

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

You can't do this with a simple rename. Try this instead:

@echo off

setlocal EnableDelayedExpansion

for %%f in (*_P1.csv) do (
  set "basename=%%~nf"
  ren "%%~ff" "!basename:~0,-3!%%~xf"
)

Upvotes: 1

Related Questions