Link to home
Start Free TrialLog in
Avatar of cavalierlan
cavalierlan

asked on

Batch File If Statement in For Loop

My goal is to do a dir of a folder to create a txt list of the files. I then want to add text to the end of the file name based on its file extension.  I want to do this with a batch file and I think i need to have an if statement in my for loop

Here's where I have gone so far.
.............................................
dir c:\folder1>text.txt

setlocal
set msifile=--is a msi file
set exefile=--is a exe file
for /f "delims=" %%a in (text.txt) do (
      if %%a=*.exe echo %%a%exefile% else echo %%a%msifile
)
Avatar of Bill Prew
Bill Prew

Try this:

dir c:\folder1>text.txt

setlocal
set msifile=--is a msi file
set exefile=--is a exe file
for /f "delims=" %%a in (text.txt) do (
      if %%~xa=.exe echo %%a%exefile% else echo %%a%msifile
)
Avatar of cavalierlan

ASKER

Thanks for the reply,
my for loop looks like below but it doesn't run.  the cmd window flashes and goes away before it gets to the pause.  I tried it echoing to screen and to output.txt, i get nothing.

is the if %%~xa statement correct?



for /f "delims=" %%a in (text.txt) do (
      if %%~xa=.exe echo %%a%exetext%>>output.txt else echo %%a%exetext%>>output.txt
)
Pause
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect! Works great