I want to create a For loop to look if a program is on a flash drive, and copy a text file if it is. Since the drive letter varies on every PC, I want it to look at every drive letter.
if exist "D:\Test.exe" (
copy "%FileName%.txt" "D:\" >nul
)
if exist "E:\Test.exe" (
copy "%FileName%.txt" "E:\" >nul
)
if exist "F:\Test.exe" (
copy "%FileName%.txt" "F:\" >nul
)
if exist "G:\Test.exe" (
copy "%FileName%.txt" "G:\" >nul
)
Rem ...Continues possibly until drive Z or once it finds the file
Is there a way to create a For loop to increment the drive letter so I don't have to make an If statement each time?
CodePudding user response:
Here is a community wiki with all methods combined from the comments, per user.
@echo off
FOR %%G IN (A B C D etc..Z) DO IF EXIST "%%G:\test.exe" copy
then, a very intuitive method using the exit codes 65
to 90
in ascii format, by Aacini
.
@echo off
setlocal enabledelayedexpansion
for /L %%i in (65,1,90) do cmd /C exit %%i & if exist "!=ExitCodeAscii!:\test.exe" copy...
Then my
suggestion of using wmic
to find all the actual drive letters available on the device:
@echo off
for /f "tokens=2*delims==" %%i in ('wmic logicaldisk get caption /value') do for /f "delims=" %%d in ("%%i") do if exist "%%d\test.exe" copy ...