Page 1 of 1
Annoying string wont go away
Posted: 03 Apr 2011 08:13
by flexbuffchest
Hey guys,
I am trying to reformat a current list of all the students where I work. The text file I have has 2-3 students per line and each are delimited by a space. I want to be able to create a list with each student on it's own line. Not every line has 3 students though, some only have 2.
What I find happening is that on the lines where there are only 2 students I am getting a "Echo is off." being redirected to the new list that I create. Obviously, I don't want this in there. Here is the code I have so far.
Code: Select all
@echo off
for /f "tokens=1-3 usebackq" %%a in ("C:\Users\Peter\Desktop\Removingusers\userstodelete.txt") do (
if "%%a"=="Echo is off." (%%a > nul) else (@echo %%a >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
if "%%b"=="Echo is off." (%%b > nul) else (@echo %%b >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
if "%%c"=="Echo is off." (%%c > nul) else (@echo %%c >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
)
Any help would be much appreciated
Re: Annoying string wont go away
Posted: 03 Apr 2011 08:34
by ghostmachine4
flexbuffchest wrote:Hey guys,
I am trying to reformat a current list of all the students where I work. The text file I have has 2-3 students per line and each are delimited by a space. I want to be able to create a list with each student on it's own line. Not every line has 3 students though, some only have 2.
What I find happening is that on the lines where there are only 2 students I am getting a "Echo is off." being redirected to the new list that I create. Obviously, I don't want this in there. Here is the code I have so far.
Code: Select all
@echo off
for /f "tokens=1-3 usebackq" %%a in ("C:\Users\Peter\Desktop\Removingusers\userstodelete.txt") do (
if "%%a"=="Echo is off." (%%a > nul) else (@echo %%a >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
if "%%b"=="Echo is off." (%%b > nul) else (@echo %%b >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
if "%%c"=="Echo is off." (%%c > nul) else (@echo %%c >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
)
Any help would be much appreciated
what you are doing are absolutely unnecessary. Use a good tool or a language for file processing. download
gawk for windows and then try this
Code: Select all
C:\test>more file
student1 student2
student3 student4 student5
C:\test>gawk "{for(i=1;i<=NF;i++)print $i}" file
student1
student2
student3
student4
student5
Re: Annoying string wont go away
Posted: 03 Apr 2011 08:58
by flexbuffchest
ghostmachine4 wrote:flexbuffchest wrote:Hey guys,
I am trying to reformat a current list of all the students where I work. The text file I have has 2-3 students per line and each are delimited by a space. I want to be able to create a list with each student on it's own line. Not every line has 3 students though, some only have 2.
What I find happening is that on the lines where there are only 2 students I am getting a "Echo is off." being redirected to the new list that I create. Obviously, I don't want this in there. Here is the code I have so far.
Code: Select all
@echo off
for /f "tokens=1-3 usebackq" %%a in ("C:\Users\Peter\Desktop\Removingusers\userstodelete.txt") do (
if "%%a"=="Echo is off." (%%a > nul) else (@echo %%a >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
if "%%b"=="Echo is off." (%%b > nul) else (@echo %%b >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
if "%%c"=="Echo is off." (%%c > nul) else (@echo %%c >> C:\Users\Peter\Desktop\Removingusers\reformat.txt)
)
Any help would be much appreciated
what you are doing are absolutely unnecessary. Use a good tool or a language for file processing. download
gawk for windows and then try this
Code: Select all
C:\test>more file
student1 student2
student3 student4 student5
C:\test>gawk "{for(i=1;i<=NF;i++)print $i}" file
student1
student2
student3
student4
student5
Alright, that actually worked perfectly
. Thanks a lot.
Re: Annoying string wont go away
Posted: 03 Apr 2011 10:31
by !k
Code: Select all
@echo off &setlocal enableextensions
for /f "usebackq tokens=1,*" %%a in ("%~1") do call :parse "%%a" "%%b"
goto :eof
:parse
echo.%~1
for /f "tokens=1,*" %%c in (%2) do call :parse "%%c" "%%d"
goto :eof
z:\>more file
student1 student2
student3 student4 student5
student6 student7 student8 student9 student10
z:\>recursion file
student1
student2
student3
student4
student5
student6
student7
student8
student9
student10
Re: Annoying string wont go away
Posted: 03 Apr 2011 16:50
by aGerman
There's no need to download a third party tool for such a simple exercise.
Code: Select all
@echo off &setlocal
>"C:\Users\Peter\Desktop\Removingusers\reformat.txt" type nul
for /f "usebackq tokens=*" %%a in ("C:\Users\Peter\Desktop\Removingusers\userstodelete.txt") do (
for %%b in (%%a) do >>"C:\Users\Peter\Desktop\Removingusers\reformat.txt" echo %%b
)
Regards
aGerman
Re: Annoying string wont go away
Posted: 03 Apr 2011 18:55
by ghostmachine4
aGerman wrote:There's no need to download a third party tool for such a simple exercise.
downloading stuff happens everyday. No excuse not to if you want to get the job done quickly and with ease of maintenance. Even M$ download stuff at the back end without anyone's notice. Windows update, anti-virus updates etc. Well even if one cannot download stuff, I would still recommend vbscript/powershell rather than batch.
Downloading a third party tool that can do a lot more than just create one-liners or this "simple exercise" is certainly one to keep in an administrator's arsenal. It (gawk) can replace what cmd.exe can provide (ie the programming features). Of course using a programming language (Python/Perl/Ruby) is even better. how many for loops do you need to create in batch to do this "simple exercise"? A simple way to do a simple exercise is preferred, than creating humongous lines of code to do that "simple exercise".
Last but not least. apparently OP seems to get it done. So it is possible for him to get 3rd party tools. Enough said