Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Dour High Arch Sep 8 '13 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 '13 at 20:41
    
Can this be done via a script rather then command line? –  50-3 Sep 8 '13 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 '13 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 '13 at 21:16

4 Answers 4

up vote 1 down vote accepted

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

share|improve this answer
    
I am writing my own script and will post if anybody wants..Thanks.. –  Ani Sep 8 '13 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 '13 at 21:32
    
Ok Sir...Will do –  Ani Sep 8 '13 at 21:43

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
share|improve this answer
    
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 '13 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. –  Yassar Sep 8 '13 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 '13 at 20:37
    
I'll take a look at the file...thanks. –  Ani Sep 8 '13 at 20:37
    
Cool. Let me know if it works out for you. –  Yassar Sep 8 '13 at 20:38

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"
share|improve this answer

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
share|improve this answer
    
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 '13 at 15:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.