How To Use FOR Loop To map Network Shares From Different Networks Resources

Mapping network shares from a network is a pretty easy thing to do. The standard command for this in windows is:

net use x: \\{host}\{share} {password} /u:{unsername} /p:{no|yes}

where:

x = drive letter (a-z)

p = persistent (yes means re-map after system restart and no means the opposite)

Assuming you want to automate such action mapping more shares from different networks, you will want to use the FOR loop in a batch script. These steps will show my aproach:

 

1.) The first thing to do is create and place all your share information (host, share, password, username and drive letters in a file and name the file anything you want. To suite my purpose, I’ll jsut name the file “dat2” without any file extention. The content should look like this:

01,10.11.11.21,username,password,t,share

02,172.16.10.21,username,password,u,share

03,195.34.13.21,username,password,v,share

04,192.168.0.21,username,password,w,share

 

2.) Create a batch script to disconnect all existing network drives. Let’s name this file “delmap.bat”

 

REM ======================= Begin Of Script =============================

@echo off

REM Check and disconnect all existing network drives

cls

if exist t: net use /del t: /y

if exist u: net use /del u: /y

if exist v: net use /del v: /y

if exist w: net use /del w: /y

 

goto :EOF

REM ======================= End Of Script =============================

 

3.) Now, we can create the main script “NetShare.bat” with the following contents:

 

REM ======================= Begin Of Script =============================

 

@echo off

REM To avoid error messages, check and disconnect all existing network drives

REM by calling another batch file named delmap.bat.

cls

call “%~dp0\delmap.bat”

REM To avoid unexpected errors, the system needs to wait 3 seconds after

REM disconnecting all existing drives before re-mapping the network resources.

REM All pre-defined parameters in the file (dat2) will be fetched line by line using

REM the (for) loop. There are 6 columns in the file (dat2) separated by commas (delims).

REM The first is the line numbers (not needed). That is the reason why the tokens is

REM defined from 2-6. The pre-defined variables (hostN, userN, paswd, drive and share) are

REM passed one by one from the ‘sub routine’ (:var) to process the mapping of the drives.

cls

ping -n 3 127.0.0.1>nul

for /F “tokens=2-6 delims=,” %%g IN (%~dp0dat2) do call :var %%g %%h %%i %%j %%k

goto end

:var

set hostN=%1

set userN=%2

set paswd=%3

set drive=%4

set share=%5

net use %drive%: \\%hostN%\%share% %paswd% /U:%userN% /P:no

goto :EOF

:end

exit

REM ======================= End Of Script =============================

 

 

Download:

Both files NetShare.bat and delmap.bat are included in the dowload below:

NetShare

You need to create  your share information (dat2) by following the first step above.


Posted in Windows and tagged , , , , , by with comments disabled.

Switch IP address configurations between two networks using a simple batch program

I use an official Notebook which I often take home when dealing with official projects. The problem is I only have one Network Adaptor or LAN device on this machine. As a result, the network configurations in both locations becomes very tricky. As a network engineer, I know there are other possibilities of solving this problem but I just thought this little batch program might help someone else.

Just copy and past the content between “Start” and “End” on a file using notpad or other editing tools. Save and name the file what you want, run and follow the instructions. Enjoy and share! 😉

:::::::::::::::::::::::::::::::::: Start ::::::::::::::::::::::::::::::::::::::::

@echo off
cls
Echo ————————— Switch IP Address ——————————————————–
Echo (c) 2010 omotech computer services
Echo This Program was written on March 24, 2010 by Stanley O. Omoregie.
Echo It allow the easy switch of IP Address settings between two networks.
Echo The program is copyrighted and can only be used and distributed as it is,
Echo without removing this copyright information.
Echo —————————————————————————————————————–
echo Choose between A und B:
echo [A] Set Static IP Address (Office)
echo [B] Set DHCP (Home)
echo.
echo Choose between A und B:
echo [A] Set Static IP Address (Office)
echo [B] Set DHCP (Home)
:choice
SET /P C=
Echo.
Echo.[A,B]
for %%? in (A) do if /I “%C%”==”%%?” goto A
for %%? in (B) do if /I “%C%”==”%%?” goto B
goto choice
:A
@ECHO off
echo Please enter IP Adresse for choice [A]:
set /P IP_Addr=

echo Default Gateway:
set /P D_Gate=

echo Subnet Mask:
set /P Sub_Mask=

echo Setting static IP Address&
netsh interface ip set address “LAN” static %IP_Addr% %Sub_Mask% %D_Gate% 1
ECHO Here is the setting information for %computername%:
netsh int ip show config
pause
goto end

:B
@ECHO OFF
ECHO DHCP is resetting the IP Address and Subnet Mask.
netsh int ip set address name = “LAN” source = dhcp

ipconfig /renew

ECHO Here is the setting information for %computername%:
netsh int ip show config
pause
goto end

:end
Script END!

:::::::::::::::::::::::::::::::::: End ::::::::::::::::::::::::::::::::::::::::

I made an EXE file from the batch program. You can download this to get both files.


Posted in Networking and tagged , , by with comments disabled.

How to use rsync with progressbar

It becomes very frustrating when copying large files with “cp” because of the lack of progressbar. With “rsync” in contrast, you can toggle verbose mode with ‘-v’ to enable feedback through the progressbar. So when copying several files, you will be able to know which files are done. Unfortunately, there’s no single-file progress to be seen.

Using rsync, you can. And it’s available on nearly every linux release, too.

Here’s the syntax difference:

cp <oldfile> <newfile>
rsync --progress <oldfile> <newfile>

rsync would then look like this:

It gives you an estimated time remaining, as well as a speed indication. And it shouldn’t affect your total copy time by that much, either.

For more details on parameters and syntax, click here


Posted in Vista, Windows, Windows 7, Windows Vista and tagged , , by with comments disabled.