0

Whenever I need something, I come here and at this moment I need something I found here but wanted more in this code:

@echo off
setlocal enableDelayedExpansion
for %%F in (*.txt) do (
  set "name=%%F"
  ren "!name!" "!name:file1=fileA!"
  ren "!name!" "!name:file2=fileB!"
)

My question is: how to rename the files1 and file 2 in this folder and subfolders and write at the end a message "Rename: 2 files"

Thank you for your help.

Sweeper
  • 465
  • 1
  • 4
  • 15
  • 1
    What is up with all the spacing within your commands. I am pretty sure nobody has ever taught you that on StackOverFlow nor will you ever see an example of that on StackOverflow. Regardless of that, you have not stated a question or what exactly you need help with. – Squashman Jun 12 '18 at 13:54
  • Excuse. I restated the question – Sweeper Jun 12 '18 at 14:53
  • I looked back at all your batch file questions and you have never coded anything in that syntax format. Why are you attempting to free form the coding now. It will not work that way. – Squashman Jun 12 '18 at 14:55
  • I've seen this code here in the forum and you're fine. just wanted it to search the subfolders file1 and file2 and rename them, and in the end write down how many changes. – Sweeper Jun 12 '18 at 15:10
  • Please show me the question on StackOverFlow that programmed a batch file with spaces in the variable names. Your code as it stand would never work. Go back and look at your previous batch file questions. You can easily see what is wrong with most of your code. – Squashman Jun 12 '18 at 15:15
  • https://stackoverflow.com/questions/9383032/rename-all-files-in-a-directory-with-a-windows-batch-script/50816205?noredirect=1#comment88641500_50816205 – Sweeper Jun 12 '18 at 15:37
  • @Sweeper you really shouldn't write code with an editor, that tries to auto-correct. The code, you referenced, doesn't have those spaces. Please edit your question to reflect the code you actually run. – Stephan Jun 12 '18 at 16:06
  • That code does not have any spaces within the FOR variable or the environmental variables. You can't make up your syntax for how a command is supposed to work. You have to program within the construct of the programming language you are using. Remove the free form spaces from your code and use `DO` with the `FOR` command just like you have been shown in your previous batch file questions. – Squashman Jun 12 '18 at 16:06

1 Answers1

0

to make it recursive (processing subfolders), add the /R switch.
Use a variable to build the new filename and execute the ren command only once per file.

@echo off
setlocal enableDelayedExpansion
set count=0
for /R %%F in (*.txt) do (
  set "newname=%%~nxF"
  set "newname=!newname:file1=fileA!"
  set "newname=!newname:file2=fileB!"
  ren "%%F" "!newname!"
  set /a count+=1
)
echo %count% files renamed
Stephan
  • 53,940
  • 10
  • 58
  • 91