|
Answer» I need help to create a set of subdirectories two levels down in a root directory. I have to use a source file to read from but do not know how to get the next line of the source file.
The root directory has the following structure: e:\26334\7327\ e:\26335\7317\ e:\26910\7690\ e:\26912\7100\ e:\26916\1414\ e:\27127\7702\ e:\28145\4726\ e:\28177\4122\ e:\28189\7125\ e:\28211\3527\ e:\28528\8236\ e:\28535\8155\ e:\28746\6291\ e:\29036\3448\ e:\29222\8231\ and so on...
I am using a source file to read from that looks like this: 23742\3524 26035\8919 26316\7814 26334\1223 26334\7327 26910\7690 27127\7702 28145\4726 28211\3527 28528\8236 28535\8155 28746\6291 29036\3448 29222\8231 29353\1669 30529\4689 30843\7460 31415\8053 and so on...
The source file is called dir-list.txt. The batch file I'm trying to create will be run from the root e:\
The following is the batch file so far:
::--- FOR /F "tokens=1 eol=^" %%G IN (dir-list.txt) DO (
::combine current directory with path returned in variable %%G set "D1=%cd%" set "D2=%%G" set "DD=%D1%\%D2%"
::find destination directory IF EXIST %DD% GOTO cd
::change directory to destination directory :cd PUSHD %DD% GOTO md
::create folders in destination directory :md MD TEST1, TEST2, TEST3
::return to ORIGINAL directory POPD ) ::---
This works by creating the folders TEST1, TEST2 and TEST3 under the second level down but then stops after one iteration. I need it to go through the dir-list.txt until the end. Also, I don't have any error routine for the folders that are not in the dir-list.txt or the entries in the dir-list.txt that are not found in the root directory. The root directory is on a Windows 2008 r2 machine. Thank you for any suggestions.1. You need to use delayed expansion to set and use variables in a FOR loop. Use Google to find out about this. 2. Do not start comment lines with unofficial, unsupported, deplored, BAD double colons. ( This breaks FOR loops. Use the proper REM instead.
Then see if your code works.
Thanks for the reply. I tried it with the setlocal enabledelayedexpansion. the following is the output of the batch file. I removed the comments. E:\>test.bat
E:\>SETLOCAL enabledelayedexpansion
E:\>FOR /F "tokens=1 eol=^" %G IN (dir-list.txt) DO ( set "D1=E:\" set "D2=%G" set "DD=E:\01333\8915" IF EXIST E:\01333\8915 GOTO cd PUSHD E:\01333\8915 GOTO md MD TEST1, TEST2, TEST3 POPD )
E:\>( set "D1=E:\" set "D2=01333\8915" set "DD=E:\01333\8915" IF EXIST E:\01333\8915 GOTO cd PUSHD E:\01333\8915 GOTO md MD TEST1, TEST2, TEST3 POPD )
E:\>PUSHD E:\TEMP\01333\8915
E:\01333\8915>GOTO md
E:\01333\8915>MD TEST1, TEST2, TEST3
E:\01333\8915>POPD E:\> --------------------------------------------
It still stops after one iteration. You showed the output but not the new code. Why? Also, why do you have gotos and labels inside a loop? (That's why you can't have double colon "comments" inside a loop, because such a comment is really a broken label, and labels break loops.) Did you actually "write" this code yourself, or did you just copy and paste bits of other batch scripts you found on the web? It would be better if you went back to the beginning and started again, studying batch script syntax carefully. As I indicated in my post, I removed the comments and added the SETLOCAL enabledelayedexpansion, so didn't think the code was needed because beyond that, the code was the same. And yes, I wrote it all by my lonesome. I subsequently MOVED the goto out of the loop and it continues through the entire source file. But then I had trouble reinitializing the %DD% variable. It kept the same value through all iterations. So, to keep from further disrupting this great forum, I accomplished it in about 1/2 an hour in C#. SORRY, I create batch files all the time for simpler stuff and thought it would have been easy. Swim on Mister Trout! Thanks for the help.This code needs work but should help get you started. I will write more latter. Good luck
@echo off setlocal enabledelayedexpansion FOR /F %%i in (dir-list.txt) do (
rem combine current directory with path returned in variable %%i set D1=%%i echo D1=!D1! set D2=%%i echo D2=!D2! set DD=!D1!/!D2! echo DD=!DD! echo. ) pause rem find destination directory IF EXIST !DD! goto chgdir
rem change directory to destination directory :chgdir echo DD=!DD! echo pushd popd PUSHD
echo DD=!DD! pause popd dir pause
rem create folders in destination directory
rd xTEST1, xTEST2, xTEST3
md xTEST1, xTEST2, xTEST3
rem return to original directory POPD echo DD=!DD! )
C:\test>
C:\test> lexus.bat D1=23742\3524 D2=23742\3524 DD=23742\3524/23742\3524
D1=26035\8919 D2=26035\8919 DD=26035\8919/26035\8919
D1=26316\7814 D2=26316\7814 DD=26316\7814/26316\7814
D1=26334\1223 D2=26334\1223 DD=26334\1223/26334\1223
D1=26334\7327 D2=26334\7327 DD=26334\7327/26334\7327
D1=26910\7690 D2=26910\7690 DD=26910\7690/26910\7690
D1=27127\7702 D2=27127\7702 DD=27127\7702/27127\7702
D1=28145\4726 D2=28145\4726 DD=28145\4726/28145\4726
D1=28211\3527 D2=28211\3527 DD=28211\3527/28211\3527
D1=28528\8236 D2=28528\8236 DD=28528\8236/28528\8236
D1=28535\8155 D2=28535\8155 DD=28535\8155/28535\8155
D1=28746\6291 D2=28746\6291 DD=28746\6291/28746\6291
D1=29036\3448 D2=29036\3448 DD=29036\3448/29036\3448
D1=29222\8231 D2=29222\8231 DD=29222\8231/29222\8231
D1=29353\1669 D2=29353\1669 DD=29353\1669/29353\1669
D1=30529\4689 D2=30529\4689 DD=30529\4689/30529\4689
D1=30843\7460 D2=30843\7460 DD=30843\7460/30843\7460
D1=31415\8053 D2=31415\8053 DD=31415\8053/31415\8053
Press any key to continue . . .
|