# Need help to simplify the dos batch file



## anillusions (Jun 22, 2012)

How do I simplify the following code using FOR command? 

Thank you.


set drive2=
if exist E:\*.gho set drive2=2:2
if exist F:\*.gho set drive2=2:3
if exist G:\*.gho set drive2=2:4
if exist H:\*.gho set drive2=2:5
if exist I:\*.gho set drive2=2:6
if exist J:\*.gho set drive2=2:7
if exist K:\*.gho set drive2=2:8
if exist L:\*.gho set drive2=2:9
if exist M:\*.gho set drive2=2:10
if exist N:\*.gho set drive2=2:11
if exist O:\*.gho set drive2=2:12
if exist P:\*.gho set drive2=2:13
if exist Q:\*.gho set drive2=2:14
if exist R:\*.gho set drive2=2:15
if exist S:\*.gho set drive2=2:16
if exist T:\*.gho set drive2=2:17
if exist U:\*.gho set drive2=2:18
if exist V:\*.gho set drive2=2:19
if exist W:\*.gho set drive2=2:20
if exist X:\*.gho set drive2=2:21
if exist Y:\*.gho set drive2=2:22
if exist Z:\*.gho set drive2=2:23


----------



## Squashman (Apr 4, 2003)

I wouldn't really call this simplifying it. I bet all your IF exists run faster than this.


```
@echo off

set drive2=

FOR %%G IN ("E=2:2" "F=2:3" "G=2:4" "H=2:5" "I=2:6" "J=2:7" "K=2:8" "L=2:9" "M=2:10" "N=2:11" "O=2:12" "P=2:13" "Q=2:14" "R=2:15" "S=2:16" "T=2:17" "U=2:18" "V=2:19" "W=2:20" "X=2:21" "Y=2:23" "Z=2:24") DO (
	FOR /F "TOKENS=1,2 DELIMS==" %%H IN ('echo %%~G') DO (
		if exist %%H:\*.gho set drive2=%%I
	)
)
```


----------



## anillusions (Jun 22, 2012)

Thank Squashman for the suggestion.

However I got the following dos error msg

Bad command & Filename on For %% G in ....

Syntax error on For / F "TOKEN=1,2 .... 

When I run it on windows 98 dos


----------



## Squashman (Apr 4, 2003)

I suppose it didn't occur to you that the operating system makes a difference. Would have helped to know you were running a 14 year old Operating system when you posted your question.

My script will only work on versions of Windows based on the NT Kernel. 

Don't know how you would do this with a batch file on older version of Windows.


----------

