nasoy
nasoy

Reputation: 11

how to have a multiple command in batch file

I'm a newbie and trying to make a simple progman that start cm.exe and then change its drive letter and change the attribute of all the files in that particular drive and delete all the .link files or the shortcut folders.

In cmd, the input is like this:

(Drive letter):
Attrib -r -s -a -h *.*
Del auto run.inf
Del *.lnk

How write this command in batch file. Thanks a lot (sorry for my English. Lol :3)

Upvotes: 0

Views: 189

Answers (1)

Magoo
Magoo

Reputation: 79982

Use an editor line Editplus or notepad++ to enter precisely those lines in a file named whateveryoulike.BAT. It's the .BAT part that does the trick. (You could also use NOTEPAD but be careful - you need to save as ANSI and NOTEPAD has a habit of attempting to format the text)

From the CMD prompt, type PATH. This will show a ;-separated list of directories which is searched to try to find the command whateveryoulike. Save the whateveryoulike.BAT in any directory in that list.

Serious batchers will add a directory to contain batches and add that directory to the path.

Then all you need to do is issue the command whateveryoulike at the prompt, and the batch file will run - just as though you had manually entered the lines it contains.

It's traditional to add a line

@echo off

at the start of the file, which suppresses ECHOing of the commands as they are executed.

It's also a good idea to add a

SETLOCAL

command after that, to automatically dispose of changes the batch may have made to the environment when the batch finishes. This is a matter of judgement, but problems can be caused by batches unexpectedly altering variables.

Finally, a batch can access parameters by using %n where n=1..9 so if you were to compose

@echo off
setlocal
set drive_letter=%1
%Drive_letter%:
Attrib -r -s -a -h *.*
Del auto run.inf
Del *.lnk

as whateveryoulike.BAT and save it in a directory on your PATH then, from the prompt

whateveryoulike e

would execute your series of commands and assign e as drive_letter. Note the method of accessing a variable - %variablename%

(but be careful!!!!!)

You would possibly prefer

@echo off
setlocal
set drive_letter=%1
if not defined drive_letter echo drive letter required&goto :eof
%Drive_letter%:
...

Which checks whether you actually specified a parameter. Note the use of & to separate commands on the same physical line and the label :eof (where the : is required) to mean go to the end of the physical batchfile

Finally - and be VERY, VERY careful with this - you can also use DEL /s to delete matching files in ALL SUBDIRECTORIES and attrib /s to change the attributes of matching files in ALL SUBDIRECTORIES (well, all subdirectories from the current, anyway...) - not for the faint-hearted!!

Upvotes: 1

Related Questions