Page 1 of 1
Mapped Network Drives and Batch Files
Posted: 28 Apr 2013 19:20
by Meimnioch
Good day,
I am wanting to discover a way to copy mapped drives from one computer and copy them over to a new computer.
For example: UserA has a computer with Windows 7, which needs to be rebuilt. I run a batch file to copy the mapped network drives. Then I rebuild the computer with a fresh installation of Windows 7, run another batch file that remapps the drives to the new installation of Windows 7.
Any help, advice, or direction recommened would be well appriciated.
Kindest Regards,
Matthew
Re: Mapped Network Drives and Batch Files
Posted: 28 Apr 2013 21:41
by abc0502
This Batch Should Backup & Restore All Your mapped Drives.
It Backup the Mapped drives in a .log file in the same folder where the batch exist, and it reads it to restore the resources.
Backup & Restore Mapped DrivesCode: Select all
@ECHO OFF
REM Log File That Will Contain Mapped Resources
SET "LogFile=MappedResources.log"
REM Main Menu
:Menu
CLS
Color 0A
Echo.
Echo.
Echo Backup ^& Restore Mapped Resources
Echo ==================================
Echo.
Echo Select Operation:
Echo.
Echo 1^> Backup Mapped Resources.
Echo 2^> Restore Mapped Resources.
Echo 3^> Exit.
Echo.
SET "cho="
SET /P "cho= Select: "
IF /I "%cho%" == "1" Goto :BackupMD
IF /I "%cho%" == "2" Goto :RestoreMD
IF /I "%cho%" == "3" Goto :EOF
CLS
Color 0C
Echo.&Echo.&Echo.&Echo Error! , No Such Option.
Ping Localhost -n 4 >NUL
GOTO :Menu
:BackupMD [ Backup Mapped Resources to Log File ]
REM =====[ Backup Mapped Resources ]===============================
:: Check for any existence of a Mapped Resources in the system.
:: We do nothing if Mapped Resources was not found.
CLS
SETLOCAL EnableDelayedExpansion
for /f "skip=6 tokens=1-4* delims= " %%A in ('net use') Do (
REM %%A is Status, %%B is Local Drive Letter, %%C is Remote Share, %%D and %%E is Network Name
SET "Line=%%A %%B %%C %%D %%E"
SET "LocalDrive=%%B"
SET "Remote=%%C"
REM Leave a single space after the word "successfully."
IF "!Line!" NEQ "The command completed successfully. " (
Echo %%B?%%C>>"%LogFile%"
)
)
Echo.&Echo.&Echo.&Echo All Done! , Check %LogFile%.
Ping Localhost -n 6 >NUL
GOTO :Menu
:RestoreMD [ Restore Mapped Resources from Log File ]
REM =====[ Restore Mapped Resources ]===============================
Rem Read The .log File to restore
CLS
For /F "tokens=1* delims=?" %%A In (' Type "%LogFile%" ') Do (
REM %%A is shared Name, %%B is The Location of shared resource
NET USE "%%A" "%%B" >NUL
)
Echo.&Echo.&Echo.&Echo All Done!.
Ping Localhost -n 6 >NUL
GOTO :Menu
This One I made it first, I didn't read carefully your post and thought you wanted to backup and restore your shared folders.
So here it is in case someone else wanted it.
It's basically the same idea.
Backup & Restore Shared ResourcesCode: Select all
@ECHO OFF
REM Log File That Will Contain Shared Resources
SET "LogFile=SharedResources.log"
REM Main Menu
:Menu
CLS
Color 0A
Echo.
Echo.
Echo Backup ^& Restore Shared Resources
Echo ==================================
Echo.
Echo Select Operation:
Echo.
Echo 1^> Backup Shared Resources.
Echo 2^> Restore Shared Resources.
Echo 3^> Exit.
Echo.
SET "cho="
SET /P "cho= Select: "
IF /I "%cho%" == "1" Goto :BackupSR
IF /I "%cho%" == "2" Goto :RestoreSR
IF /I "%cho%" == "3" Goto :EOF
CLS
Color 0C
Echo.&Echo.&Echo.&Echo Error! , No Such Option.
Ping Localhost -n 4 >NUL
GOTO :Menu
:BackupSR [ Backup Shared Resources to Log File ]
REM =====[ Shared Resources ]========================================
:: Check for any existence of a shared folders in the system.
:: We do nothing if shared resources was not found.
CLS
SETLOCAL EnableDelayedExpansion
REM we use /format:csv to remove trailling spaces WMIC leave it after each result
REM review Post: http://www.dostips.com/forum/viewtopic.php?p=24169#p24169
FOR /F "skip=2 tokens=1,* delims=," %%A IN ('WMIC SHARE GET Name /format:csv') DO (
REM %%B is the shared folder's name
FOR /F "delims=" %%Z IN ("%%B") DO SET "ShareName=%%Z"
REM we check if sharename isn't IPC$ or empty line
IF "!ShareName:~0,3!" NEQ "IPC" (
IF /I "!Sharename!" NEQ "" (
Call :ShareLocation "!ShareName!" "ShareLocation"
)
)
)
Echo.&Echo.&Echo.&Echo All Done! , Check %LogFile%.
Ping Localhost -n 6 >NUL
GOTO :Menu
:ShareLocation
FOR /F "skip=2 tokens=1,* delims=," %%A IN ('WMIC SHARE where name^="%~1" GET Path /format:csv') DO (
FOR /F "delims=" %%Z IN ("%%B") DO (
SET "%~2=%%Z"
REM We use %%~nxZ instead of %~1 as the last will remove the last char was ).
Echo %%~nxZ/!ShareLocation!>>"%LogFile%"
)
)
GOTO :EOF
:RestoreSR [ Restore Shared Resources from Log File ]
REM =====[ Restore Shared Resources ]===============================
:: Make Sure The Shared Folders and Files Exist
Rem Read The .log File to restore
CLS
For /F "tokens=1* delims=:" %%A In (' Type "%LogFile%" ') Do (
REM %%A is shared Name, %%B is The Location of shared resource
NET SHARE "%%A"="%%B" >NUL
)
Echo.&Echo.&Echo.&Echo All Done!.
Ping Localhost -n 6 >NUL
GOTO :Menu