Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a list in a txt like this

AAA 1234561
BBB12 66698732
CCCCC 5878471
DD131 262554158

and and I have a Directory like the below

DirectoryA/AAA
DirectoryA/BBB12
DirectoryA/CCCCC
DirectoryA/DD131

I want to create a txt file with a give name lets call it id.txt which contains the respective code for its folder

So the DirectoryA/AAA/id.txt contains 1234561
DirectoryA/BBB12/id.txt contains 66698732 and so on. 

I tried to extract the line with /p but it copies the whole line not just the ID.

share|improve this question

1 Answer 1

up vote 3 down vote accepted
for /f "tokens=1-2" %%A in (x.txt) do echo %%B > DirectoryA\%%A\id.txt

Discussion:

You can get a lot of useful information by typing FOR /? into a Command Prompt.  In particular,

FOR /F  ["options"]  %variable IN  (file-set)  DOcommand[command-parameters]

reads the file or files specified between the parentheses (the file-set) and parses each line out into tokens.  tokens=1-2 is the options string to say that you want the first and second words on each line.  %%A specifies that %%A is the variable that the first word will be read into; implicitly/automatically the second word goes into %%B.  Then the echo command gets executed with %%A and %%B set to the two words from the file.

Note: If you were typing this command directly into the Command Prompt, you would use %A and %B, but you have to use double percent signs when you do the same thing in a script (batch file).

share|improve this answer
    
Its working great can you please explain to me the tokens=1-2 and the whole thinking behind this command? thanks in advance –  Sonamor Aug 16 '13 at 23:57
    
I hope I could rep you up Scott, thanks for the exlanation –  Sonamor Aug 17 '13 at 0:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.