创建另一个批处理文件的批处理文件

时间:2014-01-31 06:25:31

标签: batch-file logging ping

我正在尝试创建一个批处理文件,以帮助在启动时映射驱动器 基本上它会将一个批处理文件写入运行它的计算机的启动文件夹中。

@Echo Off
cls
title Set Network drive 1.0
color 0A

rem "rem" is a message that can only bee seeing when editing a batch file. It will not        be includded when running a batch file.

rem =====saving log file to your desktop==========


set log="%USERPROFILE%\Desktop\Set Network Drive 1.0.log"
echo.>>%log%

echo Starting log at %date% %time%>>%log%
echo.>>%log%

rem ==================Detecting OS type===========================

echo.
echo =====================Detection OS type=================
echo =====================Detection OS type=================>>%log%
echo.
echo.>>%log%
color 0c
echo One second please.......
echo.
for /f "tokens=2 delims=:" %%a in ('systeminfo ^| find "OS Name"') do set OS_Name=%%a
for /f "tokens=* delims= " %%a in ("%OS_Name%") do set OS_Name=%%a
for /f "tokens=3 delims= " %%a in ("%OS_Name%") do set OS_Name=%%a



rem ===========Setting OS type to use as variable %variable%=====


if "%os_name%"=="XP" set verssion=xp
if "%os_name%"=="7" set version=7

if "%version%"=="XP" set desktop=xp
if "%version%"=="7" set desktop=7


cls
color 0A
echo.
echo Operating system detected.............. Windows %version%
echo Operating system detected.............. Windows %version% >>%log%
echo.>>%log%

rem ===============Gathering information=========================
echo ===============Drive information==============>>%log%
echo.
echo.>>%log%
set /p driveletter=Drive letter to assign to:
echo Drive Letter................................ %driveletter%>>%log%
echo.>>%log%
echo.
set /p IP=Ip address device:
echo IP address of remote device................. %IP%>>%log%
echo.>>%log%
echo.
set /p path=Path on remote machine:
echo Path on remote machine set to............... %path% >>%log%
echo.>>%log%
echo.
set /p Username=Username (will be viewable in the log file and batch file):
echo Username.................................... %username%>>%log%
echo.
echo.>>%log%
set /p pass=Password (will be viewable in the batch file):
echo Password.................................... **********>>%log%
echo. >>%log%
echo ============================================================>>%log%
echo.>>%
rem ====================================================================

rem ====Writing batch file to startup folder as Map %driveletter%.bat===







rem ========windows 7==================

:win7
set startup="C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\Map %driveletter%.bat"
pause
echo @echo off >>%startup%
echo title Mapping network drive %driveletter%:>>%startup%
echo color 0A>>%startup%
echo cls>>%startup%
echo echo.>>%startup%
echo echo Wating for connection to drive location.........>>%startup%
echo goto loop>>%startup%
echo.>>%startup%
echo :loop>>%startup%
echo.>>%startup%
echo echo Pinging %ip%.........>>%startup%
echo cd c:\windows\system32\>>%startup%

这就是我遇到问题的地方。此命令不会输出到其创建的批处理文件,而是关闭此批处理文件

echo ping %ip% -n 1 | find "TTL">>%startup%




echo if not errorlevel 1 goto up>>%startup%
echo if errorlevel 1 goto loop>>%startup%
echo.>>%startup%
echo.>>%startup%
echo :up>>%startup%
echo cls>>%startup%
echo echo.>>%startup%
echo echo Device is up>>%startup%
echo echo.>>%startup%
echo echo Mapping drive......>>%startup%
echo c:\windows\system32\net use %driveletter%: \\%ip%\%path% %pass% /USER:%username% >>%startup%
echo if not errorlevel 1 goto up>>%startup%
echo if errorlevel 1 goto end>>%startup%
echo echo.>>%startup%
echo :end>>%startup%
echo echo Drive was mapped>>%startup%
echo echo.>>%startup%
rem ==========REMOVE THE FOLOWING LINE TO HAVE THE BATCH FILE GO AWAY WHEN COMPLETE======
echo pause>>%startup%


start notepad %log%

pause

1 个答案:

答案 0 :(得分:1)

@echo off
echo @echo off>t.bat
echo echo test complete>>t.bat
echo quit>>t.bat

这基本上是一个批处理代码,它在当前工作目录中创建另一个批处理文件t.bat。您可以使用cd命令将输出文件(创建的批处理文件)定向到启动文件夹。 >>运算符将文本回显到输出文件的下一行。