Link to home
Start Free TrialLog in
Avatar of sn0
sn0

asked on

Scripting "fx." to file name?

I have a client who, for his website, needs a group of images (15,000+) renamed with a "fx." prefix. So, something similar to going from 001002.jpg to fx.001002.jpg.

My goal is to write a command script or find a way to automate this but I'm not having any luck with the copy or xcopy commands.
Avatar of osiris00
osiris00

I've created a simple vbs script to do it. Basically it will prompt for directory containing the files you want to rename and it will add the fx. Of course you can modify this to add whatever prefix you want.

Dim WshShell, FileManagement, BrowseDialogBox, SelectedFolder, OldString, NewString, FullPath, TheFolder, FileList
Dim File, ThisFile, TheString, AlreadyRenamed, TempName, FlagName, Success, FindFlag, NewName, Dummy
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FileManagement = WScript.CreateObject ("Scripting.FileSystemObject")
Set BrowseDialogBox = WScript.CreateObject("Shell.Application")
Set SelectedFolder = BrowseDialogBox.BrowseForFolder(0, "Select the folder containing the files you want to rename.", &H0001)
If InStr(1, TypeName(SelectedFolder), "Folder") = 0 Then
  Wscript.Quit
End If
   
Newprefix="fx."
   

FullPath = SelectedFolder.ParentFolder.ParseName(SelectedFolder.Title).Path
Set TheFolder = FileManagement.GetFolder(FullPath)
Set FileList = TheFolder.Files

Success = 0
For Each File in FileList
  ThisFile = File.Name
  
  AlreadyRenamed = InStr(ThisFile, Newprefix)

  If (AlreadyRenamed = 0) Then
     Success = 1
     
     NewName = Newprefix + ThisFile
     File.Name = Newname
End If
Next

If Success = 1 Then
Dummy = WshShell.Popup ("Rename Files operation complete!",5,"Rename Files",64)
Else
Dummy = WshShell.Popup ("Rename Files operation failed! Please repeat the operation.",0,"Rename Files",16)
End If
Wscript.Quit

Open in new window

Avatar of Qlemo
As a batch file, it is looking much smaller:

@echo off
rem if you want to be prompted for the folder, remove REM
REM set /P target=Which folder to use? 
rem if you want to setup a fixed folder,remove REM
set target=c:\test

for %%F in ("%target%\*.jpg") do ren %%F "fx.%%~nxF"

Open in new window

If you didn't want the . you could do it with an _ or - say with just one line:

rename *.* fx_*.*

If it were me I'd probably use a for loop anyway mind.

Steve
The ren with wildcards is buggy, and hence unreliable. I only use it if I want to replace parts, or insert parts between dots. Anything else is producing unexpected results in most cases. That is why I used a FOR loop, which gives you much more control over what is done.
Agreed there qlemo.. it works when it does and sometimes not quite the expected way - I.e  you can't rename to inc fx. at the start etc.

Steve
Avatar of sn0

ASKER

Qlemo: Your batch file works great except it renames the first file, for example: blah.jpg, to fx.fx.blah.jpg.

Not a big deal since it's only one file that it does it to (the first one) but, is there a way to get it to stop at the end of the group rather than going back to the beginning and stopping?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sn0

ASKER


Qlemo: I had to edit line 10 because "ren" appeared twice and was prohibiting the code from doing anything but, aside from that, it works perfectly.

Thank you very much.
@echo off
setlocal EnableDelayedExpansion
rem if you want to be prompted for the folder, remove REM
REM set /P target=Which folder to use? 
rem if you want to setup a fixed folder,remove REM
set target=c:\test

for %%F in ("%target%\*.jpg") do (
  set x=%%~nF
  if not "!x:~,3!" == "fx." ren %%F "fx.%%~nxF"
)

Open in new window

Avatar of sn0

ASKER

"ren" appears twice in line 10 which prevents the code from working correctly. Remove the 2nd instance of "ren" and it works perfectly.