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.
sn0Asked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Hmm, I cannot explain why it should redo the first one, but to prevent it from doing:

@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 ren %%F "fx.%%~nxF"
)

Open in new window

0
 
osiris00Commented:
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

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Steve KnightIT ConsultancyCommented:
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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
sn0Author Commented:
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?
0
 
sn0Author Commented:

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

0
 
sn0Author Commented:
"ren" appears twice in line 10 which prevents the code from working correctly. Remove the 2nd instance of "ren" and it works perfectly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.