user4156827
user4156827

Reputation: 127

batch script to edit file names

My file names pattern is a_AA-A.txt or a_11-1.txt. I need to remove "a_" from the file name.

Is it possible to edit / remove "a_" from file name through batch commands.

I am using windows 7

Thanks in advance!

Upvotes: 0

Views: 601

Answers (2)

SomethingDark
SomethingDark

Reputation: 14325

If you need to simply remove the first two characters

@echo off
setlocal enabledelayedexpansion

for /f %%A in ('dir /b a_*.txt') do (
    set old_filename=%%~A
    set new_filename=!old_filename:~2!
    rename !old_filename! !new_filename!
)

If you want to remove the characters a_

@echo off
setlocal enabledelayedexpansion

for /f %%A in ('dir /b a_*.txt') do (
    set old_filename=%%~A
    set new_filename=!old_filename:a_=!
    rename !old_filename! !new_filename!
)

Upvotes: 1

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

not sure you can do this with batch... but you can do it with vbscript for sure...

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/81e7f64b-aa01-4087-b245-41f8a2972675/rename-a-file-in-vbscript?forum=ITCG

and you can call it from your batchjob

Upvotes: 0

Related Questions