Batch file to tidy file names
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Batch file to tidy file names
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 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.
-
- Administrator
- Posts: 12601
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Batch file to tidy file names
There is a bulk rename utility that will do what you want. It's not a PowerShell script though.
StuartR
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
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 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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Batch file to tidy file names
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
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
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.
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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 5406
- Joined: 24 Jan 2010, 08:33
- Location: A cathedral city in England
Re: Batch file to tidy file names
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...
"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
Thanks for the offer. I just need a guidance with the following:
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.
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
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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Batch file to tidy file names
There seems to be some confusion here - args(0) appears to be a file path AND a 3 character extension.
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
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.
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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Batch file to tidy file names
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
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
You beat me to it! I had:
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.
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'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.
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
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.
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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Batch file to tidy file names
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.
Notepad++ is a free source code editor that supports many programming languages.
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
(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 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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Batch file to tidy file names
There's a bunch of recommendations in What is your favorite VBScript Editor?
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
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.
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.
-
- PlatinumLounger
- Posts: 5406
- Joined: 24 Jan 2010, 08:33
- Location: A cathedral city in England
Re: Batch file to tidy file names
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...
"(or one of the team)" - how your appointment letter indicates you won't be seeing the Consultant...
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Batch file to tidy file names
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.
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.
-
- Administrator
- Posts: 78415
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Batch file to tidy file names
Certainly! Start > All Programs > Accessories > Command Promptagibsonsw wrote:I assume Windows 7 still has a cmd 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
Hans