1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Script Issues

Discussion in 'DOS/Other' started by Chained, Feb 19, 2013.

Thread Status:
Not open for further replies.
  1. Chained

    Chained Thread Starter

    Feb 19, 2013
    Hi guys,

    I've been trying to get a script going on a server which doesn't allow me to run anything but robocopy and command prompt commands.

    We basically have a small server with all it's users under a single folder going to a large server with all it's user drives seperated into their first letter then name. For example:

    D:\userdata\Chained.Phoenix will go to:

    I've got a script working which does everything except the letter at the start. Is anyone able to help? My current script is as follows:

    @Echo Off
    SetLocal EnableDelayedExpansion
    SetLocal InputFile=%1
    SetLocal Domain=%2
    For /F "tokens=*" %%I IN (%InputFile%) DO (
    mkdir s:\userdata\%%I
    cacls s:\userdata\%%I /g %Domain%\%%I:W
    robocopy d:\userdata\%%I s:\userdata\%%I /e

    I've tried doing "set first=%I:0,1% which works outside of the for loop but isn't much use there. Are you able to set part of a variable to a variable with-in a for loop?!?!
  2. foxidrive

    foxidrive Banned

    Oct 20, 2012
    With delayed expansion:

    set "var=%%I"
    set "var=!var:~0,1!"
    echo !var!
  3. Chained

    Chained Thread Starter

    Feb 19, 2013
    Thanks for that foxidrive!

    Just in case it's ever useful for anyone else here is my full script (changed the comments to echos for the hell of it):

    @Echo off
    Echo Please use this file ONLY if you know what you're doing!
    Echo This should only be run on the server where the source files are located!
    Echo The file must be run with an account which has full access to the source and destination servers!
    Echo The first argument is the txt file with usernames, the 2nd is the domain, and the 3rd is the destination.
    Choice /M "Do you wish to continue?"
    If errorlevel 2 goto end
    If errorlevel 1 goto start
    goto end

    SetLocal EnableDelayedExpansion
    Echo Checking values were set.
    If [%3]==[] goto missed
    Echo Setting variables from command line and mapping the S drive to the destination server.
    Set File=%1
    Set Dom=%2
    Set Dest=%3
    net use s: \\userdata.!dest!\userdata
    For /f "tokens=*" %%I IN (%File%) Do (
    Echo Setting the first letter of the username to a variable
    set "first=%%I"
    set "first=!first:~0,1!"
    Echo Making the folder on the destination server and setting it's permissions
    mkdir S:\!first!\%%I
    cacls S:\!first!\%%I /e /g !Dom!\%%I:C
    Echo Copying the data to the destination server
    robocopy d:\userdata\%%I s:\!first!\%%I /e
    Echo Renaming the old data folder
    ren d:\userdata\%%I _%%I
    Echo Please update the users in Active Directory!
    goto end

    Echo Command line entry missing, I thought you said you could use this file!
    goto end

  4. Squashman

    Squashman Trusted Advisor

    Apr 4, 2003
    You don't need to use delayed expansion for the DOM or DEST variables.

    You could probably do some error checking to make sure they put the order of arguments in the correct order.
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1090257

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice