# batch file to read filename and assign different variable to be called upon



## sadguy123 (Jun 9, 2010)

Hi, any advice for the following problem? Thanks for any help possible.

I wish to have a batch file to read the filename from folder A and later display a menu from these filename. 

For example folder A has 5 file(1.zip, 2.zip, 3.txt, 4.exe, 5.zip). It will read all the .ZIP file and assign variable so that I can call upon these variable for use later.

The final output on screen should be something like these and when I enter 1, I can call the filename 1.zip. Likewise when I enter 2, I can call the filename 2.zip.

1. 1.zip
2. 2.zip
3. 5.zip

Please enter an input:


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

This will do it:

```
@Echo Off
SetLocal EnableDelayedExpansion
Set _Source=C:\Temp Dir
Set _Count=0
For /F "Tokens=* Delims=" %%I In ('Dir /A-D /B "%_Source%\*.zip" 2^>Nul') Do (Set /A _Count+=1
Set _F!_Count!=%%I)
If %_Count%==0 Echo.No .zip files found&Goto :EOF
For /L %%I In (1,1,%_Count%) Do Echo.%%I. !_F%%I!
:_Ask
Echo.
Echo.Press Enter or 0 to exit.
Set /P _Choice=Please enter an input:
If "%_Choice%"=="" Goto :EOF
If %_Choice%==0 Goto :EOF
If %_Choice% GTR %_Count% Echo.Please enter a number between 1 and %_Count%&Goto :_Ask
Set _FileName=!_F%_Choice%!
Echo.Your file name is %_FileName%
```


----------



## sadguy123 (Jun 9, 2010)

Thanks TheOutCaste. Your batch works flawlessly. Thank you so much. Do you happen to know diskpart scripting as well? Having some trouble with that too.


----------



## Keebellah (Mar 27, 2008)

Excuse my butting in but you could make the batch file more flexible by allowing user input


```
@Echo Off
SetLocal EnableDelayedExpansion
If '%1%'=='' goto S1
Set _Source=%1%
goto continue
:S1
Set _Source=%CD%
:continue
Set _Count=0
```
I namend file TSG.bat

If you Type TSG c:\Temp it will serach in the C:\Temp for the zip files

If you type TSG without parameters it will look in the current directory whihc is read from %CD%

You could even give the file extension as a parameter but then I would reverse the order, making the extension mandatiry and the path as optional


```
@Echo Off
SetLocal EnableDelayedExpansion
if '%1%'=='' goto syntax

If '%2%'=='' goto S1
Set _Source=%2%
goto continue
:S1
Set _Source=%CD%
:continue
Set _Count=0
For /F "Tokens=* Delims=" %%I In ('Dir /A-D /B "%_Source%\*."%1% 2^>Nul') Do (Set /A _Count+=1
Set _F!_Count!=%%I)
If %_Count%==0 Echo.No .zip files found&Goto :EOF
For /L %%I In (1,1,%_Count%) Do Echo.%%I. !_F%%I!
:_Ask
Echo.
Echo.Press Enter or 0 to exit.
Set /P _Choice=Please enter an input:
If "%_Choice%"=="" Goto :EOF
If %_Choice%==0 Goto :EOF
If %_Choice% GTR %_Count% Echo.Please enter a number between 1 and %_Count%&Goto :_Ask
Set _FileName=!_F%_Choice%!
Echo.Your file name is %_FileName%
goto end
:syntax
Echo Please enter a file extension 
Echo  e.g.: TSG zip  (to check for zip files)
Pause
goto end
:end
```


----------



## TheOutcaste (Aug 8, 2007)

Some good ideas *Keebellah*, always welcome.

Couple of things:
Batch parameters don't need the trailing *%*, so you can just use *%1* instead of *%1%*.
Either form works due to the way *%* symbols are handled in a batch file, but makes it easier to see that you are referring to a parameter rather than a variable
You can actually use 0-9 as variables, but you can't access them from within a batch file, only in a Command Prompt.
Try this example, name it test.cmd:

```
@Echo Off
Set 1=This is One
Echo. Passed parameter is %1
Echo. With extra percent it's still %1%
```
Open a Command Prompt and run the file:
*test hello*
then type *Echo %1%* at the prompt; that's the only way to access the value assigned to the variable named *1*. It can't be accessed while the file is running, but can be set.

If the file name/path contains spaces, it has to be quoted. Since you won't know if the parameter will be quoted or not, best to use the tilde modifier to remove the quotes if present. Then you can put quotes around the variable and don't have to worry about ending up with doubled quotes:
*Set _Source=%~1*
or
*Set _Source=%~2*

The extension should be inside the quotes in the For loop, and the tilde modifier used as well, just in case they used an extension with a space in it, like *.zip old* instead of *.zipold*:
*'Dir /A-D /B "%_Source%\*.%~1" 2^>Nul'*

.


----------



## Keebellah (Mar 27, 2008)

Thanks for the info.
I didn't know that about the % at the end not being necessary. 
Like I always say, 'Never too old to learn'.

Thanks,


----------

