Sie sind auf Seite 1von 5

Batch File Examples

Password Example
The following batch file can be used to establish a password for running a program. The batch file is
named START.BAT and calls the program named CALC.EXE.

@ECHO OFF
IF %1==ExeScript GOTO OK
ECHO WRONG PASSWORD
GOTO END
:OK
ECHO PASSWORD IS ACCEPTED...STARTING
CALC.EXE
:END

Below you'll see the response of the computer to various commands.

First the bad password. At the prompt type START 123.


C:/>
C:/>START 123
C:/>BAD PASSWORD

Now use the correct password. Type the correct command at the prompt.
C:/>
C:/>START ExeScript
C:/>PASSWORD IS ACCEPTED...STARTING

At this point the CALC.EXE program starts.

Backup your .doc files (Windows NT/2000/XP version)


usage: backbat backupdir
where: backupdir is the directory to copy your .doc files

@echo off
if not "%1"=="" goto argsok
echo usage: %0 backupdir
echo where: backupdir is the directory to copy your .doc files
goto end

:argsok
setlocal
set backupdir=%1
if not exist %backupdir% goto notfile
echo %backupdir% is a file
goto end
:notfile
rem If the directory does not exist, create it.
if exist %backupdir%\nul goto skipdir
md %backupdir%
if "%errorlevel%"=="0" goto skipdir
echo Error creating backup directory
goto end
:skipdir
rem Copy each .doc file one at a time.
rem Note: the for loop variable (%%b) must be contain only one letter.
for %%b in ( *.doc ) do copy %%b %backupdir% > nul
rem Use the for loop again to check if each file was copied (since it is
rem difficult to run multiple commands in a for loop).
for %%b in ( *.doc ) do if not exist %backupdir%\%%b echo %%b was not copied
:end
rem Clean up
endlocal

How to make a time log


In the following example, you can create a time log of when the batch file is loaded or, for example, in
the autoexec.bat file when someone logs into a computer.

ECHO. |TIME > TIME


COPY LOG +TIME

An alternate slightly more complicated method that, to our knowledge, cannot be used in Windows NT,
Windows 2000 or Windows ME would be the following:

echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat


%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> log

Another alternative is:

echo. |time |find "current" >> log

For the above batch file to work properly, you must create a file called log by typing "edit log" (without
the quotes) and then save and exit the file which will create the file at 0 bytes. If this file is not created
or not created properly, you will receive the error message, "Content of destination lost before copy".
Here are some Useful command line batch
files we have written to help us out.
The command prompt is alive and well in Citrix Winframe and Metaframe, it is often faster and
more efficient to do things from it than from winstation administration. Below are some great
helps to make it even more efficient for you.

===========================================================================

Killall.bat

The text between the double lines is what you type at the command prompt to create the files.
Here is the most useful one we have it is called Killall.bat all you need to do is type killall
followed by the workstation id numbers you want to reset ex: killall 21 22 23 24. This
works great if you need to log multiple users off to fix a problem.

===================

Copy con killall.bat


@echo off
cls
:killanother
if <%1>==<> goto alldead
reset winsta /v %1
shift
goto killanother
:alldead
================

press control-z to save the file

================

Disable.bat and Enable.bat

We created these two batch files to quickly disable and reenable logons. We created an Icon
from the batch files. and added it to our admin group. You can omit the Rem lines.

===================

Copy con disable.bat


Rem This command disables logons
change winsta /disable
====================

press ctrl-z to save the file


====================

Copy con enable.bat


Rem This command enables logons
change winsta /enable
================

press ctrl-z to save the file

==================

S.bat

S.bat was created to get a status of another server from any of our servers. Our servers are
named Citrix1 - Citrix5 You will need to substitute your server names for the word Citrix. You
would type s and then the server number ex. s 1. Hopefully your server names are sequentially
numbered or this wont work.

=========================

Copy con s.bat


@echo off
cls
quser /server:CITRIX%1
if CITRIX(%1).==CITRIX(). goto end
:end
===========================

press ctrl-z to save this file

=========================================================

Deltree.bat

Deltree.bat is a batch file I found on the web to solve a major problem with NT. We needed a
way to delete all files within a subdirectory without deleting the directory itself, and also avoid
having to answer y a million times. The deltree command is gone in NT so this is the way around
it. We use this to clear our Temp Directory as well as delete and recopy program subdirectorys
when files get corrupted. The command is such you would type deltree then the drive and
directory you want to delete for example deltree c:/temp
==================================================

Copy Con Deltree.bat


@echo off
pushd %1
del /q *.*
for /f "Tokens=*" %%i in ('dir /B') do rd /s /q /a "%%i"
popd
=================================================
press ctrl-z to save this file

Another simpler way to delete all files

Another simpler way to delete all files from a subdirectory but it takes up two lines in your batch
file is to use the below two lines:

echo y | del c:\temp\*.* /S


rd c:\temp\ /s /q
md c:\temp
Cacls C:\TEMP /E /P EveryOne:F

The first line clears out the files in the root and all subdirectories(/s)without prompting, the
second line deletes the subdirectory folders. Make sure you have the \ at the end of the directory
name on the second line or it will delete the directory also. Sometimes this command deletes the
temp directory. Thus the md statement to recreate it. It is ignored if the directory exists. The
cacls command gives everyone full permission to write to the temp directory in case someone
accidently took permission of it.

Pro.bat

Pro.bat was written to view the running processes on the system you are on. We have found that
a process stuck in the run state for a long period of time is usually a runaway and killing that
process will bring a server back to life.

============================

copy con Pro.bat


qprocess * |more
============================

press ctrl-z to save this file

P.bat

P.bat took pro.bat one step further it gave us the ability to look at processes on our other servers.
Just by typing in P and the number of the server. Ex. P 1. Note like s.bat that you have to
substitute the word Citrix with the name/number of your server and hopefully they are
sequentially numbered.

==============================

@echo off
cls
qprocess * /server:CITRIX%1
if CITRIX(%1).==CITRIX(). goto end
:end

Das könnte Ihnen auch gefallen