# Solved: Script Issues



## Chained (Feb 20, 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:
S:\userdata\C\Chained.Phoenix

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
)
EndLocal

-------------------------------------
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?!?!


----------



## foxidrive (Oct 20, 2012)

With delayed expansion:

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


----------



## Chained (Feb 20, 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

:start
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

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

:end


----------



## Squashman (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.


----------

