file name to variable

June 10, 2010 at 09:13:59
Specs: Windows Vista
Hi Folks,

I have just started using batch files for the first time today! I want to set the 32 different variables v1 to v32 to the file names of 32 different files that have the extention *.001 , *.002 upto *.032 etc. The variables should be the file name without the extension. The files are in the directory c:\batch\ Any ideas on how to achieve this?

Many thanks Tim.

See More: file name to variable

Report •

June 10, 2010 at 09:47:46
@echo off & setlocal EnableDelayedExpansion
cd /D C:\batch
for /L %%i in (1 1 32) do (
  set ext=00%%i
  set ext=!ext:~-3!
  for %%j in (*.!ext!) do set v%%i=%%~nj
:: End_Of_Batch

Report •

June 10, 2010 at 09:57:00
obviously you need a for-loop to increment 1-32:
for /L %%a in (1 1 32) do (
and you need a way to separate the filename from its extension.
also, a way to "pad" the numbers generated with zeros to get the correct format for the extension. (1 to 001)
You'll probably need to use enabledelayedexpansion
(see setlocal /?)
Since you're trying to learn, rather than write a script, I'll just point you to these things and recommend consulting:
for /? (esp: the 3rd "page" for extracting filename-only)
set /?
if /?
setlocal /?

padding can be done like this:
set var=00%var%
set var=%var:~-3%

Report •

June 10, 2010 at 10:11:15

thanks for the accurate explanation pf the code.

Enrico Fermi, the father of nuclear engineering, was a wonder scientist but a worst teacher unable to write a high school physics book. His wife did the job using his notes.

Report •

Related Solutions

June 11, 2010 at 05:56:01
Thank you for this brilliant solution!

I am trying to use the batch file to upload data to a database on a website. I want the output of the batch file to be:


The variable from the batch file is %s1%, the problem is with the & symbol. Is there any way of outputting this command line. I have tried putting the & in double quotes but the quote are entred in the url. I have tried using the command but can't get this to create the right output.

Any ideas folks?

Report •

June 11, 2010 at 05:59:00
End of the url should be this, since it got cut off in the reply above...


Report •

June 11, 2010 at 12:58:11
I don't understand how your command line is related to the original request since from 32 variables now just one survives.

Anyway to output the & symbol prefix it with the caret ^, i.e. ^&.

When you post a request better you post *exactly* what you want to achieve and that is higly suggested for newbie.

Report •

June 14, 2010 at 01:15:34
Thanks Ivo,

This now works perfectly. I did not include all the variables to keep the length of the URL down.

Cheers, Tim

Report •

Ask Question