Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

aff a prefix  to all file in a folder

Posted on 2014-01-26
6
479 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
ID: 39811335
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 27

Expert Comment

by:MacroShadow
ID: 39811351
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
ID: 39811872
Thanks for reply,
@ MacroShadow , can I pass the prefix through input box?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 150 total points
ID: 39811876
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 27

Accepted Solution

by:
MacroShadow earned 350 total points
ID: 39811920
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
ID: 39817064
thanks guys
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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