JMA1GDO Posted May 12, 2011 Posted May 12, 2011 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.JMA1DGOEcho offCLScd \.cd projectsSET MyDirectoryList=%CD%dir %1 /b>MyDirectoryListSet /p TOOLOUTPUT= < MyDirectoryListdel MyDirectoryListECHO %TOOLOUTPUT%
jaclaz Posted May 12, 2011 Posted May 12, 2011 How MANY results do you expect?I mean like:a handfulless than tenbetween ten and 99infiniteIn the meantime a couple notes.What is the %CD%?Since you are looking ONLY for directories, you should use the /AD switch:DIR /b /ADand maybe also the /s one, as this way you will get a "full path" as reply You cannot "feed a list" to SET /P! You don't need to create a temporary file, this should work:@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONCLScd \.cd projectsSET My_path=%~dp0SET My_search="*%**":loopSET /A My_counter=0SET MY_DIR "%My_search%" /AD /b >nul 2>&1IF %ERRORLEVEL%==1 ECHO NOT FOUND&GOTO :EOFFOR /F %%A in ('DIR "%My_search%" /AD /b' ) DO (SET /A My_counter+=1ECHO !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 :loopECHO 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
JMA1GDO Posted May 12, 2011 Author Posted May 12, 2011 Thanks a lot!!!!I expect to get between 1-9 directories, no more.thanks again.JMA1GDO
JMA1GDO Posted May 12, 2011 Author Posted May 12, 2011 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
allen2 Posted May 12, 2011 Posted May 12, 2011 (edited) With delims, it now should do what you want.@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONCLScd \.cd projectsSET My_path=%~dp0SET My_search="*%**":loopSET /A My_counter=0SET MY_DIR "%My_search%" /AD /b >nul 2>&1IF %ERRORLEVEL%==1 ECHO NOT FOUND&GOTO :EOFFOR /F "delims=;" %%A in ('DIR "%My_search%" /AD /b' ) DO (SET /A My_counter+=1ECHO !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 :loopECHO You chose directory !My_dir_%My_choice%!SET MY_ Edited May 12, 2011 by allen2
jaclaz Posted May 12, 2011 Posted May 12, 2011 With delims, it now should do what you want.Wouldn't "tokens=* delims=" or "delims=" be better?I mean, we don't know if any dir has ";" in it's name....http://www.robvanderwoude.com/ntfortokens.phpjaclaz
JMA1GDO Posted May 12, 2011 Author Posted May 12, 2011 It is working great!!!! thank you so much!!JMA1GDO
jaclaz Posted May 12, 2011 Posted May 12, 2011 It is working great!!!! thank you so much!!JMA1GDOYou 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
JMA1GDO Posted May 12, 2011 Author Posted May 12, 2011 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
allen2 Posted May 12, 2011 Posted May 12, 2011 ";" 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.
jaclaz Posted May 13, 2011 Posted May 13, 2011 ";" 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. 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-usthough the ";" is NOT in the list of "allowed".jaclaz
allen2 Posted May 13, 2011 Posted May 13, 2011 True, i was wrong. I never saw one folder containing it until now. So "delims=" would be better as you corrected.
jaclaz Posted May 13, 2011 Posted May 13, 2011 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 :jaclaz
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now