rockymonkey555
rockymonkey555

Reputation: 8340

Removing portion of each folder name in Batch

I am trying to loop through every folder under "source" and remove the version extension that I've added.

For example,

\pathTo\source\FirstComponent.1.5\...
\pathTo\source\SecondComponent.4.6\...

Changed to,

\pathTo\source\FirstComponent\...

Here's what I have so far...

SETLOCAL
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%") DO FOR /f "delims=." %%j IN ("%%i") DO REN "%%~i" "%%~j%%~xi"

Upvotes: 1

Views: 2506

Answers (2)

Andriy M
Andriy M

Reputation: 77657

If the folder names that you need to fix are always in this format

name.number.number

and you need to remove the .number.number part, you can also use this method:

SETLOCAL
SET "SourceDir=C:\pathTo\source"
FOR /D %%I IN ("%SourceDir%") DO (
  FOR %%J IN ("%%~nI") DO RENAME "%%~I" "%%~nJ"
)

Basically, the idea is to treat the version numbers as name extensions. The inner loop uses the %%~nI expression to take just the folder name without the extension, i.e. without the last .number, and the result is assigned to %%J. In a similar fashion, the RENAME command uses %%~nJ to get rid of the remaining .number (the first one in the original name). And so, using the FirstComponent.1.5 name as an example, the RENAME command essentially ends up like this:

RENAME "...\FirstComponent.1.5" "FirstComponent"

Since the method essentially just drops the last two extensions, you can have however big version numbers: 10.123, 2014.12... The only limitation to the method is that version numbers must always consist of exactly two parts. (It could additionally support single-part version numbers if your names were not allowed to have a ..)

Upvotes: 1

Mofi
Mofi

Reputation: 49084

foxidrive gave already the right help to find the solution.

@echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=." %%j IN ("%%i") DO REN "%%~i" "%%~nj"
set SourceDir=

%%~nj instead of %%j is necessary to remove the path to the folder and get only new name of folder without path.

And also possible is following as suggested by Andriy M:

@echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=." %%j IN ("%%~ni") DO REN "%%~i" "%%j"
set SourceDir=

That is even better in case of one of the parent folders contains also a dot in name.

But if all folders have a dot in name like Component.One before the version string which must be kept, the following batch code can be used:

@echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "tokens=1,2* delims=." %%j IN ("%%~ni") DO REN "%%~i" "%%j.%%k"
set SourceDir=

And one more solution which removes the last 4 characters from folder name if last but third character is a dot:

@echo off
setlocal EnableDelayedExpansion
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO (
   set "FolderName=%%~nxi"
   if "!FolderName:~-4,1!"=="." REN "%%~i" "!FolderName:~0,-4!"
)
endlocal

Upvotes: 2

Related Questions