none
Script to rename part of a file share path RRS feed

  • Question

  • I roll out a windows 7 image every few weeks in our classroom computers.  The instructor PC has 4 profiles, each with a class share folder on the desktop.  The student PC has 4 profiles, each with a shortcut on the desktop to the corresponding instructor class share folder.  (Student-1 share points to Student-1 folder of instructor's desktop).  The computer names are always xxx-yyy-zzz where x is the city, y is the room number and z is the seat number in the classroom.

    Image has something like: ""\\MIA-123-000\Users\Student-1\Desktop\Student-1 Share"

    I need a script that can replace the first 7 characters of the share, with the first 7 characters of the current computer name. 

    Can anyone figure out how to do this?


    • Edited by Thomas2z Saturday, October 25, 2014 8:08 PM
    Saturday, October 25, 2014 8:06 PM

Answers

  • objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
    
    objServer=Left(objComputer,7) & "-000"
    
    objLink.TargetPath = objShell.ExpandEnvironmentStrings("\\" & objServer & "\Users\%username%\Desktop\%username% Share")



    • Edited by Brian Nadjiwon Sunday, October 26, 2014 8:55 AM
    • Marked as answer by Thomas2z Sunday, October 26, 2014 9:02 AM
    Sunday, October 26, 2014 8:54 AM

All replies

  • I bet your students can do that for you.

    $name= '\\MIA-123-000\Users\Student-1\Desktop\Student-1 Share'-replace 'MIA-123-000', $env:computername


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, October 25, 2014 8:38 PM
    Saturday, October 25, 2014 8:35 PM

  • BTW, the OP asked for the first 7 chars to be replaced, not the string "MIA-123-000".

    Now does that really make any sense?  Here are the first 7 characters: \\MIA-1

    What good would that do?

    There is actually nothing about the question that makes and technical sense.


    ¯\_(ツ)_/¯

    Saturday, October 25, 2014 9:12 PM
  • Let us assume he wants to replace the first 7 characters of the computer name with the current computer name.

    $name= '\\MIA-123-000\Users\Student-1\Desktop\Student-1 Share'-replace 'MIA-123', $env:computername.SubString(0,7)

    Again this is all silliness due to a lack of basic computer knowledge.  If the instructor wants to access the students shares then he need to map to them.  If the students want the instructor shares then they need to use the instructors machine name.

    The question, as asked, is nonsense.  It may just be a bad use of language or it may be a lack of technical knowledge about how sharing works.  From the information given it is not possible to understand what is being asked.


    ¯\_(ツ)_/¯

    Saturday, October 25, 2014 9:21 PM
  • Hello;

    Sorry for the confusing question, I am not a programmer.  Yes I want to replace the first 7 letters of the share, so in my example above I would want to replace the MIA-123 with the first 7 letters of the current computer name.

    I am sorry to also ask, how I run the code above

    Sunday, October 26, 2014 1:43 AM
  • The instructor PC is providing a folder on his desktop to the students.  There is a share on the students' desktop, but when we roll the image into another classroom, we do not want to have to touch all 4 profiles on all student PCs to point to the instructor PC in that room. 

    I suppose I need the first 9 characters to be replaced in the share \\MIA-123 needs to be replaced with two slashes and the first 7 characters of the current computername.

    Thank you for your time

    Sunday, October 26, 2014 2:04 AM
  • If you do not know how to use a scrip you will need to contact a consultant or a tech to do this for you.  We do not write scripts on spec.

    Sorry.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 4:33 AM
  • If you want to try this for yourself I recommend starting here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 4:41 AM
  • I found a .lnk and have it 90% working, the only part I cannot get is the part of the computername.  There are 2 Lines I need help with.  In this line: 

    strAppPath = \\%computername%\Users\%userername%\Desktop\%username% Share

    I need to use only the first 7 characters of the computername and append a -000 to it along with the remaining \Users\%userername%\Desktop\%username% Share"  In this line: Set objLink = objShell.CreateShortcut(objDesktop & "\%username% Share.lnk") I need the actual username before the word share.lnk

    Sunday, October 26, 2014 4:45 AM
  • here is the whole thing

    Option Explicit

    On Error Resume Next

    Dim objShell
    Dim objDesktop
    Dim  objLink
    Dim strAppPath
    Dim strWorkDir
    Dim strIconPath


    strWorkDir ="C:\windows"
    strAppPath = "\\%computername%\Users\%username%\Desktop\%username% Share"   'I need to use the first 7 characters of the computername followed by -000 then the rest of the path
    strIconPath = "\\server\Xyz.ico"      'specify the path to the icon

    Set objShell = CreateObject("WScript.Shell")
    objDesktop = objShell.SpecialFolders("Desktop")
    Set objLink = objShell.CreateShortcut(objDesktop & "\ Share.lnk")  'I need to insert the username before the space and word share.lnk


    objLink.Description = "Student Share"       'I need to insert the username before the space and word share
    objLink.IconLocation = strIconPath
    objLink.TargetPath = strAppPath
    objLink.WindowStyle = 3
    objLink.WorkingDirectory = strWorkDir
    objLink.Save

    Sunday, October 26, 2014 4:51 AM
  • You are still asking for someone to write  this for you. 

    hat I stopping you from just adding this manually to the students desktop.  Why does it have to be done with a script?  Just navigatge to the share and drag it from  the address bar to the desktop.

    Mostly you are saking to do something with a script that is a native part of the Windows UI.  Where did you get the idea you needed a script?

    Just navigate to the share and drag the contents of the address bar to the desktop.

    If you want this to be automatic for any student add the shortcut to the all users profile desktop and edit it to use %username% in the locatin where you need the username.

    You do not need a script for this.


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, October 26, 2014 5:25 AM
    Sunday, October 26, 2014 5:12 AM
  • Although I appreciate you answering me, you do not understand the students or instructors we have.  This is not a programming school, these are not tech savvy students or instructors.  This script will save me hours of time each week if I can just get these last two lines working.  I cannot just add it to the All Users Profile because it is the computername that changes when the image is rolled out from classroom to classroom.

    Sunday, October 26, 2014 5:24 AM
  • Looking closely at your script changes it makes even less sense.  A share is identified by a computername.  You cannot add -000 to the computername and have it work.

    I highly recommend getting a consultant to help you sort out what you are trying to do.

    You also cannot link a user to you desktop.  Other users do not have permission to attach to your system and profile.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 5:25 AM
  • This line of yours:

    strAppPath = "\\%computername%\Users\%username%\Desktop\%username% Share"   'I need to use the first 7 characters of the

    ...cannot be made to work technically as you have designed and asked for it.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 5:29 AM
  • So your are telling me there is no way to capture the computername, pull out the first 7 characters or delete out the last 3 characters and then insert the results?   
    Sunday, October 26, 2014 5:36 AM
  • Our computernames are always xxx-yyy-zzz (MIA-123-014)  The share is always to the instructor computer, which is always number 000.  So if I can use the first 7 characters of the actual computer name, and add -000 to it, it will always be pointing to the instructor computer.

    Example - Instructor Computer:  MIA-123-000, Student Computer: MIA-123-014

    There are 25 computers in a class with 4-profiles on each one.  That is 100 profiles I need to log into every week when I roll out an image to change the shortcut on the desktop to point to the instructor.

    Sunday, October 26, 2014 5:45 AM
  • So your are telling me there is no way to capture the computername, pull out the first 7 characters or delete out the last 3 characters and then insert the results?   

    I showed you how to do that earlier.  You are asking for incremental building of a script.  We have been around in circles three times now.

    The command in VBScript for extracting part of a string is documented on the link I posted.  It is called "SubString".

    Please understand that this is not a forum for free scripting for end users.  It is a forum for technicians.  IF you do not know scripting and are not learng it then you will need to contact a consultant to help you with this.

    I will give you one more clue that will show you how to use the scrip you copied.

    Set objShell = CreateObject("WScript.Shell")
    
    strShortcut = objShell.SpecialFolders( "Desktop" ) & "\%username% Share.lnk"
    strShortcut = objShell.ExpandEnvironmentStrings(strShortcut)
    Set objLink = objShell.CreateShortcut( strShortcut ) 
    
    objLink.Description = objShell.ExpandEnvironmentStrings("%username% Share")
    objLink.TargetPath = "\\%computername%\Users\%username%\Desktop\%username% Share"
    objLink.Save
    

    That will create a link on the users desktop with the username and computername.  I removed all of the unnecessary and incorrect lines.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 5:46 AM
  • Here is a quick way to get the first 7 characters of  the computer name:

    strName = Left(objShell.Environment("process")("computername"),7)


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, October 26, 2014 5:50 AM
    Sunday, October 26, 2014 5:50 AM
  • Our computernames are always xxx-yyy-zzz (MIA-123-014)  The share is always to the instructor computer, which is always number 000.  So if I can use the first 7 characters of the actual computer name, and add -000 to it, it will always be pointing to the instructor computer.

    Example - Instructor Computer:  MIA-123-000, Student Computer: MIA-123-014

    There are 25 computers in a class with 4-profiles on each one.  That is 100 profiles I need to log into every week when I roll out an image to change the shortcut on the desktop to point to the instructor.

    All of this can be done with a single shortcut in the all users profile.

    Just add this to the shortcut an copy it to each pc all users desktop.

    target = "\\instructor\users\%username%\DEsktop\%username%"

    When you insert the string into the shortcut it mist be enclosed in quotes so it retains the percent signs.  Now it will work for any users.

    What I do not understand is why every student has a profile on the instructors computer.  That is silly. It makes all of this much harder.

    We can design a GPO that will create the links based on location and nothing will ever need to be deployed.

    You are trying to use 19th century understanding of technology to work with a 21st century technology.  The teacher need to by upgraded to "Teacher V2.0"

    Why don't you have your technicians do this for you.  Why is a teacher fixing computers?


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 6:02 AM
  • Thank you so much, I have been thinking about this for 6-months and sitting here for 2-days trying to get this to work.  Your script works great, all I need to do is add in the substring line and make this script not run if the shortcut is already on the desktop. 

    I really do appreciate your time.  I know it is not the easiest this communicating with us non-programming people.  I didn't mean to frustrate you, I just need to get this working.  Thank You.

    Sunday, October 26, 2014 6:03 AM
  • The point I am trying to make is that, if you had someone trained in Windows to set up your system you wouldn't be in this pickle.  We set up very large corporations all of the time and we do not need to write convoluted scripts. The problem I see is that you are approaching the whole issue of sharing backwards which is why it is so hard for you to see how to do this.

    You are building a sneaker-net on top of an advanced networking system which basically disables or ignores all of the built-in technology.

    Anyway - good luck.  I recommend considering some trainging and, possibley, a consultant t ohelp get you set up for automated "no-touch" deployment.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 6:11 AM
  • I don't think I am explaining the shortcut well, each student does not have a shortcut On the instructors computer, each student has a shortcut To the instructors computer.  Since all computers in a class are named the same MIA-123 followed by the seat number -014, and the instructor is always -000, the share on each student computer is simply going to be the computer name MIA-123-014 but replace the last three digits with 000 in the shortcut.

    Sunday, October 26, 2014 6:21 AM
  • So do I run the "grab the first 7" line, the call the strName like this?

    Set objShell = CreateObject("WScript.Shell")

    strName = Left(objShell.Environment("process")("computername"),7)

    strShortcut = objShell.SpecialFolders( "Desktop" ) & "\%username% Share.lnk"
    strShortcut = objShell.ExpandEnvironmentStrings(strShortcut)
    Set objLink = objShell.CreateShortcut( strShortcut )

    objLink.Description = objShell.ExpandEnvironmentStrings("%username% Share")
    objLink.TargetPath = "\\(strName)-000\Users\%username%\Desktop\%username% Share"
    objLink.Save

    Sunday, October 26, 2014 6:25 AM
  • I don't think I am explaining the shortcut well, each student does not have a shortcut On the instructors computer, each student has a shortcut To the instructors computer.  Since all computers in a class are named the same MIA-123 followed by the seat number -014, and the instructor is always -000, the share on each student computer is simply going to be the computer name MIA-123-014 but replace the last three digits with 000 in the shortcut.

    I understand that.  You are pointing the shortcut at the "users" share on the instructors computer.  Why?  This share is for profiles and not for general sharing.  If you want to share files between the instructor and the student then create a new share with a user folder for each user.  Give the instructor access.  To get everyone access just create a shortcut to the share on the instructors computer using the username% semantic and copy it to the ALLUSERS profile on all student machines.  This can be done with a simple batch file and run once when each class is set up.  Just run it from the instructors machine as an admin that can write to all student systems.

    copy <shortcut.lnk> \\MIA-123-001\c$\Users\Public\Desktop

    Create one line for each student PC.  Now all student profiles will get the link back to the instructors machine.  Assume you create a shared folder on 'C' "C:\userdata" and call the share "UserData".  The target of the shortcut will be:

    "\\MIA-123-000\UserData\%username%"

    When we create  a shortcut with a variable and place it in quotes the variable is not converted until the shortcut is executed.  It will reflect the username of the user opening it.

    Just place one line in the batch file for each machine.  Now all you ever have to do is execute that batch file.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 6:37 AM
  • But the MIA-123 changes when I roll the image into another room MIA-124.  And the city changes when I roll it into another location FLL-101.  This is the part I am trying to grab from the current computername.  When I rill an image into a FLL classroom, I can roll out the image and name the computers as part of the rollout.  So it names them FLL-101-###.  If the shortcut can either grab the first 7 characters or replace the last 3 characters with 000, then the shortcuts will always work.  The only thing I have to do is create and share the shortcut folders on the instructors PC.

    Sunday, October 26, 2014 6:47 AM
  • That is why you run it from the instructors machine which will always have the correct name.  Each instructor machine has its own shortcut template file.  It always point to itself.


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 7:07 AM
  • $StudentComputer=$Env:ComputerName
    $StudentComputer="MIA-123-014"   # Reassigned as I don't have your environment (Comment out)
    "StudentComputer=$StudentComputer"
    
    $ServerComputer="$($StudentComputer.SubString(0,7))-000"
    "ServerComputer=$ServerComputer"
    
    $UserName = $Env:UserName
    $UserName = "Student 14"         # Reassigned as I don't have your environment (Comment out)
    "UserName=$UserName"
    
    $ShortCutName = "$Home\Desktop\$UserName Share.lnk"
    "ShortCutName=$ShortCutName"
    $ShortCutTarget = "\\$ServerComputer\Users\$Username\Desktop\$Username"
    "ShortCutTarget=$ShortCutTarget"
    
    
    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut($ShortCutName)
    $Shortcut.TargetPath = $ShortCutTarget
    $Shortcut.Save()
    

    gives this output

    StudentComputer=MIA-123-014
    ServerComputer=MIA-123-000
    UserName=Student 14
    ShortCutName=C:\Users\User\Desktop\Student 14 Share.lnk
    ShortCutTarget=\\MIA-123-000\Users\Student 14\Desktop\Student 14
    

    Sunday, October 26, 2014 7:09 AM
  • The only line not working now in this script is to grab the current computer name, replace the last 3 characters with 000. That and maybe, check the desktop for a shortcut named "\%username% Share", if it exists, do not run this script. 
    Sunday, October 26, 2014 7:13 AM
  • Oh I see, but there are only four profiles on each computer (Student-1, Student-2, Student-3, Student-4).  So if the instructor is using Profile for Student-3, all the students are using Student-3 too. 

    Instructor's Student-3 desktop holds the real folder being shared
    Student's Student-3 desktop has the shortcut \\MIA-123-000\Users\Student-3\Desktop\Student-3 Share

    I just need to capture

    $ServerComputer="$($StudentComputer.SubString(0,7))-000"
    "ServerComputer=$ServerComputer"

    then append the rest of the share path "\Users\%username%\desktop\%username% share" the the Target Path line


    • Edited by Thomas2z Sunday, October 26, 2014 7:30 AM
    Sunday, October 26, 2014 7:29 AM
  • You are mixing batch files variables (%Variable%) and PowerShell variables ($Variable)

    This can not be done in batch files only

    StudentComputer=MIA-123-014
    ServerComputer=MIA-123-000
    UserName=Student 14
    ShortCutName=C:\Users\User\Desktop\Student 14 Share.lnk
    ShortCutTarget=\\MIA-123-000\Users\Student 14\Desktop\Student 14

    These variables and contents define the process, give an example of how you expect to see them?

    There should be parameter for 1 of the 4 profiles?

    Sunday, October 26, 2014 7:40 AM
  • I don't know which are Batch variables and PowerShell variables.  All I know is you were able to get the line ServerComputer=MIA-123-000.  Can this be called for the Target Path and then append the remaining "\Users\%username%\desktop\%username% share" to it?

    Sunday, October 26, 2014 7:49 AM
  • I don't know which are Batch variables and PowerShell variables.  All I know is you were able to get the line ServerComputer=MIA-123-000.  Can this be called for the Target Path and then append the remaining "\Users\%username%\desktop\%username% share" to it?

    Not directly.

    It looks like you are using a batch file for this. From within the batch file, you can call PowerShell

    $ServerComputer="$($StudentComputer.SubString(0,7))-000"
    "ServerComputer=$ServerComputer"

    To get the servercomputer name, that is PowerShell, this can not be done in a batch file

    There are 4 profiles for each student? Each profile requires a shortcut? There is only 1 shortcut on the student's desktop or are there 4 shortcuts for each of the 4 profiles? 

    Give an example of what you expect these values to contain? (Copy and paste and edit)

    StudentComputer=MIA-123-014  (This comes from %ComputName%)
    ServerComputer=MIA-123-000
    UserName=Student 14                (This comes from %UserName%)
    ShortCutName=C:\Users\User\Desktop\Student 14 Share.lnk
    ShortCutTarget=\\MIA-123-000\Users\Student 14\Desktop\Student 14

    Sunday, October 26, 2014 8:06 AM
  • 1 shortcut on each students desktop.  So the lines that say %username% all work okay.
    Sunday, October 26, 2014 8:10 AM
  • The only part below not working is the computername pointing to the instructor machine.
    ======================================================

    Set objShell = CreateObject("WScript.Shell")
    strShortcut
    = objShell.SpecialFolders( "Desktop" ) & "\%username% Share.lnk"
    strShortcut
    = objShell.ExpandEnvironmentStrings(strShortcut)
    Set objLink = objShell.CreateShortcut( strShortcut )
    objLink
    .Description = objShell.ExpandEnvironmentStrings("%username% Share")
    objLink
    .TargetPath = \\%computername%\Users\%username%\Desktop\%username% Share'needs to use the computer name and replace the last 3 digits with 000
    objLink
    .Save
    =======================================================

    Username does not need to be done, just Computername. 

    MIA-123-000\Users\%username%\Desktop\%username% Share"

    Sunday, October 26, 2014 8:17 AM
  • I need to make the Computername in the TargetPath be the current Computername (but replace the last three  characters with 000) and then the rest of the path added to it.
    Sunday, October 26, 2014 8:49 AM
  • objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
    
    objServer=Left(objComputer,7) & "-000"
    
    objLink.TargetPath = objShell.ExpandEnvironmentStrings("\\" & objServer & "\Users\%username%\Desktop\%username% Share")



    • Edited by Brian Nadjiwon Sunday, October 26, 2014 8:55 AM
    • Marked as answer by Thomas2z Sunday, October 26, 2014 9:02 AM
    Sunday, October 26, 2014 8:54 AM
  • IT WORKS ! ! ! ! - Thank you Brian and thank you to JRV.  You have no idea how much this will help me.  Thank You Thank You Thank You
    Sunday, October 26, 2014 9:02 AM
  • Can you tell me how to add a line in the beginning that says: if this is the computername ending in 000, skip this script?
    Sunday, October 26, 2014 9:54 AM
  • objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
    
    IF Right(objComputer,3) = "000" Then
    
    Else
    
    Put script contents here
    
    End If

    Sunday, October 26, 2014 10:04 AM
  • Yes, just like that
    Sunday, October 26, 2014 10:29 AM
  • FYI it gave me an error, I moved the three lines below the first line, is this right?
     -----------------------------------------------------------------------------------------

    Set objShell = CreateObject("WScript.Shell")

    objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
     IF
    Right(objComputer,3) = "000" Then
     Else

     strShortcut = objShell.SpecialFolders( "Desktop" )  & "\%username% Share.lnk"
     strShortcut = objShell.ExpandEnvironmentStrings(strShortcut)

    Set objLink = objShell.CreateShortcut( strShortcut )

     objComputer=objShell.ExpandEnvironmentStrings("%ComputerName%")
     objServer=Left(objComputer,7) & "-000"

     objLink.Description = objShell.ExpandEnvironmentStrings("%username% Share")
     objLink.TargetPath = objShell.ExpandEnvironmentStrings("\\" & objServer & "\Users\%username%\Desktop\%username% Share")
     objLink.Save


    End If

    Sunday, October 26, 2014 11:49 AM
  • You are asking for someone top write a custom script forp you one line at a time. That is in conflict with the purpose of this forum.  Your original question was answered a long time ago.  If you still cannot understand what has been shown to you please contact a consultant to assist with your project.

    Please mark the answer above and close this thread. It is rude and unprofessional to keep adding new questions to the request. YOu are, in effect, demanding free consulting.

    For forum limits please read this: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/a0def745-4831-4de0-a040-63b63e7be7ae/posting-guidelines?forum=ITCG


    ¯\_(ツ)_/¯

    Sunday, October 26, 2014 1:40 PM