Computer Hope

Microsoft => Microsoft DOS => Topic started by: mat123 on March 10, 2010, 05:34:53 PM

Title: what is wrong with this
Post by: mat123 on March 10, 2010, 05:34:53 PM
what is wrong with this code

@echo  off
setlocal enabledelayedexpansion
for /f  "delims=" %%a  %%b %%c in (test.txt) do (
set newf=%%a
set newf2=%%b
set newf3=%%c
md "!newf!
cd "!newf!
echo "!newf3!" > "!newf2!.txt"
cd..
)

input

test1 test tset
test2 teste etset
test3 tester retset

output nothing

desired output
3 folders called test 1-3
in each folder file called test.txt teste.txt tester.txt
in file tset.txt etset retset
Title: Re: what is wrong with this
Post by: Helpmeh on March 10, 2010, 05:40:11 PM
A couple issues:
1. If delims is set to "" (nul) then there will be no other tokens.
2. Only the FIRST token name is said in the command.

This would be your code:

@echo off
setlocal enabledelayedexpansion
for /f "delims= " %%a in (test.txt) do (
md %%a
cd %%a
echo %%c > %%b.txt
cd..
)
Title: Re: what is wrong with this
Post by: mat123 on March 10, 2010, 05:45:02 PM
worked to a extent
file name is %b.txt
data is %c
Title: Re: what is wrong with this
Post by: Helpmeh on March 10, 2010, 05:49:07 PM
It is.

%? on the command prompt is %%? in a batch file. (? being a single-letter wildcard)
Title: Re: what is wrong with this
Post by: mat123 on March 10, 2010, 05:50:32 PM
no the name of the file created was %b etc.
Title: Re: what is wrong with this
Post by: Helpmeh on March 10, 2010, 05:54:10 PM
Oh, what a silly mistake. Here is the proper code.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-3 delims= " %%a in (test.txt) do (
md %%a
cd %%a
echo %%c > %%b.txt
cd..
)
Title: Re: what is wrong with this
Post by: mat123 on March 10, 2010, 05:56:10 PM
thank you
Title: Re: what is wrong with this
Post by: Helpmeh on March 10, 2010, 06:01:45 PM
Any time!