1.

Solve : Help with Variables and DIR?

Answer»

Currently I am PLAYING around with some bat files, but when I wanted to create one that starts a program from anywhere on the COMPUTER, I had some difficulty. It was supposed to take user input for a filename and extension, put it in %input%, use dir /s /a /b %input% and the directory found should have gone in a variable CALLED %location%, which the bat file would then switch to, and opening the file.

It doesn’t work as I have no idea on how to get the output (searched results of %input%) into %location%. COULD you please help me fix this issue, or is there a easier method. (This bat file is for Microsoft XP).

Ps. Is there also a way to deal with multiple results?

Code of Scan.bat:
cd ..
cd ..
cd ..
rem Just to be sure...
cd /d C:
set /p input=Filename:
rem Not sure if this is the right method
dir /s /a /b %input%>scan.txt
set location cd %location%
rem Changes to directory
%input%
rem Opens file
echo Finished!
pausefor %%a in (%input%) do set location=%%a

sets %input% in the var location.

hope it helps
uliWhen developing batch code, it is always helpful to try the commands at a command prompt so you can see the results that are produced:

Code: [Select]:: guarantee the root directory
cd /d C:\
set /p input=Filename:
rem Not sure if this is the right method
dir /s /a /b %input%>scan.txt
[highlight]set /p location=<scan.txt[/highlight]
%location%
rem Opens file
echo Finished!
pause

Your dir command will produce the full path and file name, not just a directory name.

Note: This method may be problematical should the dir command produce multiple results.

Hope this helps. 8-)Thanks for the feedback, much appreciated. One more question though. Is it POSSIBLE to count the number of words/symbols in the %input%? Because then you could have cd %location:~0,-n%. I'm not even sure if this can be done. Is it possible? Thanks anyway.



Discussion

No Comment Found