Solved

aff a prefix  to all file in a folder

Posted on 2014-01-26
6
468 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

21 Experts available now in Live!

Get 1:1 Help Now