user1533274
user1533274

Reputation: 33

How to mass rename files with multiple dots in Batch file?

I have the following folders structure:

C:\Database
C:\Database\Tables
C:\Database\Stored Procedures
C:\Database\Views
C:\Database\Functions

There will lots of files in each of the Tables, Stored Procedures, Views and Functions folders of the following format:

Under the Tables folder:

dbo.Table1Name.Table.sql
dbo.Table2Name.Table.sql
mon.Table3Name.Table.sql
test.Table4Name.Table.sql

Under the Stored Procedures folder:

dbo.usp_StoredProcedure1Name.StoredProcedure.sql
dbo.usp_StoredProcedure2Name.StoredProcedure.sql
dbo.usp_StoredProcedure3Name.StoredProcedure.sql
mon.usp_StoredProcedure4Name.StoredProcedure.sql
mon.usp_StoredProcedure5Name.StoredProcedure.sql

and likewise for Views and Functions.

The files under the Tables folder would then be renamed to:

dbo.Table1Name.sql
dbo.Table2Name.sql
mon.Table3Name.sql
test.Table4Name.sql

And the name of files under the Stored Procedures folder would then be:

dbo.usp_StoredProcedure1Name.sql
dbo.usp_StoredProcedure2Name.sql
dbo.usp_StoredProcedure3Name.sql
mon.usp_StoredProcedure4Name.sql
mon.usp_StoredProcedure5Name.sql

Ideally, I'd have a batch file in the Database folder that renames the files that remove Table, StoredProcedure, View and Function from the name of the files in their respective folders.

If it can't be easily done in Batch file, I'd try to find something in PowerShell.

Thanks Alex K for editing my original post (my first post) and thanks foxidrive for your recomendation and KnightWhoSayNi sharing your way.

Upvotes: 2

Views: 3781

Answers (3)

foxidrive
foxidrive

Reputation: 41234

Launch this and it will display the ren commands on the screen.
If it is right then remove the echo and run it again to perform the renames.

@echo off
for /f "delims=" %%a in ('dir "C:\Database\*.sql" /b /s /a-d ') do (
   for /f "tokens=1,2 delims=." %%b in ("%%~nxa") do echo ren "%%a" "%%b.%%c%%~xa"
)
pause

Upvotes: 1

dbenham
dbenham

Reputation: 130819

No batch script is needed. It can be done with a simple REN command on the command line! :-)

for %F in (Tables "Stored Procedures" Views Functions) do @ren "C:\Database\%~F\*.*.*.sql" "????????????.??????????????????????????????.sql"

Just make sure you have at least as many ? as there are characters in the longest source file name.

Double the percents if you use the command in a batch script.

Here is another simple solution (batch script) using two REN commands:

@echo off
for %%F in (
  Tables:Table
  "Stored Procedures:StoredProcedure"
  Views:View
  Functions:Function
) do for /f "tokens=1,2 delims=:" %%A in ("%%~F") do (
  ren "C:\Database\%A\*.*.*.sql" *.
  ren "C:\Database\%A\*.*.%%B" *.sql
)

See How does the Windows RENAME command interpret wildcards? for help in understanding why the above solutions work.

Upvotes: 0

KnightWhoSayNi
KnightWhoSayNi

Reputation: 570

BEFORE YOUR UPDATE

I wrote small batch script.

ex.

asd.first.second.txt -> asd.first.txt
aaa.first.second.txt -> aaa.first.txt
bbb.first.second.txt -> bbb.first.txt
ccc.first.second.txt -> ccc.first.txt
ddd.first.second.txt -> ddd.first.txt

Important: create batch script in the folder with your files

ECHO OFF
SETLOCAL EnableDelayedExpansion
CLS

SET temp_file=C:\temp\temp_string.txt
FOR /f "delims=" %%A IN ('DIR /B "%~dp0"') DO (
    ECHO %%A
    ECHO %%A > %temp_file%
    FOR /F "tokens=1,2,3 delims=." %%I IN (%temp_file%) DO (
        ECHO %%I %%J %%K
        rename %%I.%%J.%%K.txt %%I.%%J.txt
    )

)
DEL %temp_file%

Upvotes: 0

Related Questions