Jump to content

Recommended Posts

Posted

Hello,

I need to write a batch file which will create a few directories inside a given address directory. The user will input the address directory using a wild character to find all directories which match the serach criteria, for example: 115-1* then all directories which match should show up. Then, the user needs to pick which one is the correct address, go to that directory and inside of it create a set of standard directories.

I have never written anything in batch so after some figuring out I wrote the following code which actually lists the directories but I don't know how to store the results of the DIR %1 /b into variables (like DIR 1, DIR 2, DIR3... etc...)

I know I am on the wrong track.... so if somebody can give me a hand I would appreciated it.

Thanks.

JMA1DGO

Echo off

CLS

cd \.

cd projects

SET MyDirectoryList=%CD%

dir %1 /b>MyDirectoryList

Set /p TOOLOUTPUT= < MyDirectoryList

del MyDirectoryList

ECHO %TOOLOUTPUT%


Posted

How MANY results do you expect?

I mean like:

  • a handful
  • less than ten
  • between ten and 99
  • infinite

In the meantime a couple notes.

What is the %CD%?

Since you are looking ONLY for directories, you should use the /AD switch:

DIR /b /AD

and maybe also the /s one, as this way you will get a "full path" as reply :unsure:

You cannot "feed a list" to SET /P! :w00t:

You don't need to create a temporary file, this should work:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CLS
cd \.
cd projects
SET My_path=%~dp0
SET My_search="*%**"

:loop
SET /A My_counter=0
SET MY_
DIR "%My_search%" /AD /b >nul 2>&1
IF %ERRORLEVEL%==1 ECHO NOT FOUND&GOTO :EOF
FOR /F %%A in ('DIR "%My_search%" /AD /b' ) DO (
SET /A My_counter+=1
ECHO !My_counter! - %%A&SET My_dir_!My_counter!=%%A
)
SET /p My_choice=Please input number of directory:

IF NOT %My_choice% LEQ %My_counter% ECHO Wrong, last dirnum is %My_counter%&PAUSE&GOTO :loop

ECHO You chose directory !My_dir_%My_choice%!
SET MY_

Mind you only a rough sketch, some more serious error control is needed if it is to go into the hands of "final users".

The ERRORLEVEL check can be avoided (and thus the double DIR command) if you check the output of the DIR command when NO item is found, in your language.

jaclaz

Posted

Just one more question....

The directories have numbers and letters such as: 115-14 BEACH CHANNEL DRIVE.

The code returns only the numbers..... can it me modified to return the complete directory name?

Thanks so much!!!

JMA1DGO

Posted (edited)

With delims, it now should do what you want.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CLS
cd \.
cd projects
SET My_path=%~dp0
SET My_search="*%**"

:loop
SET /A My_counter=0
SET MY_
DIR "%My_search%" /AD /b >nul 2>&1
IF %ERRORLEVEL%==1 ECHO NOT FOUND&GOTO :EOF
FOR /F "delims=;" %%A in ('DIR "%My_search%" /AD /b' ) DO (
SET /A My_counter+=1
ECHO !My_counter! - %%A&SET My_dir_!My_counter!=%%A
)
SET /p My_choice=Please input number of directory:

IF NOT %My_choice% LEQ %My_counter% ECHO Wrong, last dirnum is %My_counter%&PAUSE&GOTO :loop

ECHO You chose directory !My_dir_%My_choice%!
SET MY_

Edited by allen2
Posted

It is working great!!!! thank you so much!!

JMA1GDO

You are welcome :), but as said, stil a lot of work (error trapping) is needed.

What if the user just hits [ENTER]?

What if he enters "a", "b" (or "micky mouse" or "goofy" ;)) ?

You do understand that the batch changes current directory to <driveroot>\projects\ and "never goes back", right?

jaclaz

Posted

Yes, I understand.

I am now working on how to change to the directory selected when the batch file is finished.

After I get this done I will work on error trapping.

Thanks again.

JMA1GDO

Posted

";" isn't allowed in paths so there won't be any problem with this but your options should also work. Everyone has its own way to do things.

Posted

";" isn't allowed in paths so there won't be any problem with this but your options should also work. Everyone has its own way to do things.

That's strange. :unsure:

On my XP machine I can have directories containing ";" in the name, "forbidden" characters are:

\ / : * ? " < > |

And this is confirmed here:

http://support.microsoft.com/kb/177506/en-us

though the ";" is NOT in the list of "allowed".

jaclaz

Posted

True, i was wrong. I never saw one folder containing it until now. So "delims=" would be better as you corrected.

Posted

True, i was wrong. I never saw one folder containing it until now. So "delims=" would be better as you corrected.

Sure :), most people have not much fantasy when naming folders.... ;)

OT, and JFYI, a few people actually find FUN using malformed names :angel :

jaclaz

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...