1

I have the following code in my batch, which I want to read the lines in test.txt, create a folder with the first 2 Characters and make config ini files for the respective items in their intented folder.

Here is the structure of my code:

@echo off
for /f "tokens=*" %%i in (test.txt) do (
set first2=%i:~0,2%
md "C:\Documents and Settings\someuser\Desktop\template\config\%first2%" 2>nul
type part1.txt>"C:\Documents and Settings\someuser\Desktop\template\config\%first2%\%%i"
echo.>>"C:\Documents and Settings\someuser\Desktop\template\config\%first2%\%%i"
echo Hostname=%%i.somedns.net>>"C:\Documents and Settings\someuser\Desktop\template\config\%first2%\%%i"
type part2.txt>>"C:\Documents and Settings\someuser\Desktop\template\config\%first2%\%%i"
)
Pause

Example of input from test.txt:

OR0001R1
OR0001R2
OR0008R1
OR0009R1
OR0009R2
OR0009R3
OG0001R1
OG0001R2
OG0002R1
OG0003R1

My problem is the following, instead of creating folder OR in which it then creates OR0001R1.ini and so on and afterwards create folder OG for the OG inis, it only creates folder OR and all the config inis in it. Can someone help me with this ?

4
  • Please consider accepting the answer posted by ticking the check mark beside it, as it solved your problem. Mar 29, 2016 at 10:03
  • I notice you "deleted" your "thank you" answer. The correct way to express thanks is to upvote an answer (when you have enough reputation) and to accept that answer if it solved your problem.
    – DavidPostill
    Mar 29, 2016 at 10:04
  • Also you appear to have created 2 accounts. See How can one link/merge/combine/associate two accounts/users? and/or I accidentally created two accounts; how do I merge them? for guidance on how to merge your accounts. If you don't merge them you won't be able to accept answers or comment using the newer account.
    – DavidPostill
    Mar 29, 2016 at 10:06
  • Hi, it was all due to an issue when creating the account, that's what caused the comment as an answer issue, because I mostly couldn't do anything. I am new to stackexchange so I didn't know exactly what was what and what did what. Now that it was all worked out, I managed to mark the answer and post comments. Thank you and have a nice day!
    – GreyS
    Mar 30, 2016 at 12:29

1 Answer 1

0

It only creates folder OR

You need to use enabledelayedexpansion.

The problem with your code is that within a code block (parenthesised series of statements) any %var% will be replaced by the actual value of the variable at parse time not at execution time.

If you want to use the value of an environment variable that is changed within a loop, then you need to use setlocal enabledelayedexpansion and !var! instead of %var%.

Here is a corrected version of the batch file:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (test.txt) do (
  set first2=%%i
  set first2=!first2:~0,2!
  echo md "C:\Documents and Settings\someuser\Desktop\template\config\!first2!" 2>nul
  type part1.txt>"C:\Documents and Settings\someuser\Desktop\template\config\!first2!\%%i"
  echo.>>"C:\Documents and Settings\someuser\Desktop\template\config\!first2!\%%i"
  echo Hostname=%%i.somedns.net>>"C:\Documents and Settings\someuser\Desktop\template\config\!first2!\%%i"
  type part2.txt>>"C:\Documents and Settings\someuser\Desktop\template\config\!first2!\%%i"
  )
Pause
endlocal

Further Reading

1
  • Hi, I finally got the option to mark your answer as the correct one, thank you for the help
    – GreyS
    Mar 30, 2016 at 12:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .