Batch file to tidy file names

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Batch file to tidy file names

Post by agibsonsw »

Hello. Vista Home Premium SP2.

I wonder if someone has a batch file that does something similar to the following:
I want to rename all the (image) files in a folder to replace spaces with underscores and remove hyphens.

Preferably a vbScript file or even PowerShell - I would be very interested in a PS example. Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
StuartR
Administrator
Posts: 12602
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Batch file to tidy file names

Post by StuartR »

There is a bulk rename utility that will do what you want. It's not a PowerShell script though.
StuartR


User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

Thank you, although I'm more interested in the script itself.
I could probably knock it up myself using the FileSystemObject. Although I need to know how to read a command line argument in a vbScript?
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

The command line arguments are passed to the script processor in WScript.Arguments:

Code: Select all

dim i
dim arr
set arr = wscript.arguments
for i = 0 to arr.count - 1
  msgbox "Argument #" & i & " is " & arr(i)
next
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

Cool.
The arguments are always space-separated (not comma-separated) aren't they? I was thinking of supplying the folder path and an extension as well:
AndysGreatSCript.vbs C:\SomeFolder png
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

Yes, the arguments are separated by spaces.
Best wishes,
Hans

User avatar
John Gray
PlatinumLounger
Posts: 5406
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Batch file to tidy file names

Post by John Gray »

I could do it with a real BATch file, if you like...
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

Thanks for the offer. I just need a guidance with the following:

Code: Select all

Dim fso
Dim args
Dim fls
Dim itm
dim itmFN
Set fso = CreateObject("Scripting.FileSystemObject")
set args = WScript.Arguments
If args.Count <> 2 Then
    MsgBox "Enter: CorrectNames.vbs png", 16
elseIf fso.FolderExists(args(0)) Then
    Set fls = fso.GetFolder(args(0))
    For Each itm In fls.Files
        If ucase(right(itm.Name,3))=ucase(args(0)) Then
	  itmFN = replace(itm.Path,"-","")
	  itmFN = replace(itm.Path," ","_")
          'itm.movefile(itm.Path,itmFN)
      End If
    Next
End If
But I realise this will replace spaces in the folder path. How can I avoid this?
Is there an editor that could help me enter VB Script? What is the equvalent of the VBA End Statement? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

There seems to be some confusion here - args(0) appears to be a file path AND a 3 character extension.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

Sorry. I'm using Notepad currently. I meant args(1) to refer to the extension. I will use the script like this:
GreatScript.vbs "C:\SomeFolder\With a Space\My Folder" png
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

Does this work?

Code: Select all

Dim fso
Dim args
Dim fls
Dim itm
Dim itmFN
Set fso = CreateObject("Scripting.FileSystemObject")
Set args = WScript.Arguments
If args.Count <> 2 Then
    MsgBox "Enter: CorrectNames.vbs png", 16
ElseIf fso.FolderExists(args(0)) Then
    Set fls = fso.GetFolder(args(0))
    For Each itm In fls.Files
        If UCase(Right(itm.Name, 3)) = UCase(args(1)) Then
            itmFN = Replace(itm.Name, "-", "")
            itmFN = Replace(itmFN, " ", "_")
            itm.Name = itmFN
        End If
    Next
End If
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

You beat me to it! I had:

Code: Select all

Dim fso
Dim args
Dim fls
Dim itm
dim itmFN
dim lenPath

Set fso = CreateObject("Scripting.FileSystemObject")
set args = WScript.Arguments
If args.Count <> 2 Then
    MsgBox "Enter: CorrectNames.vbs C:\SomeFolder png", 16
elseIf fso.FolderExists(args(0)) Then
    Set fls = fso.GetFolder(args(0))
    For Each itm In fls.Files
	If ucase(right(itm.Name,3))=ucase(args(1)) Then
		lenPth = len(itm.Path)-len(itm.Name)
		itmFN = replace(itm.Name,"-","")
		itmFN = replace(itmFN," ","_")
		itmFN = left(itm.Path,lenPth) & itmFN
        	fso.movefile itm.Path,itmFN
	End If
    Next
End If
I found on the internet that MoveFile was the alternative to renaming a file. Didn't know that I could just set the Name to something.
I'll try yours as well. I think I should have replaced '-' with '_' to make the filenames easier to read. I'll have to correct this bit manually now :(.
Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

It seems that WScript.Quit is the alternative to VB's End statement.
Is there an editor/IDE that would help to create vb scripts? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

I seldom write VBScript, but when I need to, I write it in the Visual Basic Editor in Word or Excel, then transfer the code to a text file and remove the bits VBScript doesn't understand.

Notepad++ is a free source code editor that supports many programming languages.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

(I haven't written a script in an age either.)
I started with the Excel VBE but this isn't so helpful as I have to remove too much code. Although I can add a reference to the scripting object
my computer doesn't automatically install the help (other than the Object Browser).
I have Notepad++ but I might search for a specific scripting editor.
I even used to use the Script Editor in Excel lol. Thanks, Andy.

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

There's a bunch of recommendations in What is your favorite VBScript Editor?
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

Thanks. I chose VbsEdit. Looks great: intelli-sense, code completion, debugging, help system. I got over-excited!

Only problem is, I now have about 12 different editors on my computer, and a similar number of programming languages. Regards, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
John Gray
PlatinumLounger
Posts: 5406
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Batch file to tidy file names

Post by John Gray »

Just for fun here's my solution. Remove the word ECHO (in capital letters) from near the bottom when you're satisfied that it works OK!

Code: Select all

@echo off
setlocal enabledelayedexpansion
:: change file extensions to suit
for %%b in (jpg jpeg png gif) do (
  for /f "tokens=*" %%a in ('dir *.%%b /b /on') do (
    set orig="%%a"
    rem remove hyphens, and change spaces to underscores
    set renm=!orig:-=!
    set renm=!renm: =_!
    rem only rename if the filename needs changing
    if !renm! neq !orig! ECHO rename !orig! !renm!
  )
)
endlocal
John Gray

"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Batch file to tidy file names

Post by agibsonsw »

I'm sure you're right and it looks very sexy. Is there a secret coven for people who know this stuff?

Still I think a knowledge of batch files is an essential skill even today. I do remember stuff from my DOS days. COPY, XCOPY, REM and (the big one)
FORMAT! I just remembered 'dir /p' as well. And there were pipes | to send stuff to a text file?
I assume Windows 7 still has a cmd prompt?
But what about a PowerShell example? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Batch file to tidy file names

Post by HansV »

agibsonsw wrote:I assume Windows 7 still has a cmd prompt?
Certainly! Start > All Programs > Accessories > Command Prompt

And if you hold down Shift while right-clicking a folder in Windows Explorer, the popup menu will contain an item "Open command prompt here".
Best wishes,
Hans