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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Qlemo"Batchelor", 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
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
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Qlemo"Batchelor", 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
Qlemo"Batchelor", 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.