none
How to run a command line for every folder in a share RRS feed

  • Question

  • I'm trying to use Robocopy to merge two user home directories shares from two different servers to a new server, but the only problem is that I can't use the /purge to mirror deletes because it will delete the other servers stuff (since one server's stuff doesn't exist on the other one currently).

    But I'm thinking if someone I could enumerate the folder names from the users$ share on each server, and add it to both path statements in the commands below, i could add purge since then it would be running individually per user folder, rather than at the user level below.

    robocopy "\\CurrentServer1\users$" "F:\Users" /E /B /COPY:DATSOU /R:1 /W:1 /MT /LOG:"C:\RobocopyLogs\MirrorUsers1.txt"

    robocopy "\\CurrentServer2\users$" "F:\Users" /E /B /COPY:DATSOU /R:1 /W:1 /MT /LOG:"C:\RobocopyLogs\MirrorUsers2.txt

    What I'm hoping is that there's some scripting genius out there who could tell me how to use a batch or vbscript to save a LOT of time.  If not, I'm doing this all by hand for hundreds of users :(

    Thanks in advance for any help you can provide.

    Friday, October 17, 2014 2:01 PM

Answers

  • That's what I figured, thanks Mike.

    Hopefully this thread hasn't died off with "learn powershell" as the "solution"

    There used to be some die-hard batch guys around who loved to build convoluted and tricky batch script.  I have give up on doing batch because it is such a waste of time.

    Here is an example:

    Get-ChildItem \\wserver1\Users$ -Directory | ForEach{ $source1="\\server1\Users$\$($_.Name)"
    $source2="\\server2\Users$\$($_.Name)" $target="F:\Users\$($_.Name)" robocopy $source1 $target *.* /E /B /COPY:DATSOU /R:1 /W:1 /MT /LOG:C:\RobocopyLogs\MirrorUsers1.txt         robocopy $source2 $target *.*  /E /B /COPY:DATSOU /R:1 /W:1 /MT /LOG:C:\RobocopyLogs\MirrorUsers1.txt
    }


    That will get every folder under the share and copy it as a separate source to a separate target.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, October 17, 2014 8:17 PM
    • Marked as answer by RJO22 Monday, October 20, 2014 6:06 PM
    Friday, October 17, 2014 8:17 PM

All replies

  • Clear as mud.

    Just use Robocopy to merge everything without a mirror.  That wauy both will have complete copies of everything.


    ¯\_(ツ)_/¯

    Friday, October 17, 2014 3:33 PM
  • Not understanding your reply, I'm not using the mirror option in Robocopy to begin with.  I'm merging the two existing user directory root folders into one on a new server, not into two separate folders on the new server.

    But in regards to my original question, using some online examples I did create the following batch file, but this gives me the full network path of \\CurrentServer1\Users$\UserNameA rather than just UserNameA, which is what I want.  Is there a way to have it just return just the part after the last slash for each folder instead?

    @echo off
    for /d %%a in (\\CurrentServer1\Users$\*) do call :Sub %%a
    pause
    goto :eof

    :Sub
    set Folder=%*
    for /d %%b in ("%Folder%") do (
       echo Found %%b
    )

    Friday, October 17, 2014 3:47 PM
  • Making a little progress here.  Figured out how to truncate with the following code in the Sub, but have a problem with what it outputs in the beginning, and then due to this never outputs the end results completely.  I'm hoping if I can echo what I need, I can work that into the command line for robocopy.

    The code:

    :Sub
    set Folder=%*
    for /d %%b in ("%Folder%") do (
       echo Found %%b
       Set ToTruncate=%%b
       echo %ToTruncate%
       Set Truncated=%ToTruncate:~25,-1%
       echo %Truncated%
    )

    The beginning of the problem output (where it gets behind due to saying echo is off and the truncate parameters):

    Found "\\CurrentServer1\Users$\User1"
    ECHO is off.
    ECHO is off.
    Found "\\CurrentServer1\Users$\User2"
    "\\CurrentServer1\Users$\User1"
    ~25,-1
    Found "\\CurrentServer1\Users$\User3"
    "\\CurrentServer1\Users$\User2"
    User1
    Found "\\CurrentServer1\Users$\User4"
    "\\CurrentServer1\Users$\User3"
    User2
    Found "\\CurrentServer1\Users$\User5"
    "\\CurrentServer1\Users$\User4"
    User3


    • Edited by RJO22 Friday, October 17, 2014 4:38 PM
    Friday, October 17, 2014 4:38 PM
  • This gets you a list of all folder names on a share:

    Get-ChildItem  \\w8test\scripts -Directory |%{$_.Name}

    It only takes one simple line.


    ¯\_(ツ)_/¯

    Friday, October 17, 2014 6:52 PM
  • One line would be great!   But I don't know how to work that into the batch file I have going that I've pasted above.  Can you show me?
    Friday, October 17, 2014 6:54 PM
  • The line of code jrv posted is PowerShell, not shell script (batch).

    PowerShell is much more flexible and powerful than cmd.exe shell scripting (batch).


    -- Bill Stewart [Bill_Stewart]

    Friday, October 17, 2014 6:58 PM
    Moderator
  • If you don't know batch don't waste you time one it.  It is a dead issue.  PowerShell is part of ll newer OSs.  Win 7 and later come with PS installed.  For what you are doing it will make your life easier.

    ¯\_(ツ)_/¯


    • Edited by jrv Friday, October 17, 2014 7:11 PM
    Friday, October 17, 2014 7:11 PM
  • Well, batch is the only thing I'm mildly familiar with, so that's why I went that route.  What would you guys suggest I do?
    • Edited by RJO22 Friday, October 17, 2014 7:15 PM
    Friday, October 17, 2014 7:14 PM
  • We would suggest you start learning PowerShell. Your time investment will be well worth it.

    Here are some resources to start you off:

    http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx

    http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start


    -- Bill Stewart [Bill_Stewart]

    Friday, October 17, 2014 7:20 PM
    Moderator
  • OK.  I guess in the short term I'll just keep working on this batch file, since I'm somewhat in a time crunch.  Thanks for the tip on learning powershell.
    Friday, October 17, 2014 7:22 PM
  • Mike and Bill are right on with your approach however, to show you why your code is not working I removed all "Smart Quotes" in favor of the following.

    Refrain from commenting every line.  It makes the script impossible to follow.

    $hostname='hostname'
    $migrationdata='c:\migrate\IPDATA.txt'
    $ip=Select-String -Path C:\migrate\IPDATA.txt -Pattern $hostname
    $ip = $ip.Substring(35)
    $ip = $ip.Trim()
    
    $dns='10.10.1.10','10.10.1.11'
    $gateway='10.10.4.1'
    $netmask='255.255.252.0'
    $dnsdomain='new.domain.local'
    
    $msg='We will be using the following information for the new IP stack Hostname:{0}, IP address {1} Default Gateway: {2} DNS Servers: {4} DNS Suffix: {5}'
    Write-Output ($msg -f $hostname,$Ip,$Gateway,$DNS,$DNSdomain)
    $Config=Read-Host 'Please press enter if this is correct, and we will apply this configuration. or CTRL-C to exit'
    
    #configure the Ip stack
    $Interface = Get-WmiObject win32_networkadapterconfiguration -filter 'IPEnabled=True'
    $Interface.EnableStatic($ip, $netmask)
    $Interface.SetGateways($Gateway, 1)
    $Interface.SetDNSServerSearchOrder($dns)
    $Interface.SetDNSDomain($dnsdomain)
    $Interface.SetDynamicDNSRegistration($true)
    
    #Show the user the new IP configuration
    ipconfig /allcompartments /all
    
    $Config = Read-Host 'We have reconfigured the NIC.  Does the IP configuration for the "Local Area Connection" look right?  Press enter to continue to join the domain, or CTRL-C to exit'

    Use sample white space to make code readable.

    Much of this code does not make sense.  You should be using DHCP to set all workstations and most servers with the exception of Domain Controllers.

    It will be faster to do this with PowerShell since you do not know batch.  Your attempt is not even close.



    ¯\_(ツ)_/¯

    Friday, October 17, 2014 7:27 PM
  • Was this for my post or a different post?
    Friday, October 17, 2014 7:37 PM
  • Was this for my post or a different post?

    Looks like that response was meant for a different thread.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Friday, October 17, 2014 7:42 PM
  • That's what I figured, thanks Mike.

    Hopefully this thread hasn't died off with "learn powershell" as the "solution"

    Friday, October 17, 2014 7:44 PM
  • That's what I figured, thanks Mike.

    Hopefully this thread hasn't died off with "learn powershell" as the "solution"

    There used to be some die-hard batch guys around who loved to build convoluted and tricky batch script.  I have give up on doing batch because it is such a waste of time.

    Here is an example:

    Get-ChildItem \\wserver1\Users$ -Directory | ForEach{ $source1="\\server1\Users$\$($_.Name)"
    $source2="\\server2\Users$\$($_.Name)" $target="F:\Users\$($_.Name)" robocopy $source1 $target *.* /E /B /COPY:DATSOU /R:1 /W:1 /MT /LOG:C:\RobocopyLogs\MirrorUsers1.txt         robocopy $source2 $target *.*  /E /B /COPY:DATSOU /R:1 /W:1 /MT /LOG:C:\RobocopyLogs\MirrorUsers1.txt
    }


    That will get every folder under the share and copy it as a separate source to a separate target.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, October 17, 2014 8:17 PM
    • Marked as answer by RJO22 Monday, October 20, 2014 6:06 PM
    Friday, October 17, 2014 8:17 PM
  • jrv, thank you!  I will give this a shot.   For not knowing powershell, even that makes sense.  Thanks for saving me this time until I get more time to learn powershell properly.
    Friday, October 17, 2014 9:03 PM
  • jrv, thank you!  I will give this a shot.   For not knowing powershell, even that makes sense.  Thanks for saving me this time until I get more time to learn powershell properly.

    Ahhhh...but the catch-22 is that you have to modify it to your system and adjust it to what you need to do.  To do that you have to understand it.  Just like a batch script only easier.

    ¯\_(ツ)_/¯

    Friday, October 17, 2014 11:00 PM
  • jrv, after some minor tweaking, it worked PERFECTLY.  Thanks for the help!!
    Monday, October 20, 2014 6:06 PM
  • Great! PowerShell is a great enhancer for all other technologies and it is easy to use.


    ¯\_(ツ)_/¯

    Monday, October 20, 2014 6:09 PM