[HELP] Writing To File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
이용민
Posts: 5
Joined: 17 Mar 2012 13:24

[HELP] Writing To File

#1 Post by 이용민 » 17 Mar 2012 13:27

What I want to do is basically write information to X amount of files at the same time. I have a script that is working at the moment, but it misses out the files that have spaces in their name. If anyone can help me, it'd be greatly appreciated.

Here is the script:

Code: Select all

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.cfg') do (
>> # echo.Test=0
for /f "tokens=* delims= " %%i in (%%a) do (
>> # echo.%%i
)
move /y # %%a
)

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

Re: [HELP] Writing To File

#2 Post by aGerman » 17 Mar 2012 13:43

Not exactly sure what you're trying to do.

Code: Select all

for /f "delims=" %%a in ('dir/b *.cfg') do (
  ># echo(Test=0
  for /f "usebackq tokens=* delims= " %%i in ("%%a") do (
    >># echo(%%i
  )
  move /y # "%%a"
)

If you only want to prepend the line "Test=0" you don't need the 2nd loop.

Code: Select all

for /f "delims=" %%a in ('dir/b *.cfg') do (
  ># echo(Test=0
  >># type "%%a"
  move /y # "%%a"
)

Regards
aGerman

이용민
Posts: 5
Joined: 17 Mar 2012 13:24

Re: [HELP] Writing To File

#3 Post by 이용민 » 17 Mar 2012 13:46

Basically, I have a folder that contains X amount of CFG files. Some of the file names don't have spaces, some do have spaces. When I run the bat, it works correctly by prepending the line Test=0 into the files that don't have spaces, but it does not prepend the Test=0 line with the files that have spaces. I want it to prepend the Test=0 into all files, with and without spaces.

Thank you for your reply, and any further help would greatly be appreciated.

이용민
Posts: 5
Joined: 17 Mar 2012 13:24

Re: [HELP] Writing To File

#4 Post by 이용민 » 17 Mar 2012 13:52

Ok, it now seems to work fine (even with spaces) from using the code you posted above, but now I have a question on how to make it add it to the 'end' of the file. It is currently adding the text to the top of the file, and I want it added to the bottom.

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

Re: [HELP] Writing To File

#5 Post by aGerman » 17 Mar 2012 13:55

That's much easier.

Code: Select all

for /f "delims=" %%a in ('dir/b *.cfg') do >>"%%a" echo(Test=0


Regards
aGerman

이용민
Posts: 5
Joined: 17 Mar 2012 13:24

Re: [HELP] Writing To File

#6 Post by 이용민 » 17 Mar 2012 14:06

Thank you, and what method would I use to prepend more than 1 line?

Code: Select all

for /f "delims=" %%a in ('dir/b *.cfg') do >>"%%a" echo(Test=0 echo(Test2=0


????

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

Re: [HELP] Writing To File

#7 Post by aGerman » 17 Mar 2012 14:11

I assume you mean appending more than one line.

Code: Select all

for /f "delims=" %%a in ('dir/b *.cfg') do (
  >>"%%a" echo(Test=0
  >>"%%a" echo(Test2=0
)

Regards
aGerman

이용민
Posts: 5
Joined: 17 Mar 2012 13:24

Re: [HELP] Writing To File

#8 Post by 이용민 » 17 Mar 2012 14:12

Yeah, I just figured that out now :)

Thank you for all your help. It is greatly appreciated.

Post Reply