0

Hi I am new at using batch files and I am struggling to find a way of removing part of a file name for multiple files in a folder and all sub-folder.

the files are all named like r1_c02_200111_145423_am.csv and I need to remove the _am from the files.

I have tried the following

FOR /R "C:\Users\bob\Documents\data\" %%G IN (*_am.csv) DO REN "%%G" *.csv

but this does not change anything.

can anybody point me in the right direction please?

7-zete-7
  • 712
  • 4
  • 12

1 Answers1

0

With the help of such a script, you can perform the required operation.

@ECHO off
FOR %%i IN (*_am.csv) DO CALL :do_rename %%i
GOTO :eof

:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"

:eof

Now more.

The search in the question was correct. To perform a rename while passing through the FOR ... DO, you can use both the SETLOCAL EnableDelayedExpansion or a procedure call. I think it's easier to use a procedure call. you don’t have to puzzle over understanding (and misunderstanding) how the SETLOCAL EnableDelayedExpansion works.

For each iteration of the loop, the Wick procedure is called with the first argument of the file name passed to it:

CALL :do_rename %%i

In the procedure itself, the argument is converted to a variable. You can already use substring operations on the variable itself. More information about substring operations can be found here.

:do_rename
SET "_file=%1"
REN "%_file%" "%_file:~0,-7%.csv"
7-zete-7
  • 712
  • 4
  • 12