0

I have 9873 files in a folder on hard-drive that are named as

  • myname-script.sql.001
  • myname-script.sql.002
  • and so on

Now I need to rename these files to

  • myname-script.001.sql
  • myname-script.002.sql
  • and so on

I want to name them all at once through command line.

5
  • Why must you use the command line? What's wrong with using a rename program; there are many that do exactly what you are asking for. Sep 8, 2013 at 20:32
  • There is nothing wrong with the program...I saw a couple of people doing it in one go through command line....so I was wondering if there was a single command to do this...
    – Ani
    Sep 8, 2013 at 20:41
  • Can this be done via a script rather then command line?
    – 50-3
    Sep 8, 2013 at 20:52
  • yes it can...I was hoping to get a command line...but seems like it's more feasible using script
    – Ani
    Sep 8, 2013 at 21:04
  • Answer posted in 6 lines of vbscript - you would be hard pressed to do it in command prompt using 1 line of code without a 3rd party app or pre-writing functions in powershell
    – 50-3
    Sep 8, 2013 at 21:16

4 Answers 4

1

You can use vbscript to rename

Set fso = CreateObject("Scripting.FileSystemObject")

set oFldr = fso.getfolder("C:\file\path")

for each ofile in oFldr.Files

splited = Split(ofile.Name, ".", -1, 1)

ofile.name = splited(0) & "." & splited(2) & "." & splited(1)

Next

This script will split your file name via the . and then re-arrange it to swap the 2nd and 3rd extensions

3
  • I am writing my own script and will post if anybody wants..Thanks..
    – Ani
    Sep 8, 2013 at 21:30
  • Always best practice to answer your own question if you find the solution outside the answers provided and to upvote any answers that were helpful in being able to develop your own script
    – 50-3
    Sep 8, 2013 at 21:32
  • Ok Sir...Will do
    – Ani
    Sep 8, 2013 at 21:43
1

What have you tried so far? You should really do some research into the possible solutions and try a few things.

I also think this question may be better suited to StackOverflow. Anyway, I've modified a VBScript that I wrote a while ago that achieves what you want. I'm very much a VBScript novice, so please forgive the long-windedness of it. I'm sure there's a more elegant solution, but it does definitely work. The script assumes that the files will be encountered in order so you might run into some issues.

I have made the script available through my Google Drive account:

https://docs.google.com/file/d/0B-7FwCWf15oGd2R0R1RMaHdmRFk/edit?usp=sharing

You'll only have to change the file path in order for the script to work. The VBScript code in case the file doesn't work:

Dim sName
Dim fso
Dim folder
Dim count
count = 1

Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder("Your folder path")

For Each file In folder.Files   
    If count < 10 Then
        file.Name = "myname-script.00" + CStr(count) + ".sql"
    End If

    If count > 9 And count < 100 Then
        file.Name = "myname-script.0" + CStr(count) + ".sql"
    End If

    If count > 99 And count < 10000 Then
        file.Name = "myname-script." + CStr(count) + ".sql"
    End If
    count = count + 1
Next
10
  • This is what I have tried so far: 1. ren . .sql; 2. ren *.^sql. .sql; 3. ren *.^sql. .sql; 4. ren ^(.sql). *.sql;
    – Ani
    Sep 8, 2013 at 20:34
  • Not sure about that. Check the script in my answer. You'll just have to change one line (the path) for it to work. Test it on a few files to see if it actually works.
    – Yass
    Sep 8, 2013 at 20:35
  • I can do it through coding...but I was wondering if anyone has a command line option for the same...Thanks.
    – Ani
    Sep 8, 2013 at 20:37
  • Cool. Let me know if it works out for you.
    – Yass
    Sep 8, 2013 at 20:38
  • Please post answers here and not in a link to a different website to avoid link rot
    – 50-3
    Sep 8, 2013 at 20:55
1

Quite simple as pure cmd.exe (batch). I haven't tested, but I think I've got it correct.

As a long one liner from the command line:

for /f "eol=: delims=" %A in ('dir /b /a-d *.sql*^|findstr "\.sql\.[^.]*$"') do @for %B in ("%~nA") do @ren "%A" "%~nB%~xA%~xB"

As a batch file:

@echo off
for /f "eol=: delims=" %%A in (
  'dir /b /a-d *.sql*^|findstr "\.sql\.[^.]*$"'
) do for %%B in ("%%~nA") do ren "%%A" "%%~nB%%~xA%%~xB"
-2

You could use a loop and some cut and paste strategy, like this:

for i in `ls myname-script.sql.*` ; do j=`ls $i | cut -d '.' -f2` ; k=`ls $i | cut -d '.' -f3` ; mv $i myname-script.$k.$j ; done
1
  • The OP is on Windows though. Also note that the call to ls is unnecessary. And please, check your formatting before posting. Code should be indented by 4 spaces.
    – slhck
    Sep 9, 2013 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .