Solved

aff a prefix  to all file in a folder

Posted on 2014-01-26
6
461 Views
Last Modified: 2014-01-28
Dear all,
I need to add a given prefix to all files in a specific folder.
bypassing the prefix to .bat file that execute the vbscript...
thanks in advance.
0
Comment
Question by:ethar1
6 Comments
 
LVL 3

Expert Comment

by:Rabih
Comment Utility
you can download the Bulk Rename Utility. Easy to use and free.


http://www.bulkrenameutility.co.uk/Download.php

it has a lot of options such as renaming, removing extensions, adding a prefix....... all can be applied to a mass number of files.

regards
0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Not sure what you mean by: bypassing the prefix to .bat file that execute the vbscript...

Try this:
  Dim oFS, sDir, oFile, sPrefix
  Set oFS = CreateObject("Scripting.FileSystemObject")

  sDir = "C:\Demo" ' <<<Change to match your folder
  sPrefix = "YourPrefix" ' <<<Change to match your prefix

  For Each oFile In oFS.GetFolder(sDir).Files
     oFile.Name = sPrefix & oFile.Name
  Next

Open in new window

0
 

Author Comment

by:ethar1
Comment Utility
Thanks for reply,
@ MacroShadow , can I pass the prefix through input box?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 150 total points
Comment Utility
I'm interpretting that you want to pass the prefix in as a command line parm, in which case do this in the BAT script:

cscript //nologo yourscript.vbs "c:\temp" "myprefix_"

And access those parms like this in VBS:

' Check for folder name and prefix in command line parms
Set oArgs = WScript.Arguments
If oArgs.Count < 2 Then
    Wscript.Echo "Please specify both a folder name, and a file prefix for renaming."
    Wscript.Quit
End If

' Fetch parms into local variables
sBaseDir = oArgs(0)
sPrefix = oArgs(1)

' Access the folder
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sBaseDir)

' Process all files in the folder
For Each oFile In oFolder.Files
   ' If we haven't already renamed before, then do the rename now
   If LCase(Left(oFile.Name, Len(sPrefix))) <> LCase(sPrefix) Then
      oFile.Name = sPrefix & oFile.Name
   End If
Next

Open in new window

~bp
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 350 total points
Comment Utility
Sure.
    Dim oFS As Object, oFile As Object
    Dim strDir As String, strPrefix As String

    Set oFS = CreateObject("Scripting.FileSystemObject")

    strDir = "C:\Demo"          ' <<<Change to match your folder
    strPrefix = InputBox("Enter your prefix.", "Prompt")   ' <<<Change to match your prefix

    If Len(strPrefix) > 0 Then
        For Each oFile In oFS.GetFolder(strDir).Files
            oFile.Name = strPrefix & oFile.Name
        Next
    Else
        MsgBox ("No prefix was entered.")
    End If

Open in new window

0
 

Author Closing Comment

by:ethar1
Comment Utility
thanks guys
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now