Annoying string wont go away

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
flexbuffchest
Posts: 6
Joined: 26 Jan 2011 16:19

Annoying string wont go away

#1 Post by flexbuffchest » 03 Apr 2011 08:13

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Annoying string wont go away

#2 Post by ghostmachine4 » 03 Apr 2011 08:34

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

flexbuffchest
Posts: 6
Joined: 26 Jan 2011 16:19

Re: Annoying string wont go away

#3 Post by flexbuffchest » 03 Apr 2011 08:58

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 :D. Thanks a lot.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Annoying string wont go away

#4 Post by !k » 03 Apr 2011 10:31

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


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Annoying string wont go away

#5 Post by aGerman » 03 Apr 2011 16:50

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Annoying string wont go away

#6 Post by ghostmachine4 » 03 Apr 2011 18:55

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

Post Reply