none
Comparing two "strings" in this case paths to determine difference RRS feed

  • Question

  • Hi

    Need some help with writing some logic (or a function/sub) to compare two strings (paths)

    Let me try and explain:-

    I have a tool which backs up my data to lets say Z:\Backup\ (this path could potentially be anything) e.g. D:\MyBackup\ , X:\DarrensData\Today\ etc etc

    So if I back up my Documents folder from C: drive then it recreates the parent paths, so I would end up with Z:\Backup\Users\Darren\Documents (or using other examples above D:\MyBackup\Users\Darren\Documents or X:\DarrensData\Today\Users\Darren\Documents)

    So if I then wanted to use same tool to reverse the operation I need to modify the path (which I can do easily enough), but I want to automate doing this

    So let's say I wanted to restore my Documents folder from Z:\Backup\Users\Darren\Documents to my C:\ drive - doing a direct copy (remembering it will recreate parent paths) I will end up with C:\Backup\Users\Darren\..... rather than C:\Users\Darren\ which is what I would need

    As I say changing the path in the program isn't a problem and manually I obviously know I need to remove the \Backup part and it will be fine, but how to do this automatically when paths could be anything?

    Obviously we can discount the first bit e.g. C:\ and Z:\ as not relevant

    And we know the \Users\ part will be in both paths

    So how to automatically compare "Backup\Users\Darren\Documents" and "\Users\Darren\Documents" to remove anything before \Users\?

    Also to then further complicate the logic what if username is different so after we find and remove bit before \Users\ we need to check next bit to make sure \Darren\ and say \Darren Rose\ are different so I can then fix path as well

    I really hope this make sense to someone, and I know it is a complex puzzle but would like to hear comments from people who love string manipulation and comparison as I am sure it can be done

    I have started simply by using .Remove(0, 3) to get rid of drive letter but still trying to work out how to compare text before a chosen string e.g. \Users\


    Darren Rose

    Friday, February 24, 2017 7:03 PM

Answers

  • Does the backup/restore path always start with the Users folder (ignoring anything preceding that)? If so, you can start by using a string function Instr (VB) or String.IndexOf to locate the start location of the path. Then you can use String.Right to grab the original backup path. Am I on the right path here as what you want to do?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by wingers Friday, February 24, 2017 10:48 PM
    Friday, February 24, 2017 8:13 PM
  • Does the backup/restore path always start with the Users folder (ignoring anything preceding that)? If so, you can start by using a string function Instr (VB) or String.IndexOf to locate the start location of the path. Then you can use String.Right to grab the original backup path. Am I on the right path here as what you want to do?


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Hi Paul

    Yes the path would always start with \Users\ if we ignore anything preceding it

    Thanks I will have a look at IndexOf and Instr

    Yes definitely on the right path


    Darren Rose

    OK, so once you extract the path containing Users and everything after that you could use the Split method to create an array of your subfolders. This would allow you to compare the second folder, containing the user ID, to the user ID logged on to the system (System.Environment.UserName). This would allow you to replace it if different as well.

    Also, keep in mind that you can fetch the path to the user folder with System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile)


    Paul ~~~~ Microsoft MVP (Visual Basic)


    • Edited by Paul P Clement IV Friday, February 24, 2017 9:25 PM remove crapped up html
    • Marked as answer by wingers Friday, February 24, 2017 10:48 PM
    Friday, February 24, 2017 9:25 PM

All replies

  • Does the backup/restore path always start with the Users folder (ignoring anything preceding that)? If so, you can start by using a string function Instr (VB) or String.IndexOf to locate the start location of the path. Then you can use String.Right to grab the original backup path. Am I on the right path here as what you want to do?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by wingers Friday, February 24, 2017 10:48 PM
    Friday, February 24, 2017 8:13 PM
  • So let's say I wanted to restore my Documents folder from Z:\Backup\Users\Darren\Documents to my C:\ drive - doing a direct copy (remembering it will recreate parent paths) I will end up with C:\Backup\Users\Darren\..... rather than C:\Users\Darren\ which is what I would need

    For the first part of the task you simply use Replace and replace "Z:\Backup" with "C:".   However, because of the scheme you are using you will need to ask the user for the drive letter.

    A simpler procedure might be to create an arbitrary folder within Z:\Backup, using a random name generator, then keep an index that maps the arbitrary backup folder to the full original path.

    Also to then further complicate the logic what if username is different so after we find and remove bit before \Users\ we need to check next bit to make sure \Darren\ and say \Darren Rose\ are different so I can then fix path as well

    That cannot be automatic.  There is no way of knowing what folder in Z:\Backup\Users you should select.  You will need to confirm with the user that the name has changed, and ask for the original name and the new name. Then you can use replace again.

    Friday, February 24, 2017 8:24 PM
  • Does the backup/restore path always start with the Users folder (ignoring anything preceding that)? If so, you can start by using a string function Instr (VB) or String.IndexOf to locate the start location of the path. Then you can use String.Right to grab the original backup path. Am I on the right path here as what you want to do?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Hi Paul

    Yes the path would always start with \Users\ if we ignore anything preceding it

    Thanks I will have a look at IndexOf and Instr

    Yes definitely on the right path


    Darren Rose

    Friday, February 24, 2017 8:32 PM
  • So let's say I wanted to restore my Documents folder from Z:\Backup\Users\Darren\Documents to my C:\ drive - doing a direct copy (remembering it will recreate parent paths) I will end up with C:\Backup\Users\Darren\..... rather than C:\Users\Darren\ which is what I would need

    For the first part of the task you simply use Replace and replace "Z:\Backup" with "C:".   However, because of the scheme you are using you will need to ask the user for the drive letter.

    A simpler procedure might be to create an arbitrary folder within Z:\Backup, using a random name generator, then keep an index that maps the arbitrary backup folder to the full original path.

    Also to then further complicate the logic what if username is different so after we find and remove bit before \Users\ we need to check next bit to make sure \Darren\ and say \Darren Rose\ are different so I can then fix path as well

    That cannot be automatic.  There is no way of knowing what folder in Z:\Backup\Users you should select.  You will need to confirm with the user that the name has changed, and ask for the original name and the new name. Then you can use replace again.

    Hi Acamar

    Can't "simply" replace Z:\backup with c:\ as "backup" not a set thing, it could be anything before \users\

    Yes I had thought of creating an ini file that holds original path during backup, and then restore can read this later to get inform needed - but if file lost/deleted etc then would fail

    As for username, we can get current user name using Environment.UserName or we can get current profile path using Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) and we know the backed up user name by looking at the backed up path - so I'm sure somehow it can be automated


    Darren Rose

    Friday, February 24, 2017 8:35 PM
  • Okay so I can use something like below (Thanks Paul) to remove the z:\......\ from z:\......\users

    But how can I use same idea to actually get the bit before users as I would need to know the z:\.....\ bit to the pass to my code to replace it with say c:\

     Dim BackupPathUsersIndex As Integer = (BackupPath.IndexOf("\Users"))
            MsgBox(BackupPath.Remove(0, BackupPathUsersIndex))


    Darren Rose

    Friday, February 24, 2017 8:45 PM
  • Darren,

    Looks like you have some good replies here to get you going.

    The only thing that I'll add is that if you get heavily involved with string manipulation that you might want to use a System.Text.StringBuilder (because a string is immutable but a StringBuilder isn't).


    "One who has no vices also has no virtues..."

    Friday, February 24, 2017 8:57 PM
  • I think if you stored your initial values separately you would avoid this problem.
    Yes agreed, but I like to make life more interesting!!! and it is good to learn more about string manipulation

    Darren Rose

    Friday, February 24, 2017 9:01 PM
  • Darren,

    Looks like you have some good replies here to get you going.

    The only thing that I'll add is that if you get heavily involved with string manipulation that you might want to use a System.Text.StringBuilder (because a string is immutable but a StringBuilder isn't).


    "One who has no vices also has no virtues..."

    Fair point, thanks Frank

    Darren Rose

    Friday, February 24, 2017 9:01 PM
  • Can't "simply" replace Z:\backup with c:\ as "backup" not a set thing, it could be anything before \users\

    Then you have no option but to ask the user what it is, and use Replace with the user's response. Or, create a file that stores that information.   You cannot safely identify the backup folder in any other way - a user could likely have a folder "Z:\Old Documents\Users\..." that is not in any way associated with your backup facility. There could be multiple folders that follow that naming pattern, so you wouldn't know which one to choose.

    Yes I had thought of creating an ini file that holds original path during backup, and then restore can read this later to get inform needed - but if file lost/deleted etc then would fail

    Then keep multiple copies, or do it in the registry.  Any damage to the file system is likely to affect a backup.

    As for username, we can get current user name using Environment.UserName or we can get current profile path using Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) and we know the backed up user name by looking at the backed up path - so I'm sure somehow it can be automated

    Getting the username for the restore location is not the problem - the problem is finding the folder to restore from.  You don't know what the backed-up path is unless you have kept that as separate information (or you ask the user), so you can't automatically get the username from it.

    Friday, February 24, 2017 9:06 PM
  • Then you have no option but to ask the user what it is, and use Replace with the user's response. Or, create a file that stores that information.   You cannot safely identify the backup folder in any other way - a user could likely have a folder "Z:\Old Documents\Users\..." that is not in any way associated with your backup facility. There could be multiple folders that follow that naming pattern, so you wouldn't know which one to choose.


    sorry didn't fully explain - yes I do know the backup path as first part of app you browse to backup location so then you would know the path e.g. Z:\backup or whatever it may be

    Darren Rose


    • Edited by wingers Friday, February 24, 2017 9:18 PM
    Friday, February 24, 2017 9:08 PM
  • Then keep multiple copies, or do it in the registry.  Any damage to the file system is likely to affect a backup.

    Think if I did it then it would need to be a file in root of backup directory rather than registry, as reason for restoring backup could be after PC reinstall on or a new computer - so registry information would be lost


    Darren Rose

    Friday, February 24, 2017 9:19 PM
  • Okay so using Paul's idea I can now find first part of path and what to replace it with e.g. z:\backup\users\darren\documents restoring to c:\

    But still need to work out how to pull out the \darren\ part of the string so I can compare it with  Environment.UserName and if different then change (or prompt) to change it to correct name - so in my example code below to replace the \David\ with current username which would be \Darren Rose\

    Dim ProfilePath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
            
    Dim BackupPath As String = "Z:\Backup\Users\David\Documents"
           
    Dim username As String = Environment.UserName
          
    ' get index of where \Users is in string
    Dim BackupPathUsersIndex As Integer = (BackupPath.IndexOf("\Users"))
    ' can then use that index to remove that part
    Dim BackupPath_End As String = BackupPath.Remove(0, BackupPathUsersIndex)
    ' or can use index with substring to get the first part
    Dim BackupPath_Start As String = BackupPath.Substring(0, BackupPathUsersIndex)
    
    Dim ProfilePathUsersIndex As Integer = (ProfilePath.IndexOf("\Users"))
    Dim ProfilePath_End As String = (ProfilePath.Remove(0, ProfilePathUsersIndex))
    Dim ProfilePath_Start As String = (ProfilePath.Substring(0, ProfilePathUsersIndex))
    
    
    tbresult.AppendText("Original: " & BackupPath & vbCrLf)
    
    tbresult.AppendText("Result: " & ProfilePath_Start & BackupPath_End & vbCrLf)
    
    tbresult.AppendText("So would replace " & BackupPath_Start & " with " & ProfilePath_Start & vbCrLf)


    Darren Rose

    Friday, February 24, 2017 9:23 PM
  • Does the backup/restore path always start with the Users folder (ignoring anything preceding that)? If so, you can start by using a string function Instr (VB) or String.IndexOf to locate the start location of the path. Then you can use String.Right to grab the original backup path. Am I on the right path here as what you want to do?


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Hi Paul

    Yes the path would always start with \Users\ if we ignore anything preceding it

    Thanks I will have a look at IndexOf and Instr

    Yes definitely on the right path


    Darren Rose

    OK, so once you extract the path containing Users and everything after that you could use the Split method to create an array of your subfolders. This would allow you to compare the second folder, containing the user ID, to the user ID logged on to the system (System.Environment.UserName). This would allow you to replace it if different as well.

    Also, keep in mind that you can fetch the path to the user folder with System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile)


    Paul ~~~~ Microsoft MVP (Visual Basic)


    • Edited by Paul P Clement IV Friday, February 24, 2017 9:25 PM remove crapped up html
    • Marked as answer by wingers Friday, February 24, 2017 10:48 PM
    Friday, February 24, 2017 9:25 PM
  • Okay so using Paul's idea I can now find first part of path and what to replace it with e.g. z:\backup\users\darren\documents restoring to c:\

    But still need to work out how to pull out the \darren\ part of the string so I can compare it with  Environment.UserName and if different then change (or prompt) to change it to correct name - so in my example code below to replace the \David\ with current username which would be \Darren Rose\


    Darren Rose

    My follow-up post might help you with that (using Split method).

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, February 24, 2017 9:31 PM
  • If you backup everything in C:\Users\Darren\Documents\ to J:\Backup-02-24-2017\, you could create a text file in J:\Backup-02-24-2017\ name it something like RestorePath.txt. its contents would be the base folder you are backing up - then when you restore, the first thing to do is find that file and read its contents so you know the initial path to restore to.

    Example - You backup C:\Users\Darren\Documents\
    Subfolders:
    Spreadsheets
    MSWord
    Access
    Finance

    Your backup destination is J:\Backup-02-24-2017\
    Subfolders Created during the backup:
    Spreadsheets
    MSWord
    Access
    Finance
    RestorePath.txt contains "C:\Users\Darren\Documents\"
    When creating the restore path, use String.Replace("J:\Backup-02-24-2017","C:\Users\Darren\Documents\") on each file found in the backup folder.

    Friday, February 24, 2017 9:58 PM
  • Does the backup/restore path always start with the Users folder (ignoring anything preceding that)? If so, you can start by using a string function Instr (VB) or String.IndexOf to locate the start location of the path. Then you can use String.Right to grab the original backup path. Am I on the right path here as what you want to do?


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Hi Paul

    Yes the path would always start with \Users\ if we ignore anything preceding it

    Thanks I will have a look at IndexOf and Instr

    Yes definitely on the right path


    Darren Rose

    OK, so once you extract the path containing Users and everything after that you could use the Split method to create an array of your subfolders. This would allow you to compare the second folder, containing the user ID, to the user ID logged on to the system (System.Environment.UserName). This would allow you to replace it if different as well.

    Also, keep in mind that you can fetch the path to the user folder with System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile)


    Paul ~~~~ Microsoft MVP (Visual Basic)


    Excellent thanks Paul that works perfectly for me

    Darren Rose

    Friday, February 24, 2017 10:12 PM
  • If you backup everything in C:\Users\Darren\Documents\ to J:\Backup-02-24-2017\, you could create a text file in J:\Backup-02-24-2017\ name it something like RestorePath.txt. its contents would be the base folder you are backing up - then when you restore, the first thing to do is find that file and read its contents so you know the initial path to restore to.

    Yes I think in the end I may create a file in backup containing original path - but it was handy solving how to do it without as well - thanks

    Darren Rose

    Friday, February 24, 2017 10:12 PM