1

I have a header text file with all the column names used as a template. I have a bunch of file types which open in notepad with the ext of .cos

What I have been doing so far is pending the header file with the content of all .cos files by using the following:

type *.cos >> header.txt

However I need to know how I can append the header files to each .cos individually and have an output name as a input name of the .cos file (maybe into a new directory) so it wont overwrite the original

if anyone can please help.

Thank you

 @echo off
 setlocal enabledelayedexpansion

 set C:\Users\hyea\Desktop\Testing\Source=src
 set C:\Users\hyea\Desktop\Testing\Target=dst
 set C:\Users\hyea\Desktop\Testing\Target="%_DSTDIR%\target.txt"

 type "%C:\Users\hyea\Desktop\Testing\Source%\header.txt" 
 >    %C:\Users\hyea\Desktop\Testing\Target%

 for /f "tokens=*" %%f in ('dir /b /a-d    
 "!C:\Users\hyea\Desktop\Testing\Source!\*.cos"') do (
  type "!C:\Users\hyea\Desktop\Testing\Source!\%%f" >>  
  %C:\Users\hyea\Desktop\Testing\Target%
)
abs786123
  • 537
  • 8
  • 22
  • So you want to _append_ headers to `.cos` files or do you want them at the first line, 'cause it's a header? – Clijsters Sep 16 '16 at 10:03
  • Yes, I would just want to append the header(which is just one row with column names) to be appended to the .cos file at the top row – abs786123 Sep 16 '16 at 10:05
  • However I want this to be done for each .cos file but not sure how to go about doing it for each .cos file that exists in a folder. – abs786123 Sep 16 '16 at 10:07
  • btw, this isn't [appending](https://en.wikipedia.org/wiki/Append) ;) – Clijsters Sep 16 '16 at 10:09
  • haha, sorry what is the correct definition? would it be possible? – abs786123 Sep 16 '16 at 10:10
  • 2
    [prepend](http://english.stackexchange.com/questions/79323/is-there-a-word-meaning-append-but-at-the-beginning-not-the-end) Sure! have a look at [This SO question and it's answer (forfiles at Windows Vista and above](http://stackoverflow.com/questions/180741/how-to-do-something-to-each-file-in-a-directory-with-a-batch-script) – Clijsters Sep 16 '16 at 10:12

1 Answers1

3

To set the source/target dirs, simply change _SRCDIR/_DSTDIR variables (do not enclose them doublequotes(") since that is automatically handled by the code)

Here's the code:

@echo off
setlocal enabledelayedexpansion

set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source
set _DSTDIR=C:\Users\hyea\Desktop\Testing\Target

for /f "delims=" %%f in ('dir /b /a-d "!_SRCDIR!\*.cos"') do (
    type "!_SRCDIR!\header.txt" > "!_DSTDIR!\%%f"
    call :handle_file "!_SRCDIR!\%%f" "!_DSTDIR!\%%f"
)
goto :eof

:handle_file
    set _TMPVAR0=%~t1
    setlocal disabledelayedexpansion
    for /f "usebackq delims=" %%g in (`"findstr /n ^^ %1"`) do (
        echo %%g
        set "_TMPVAR1=%%g"
        setlocal enabledelayedexpansion
        set "_TMPVAR1=!_TMPVAR1:*:=!"
        echo.%~1 !_TMPVAR0: =-! !_TMPVAR1!>> %2
        endlocal
    )
    goto :eof

@EDIT0: Replaced sample paths (that I used) with actual paths on OP's machine.

@EDIT1: Modified the code that it dumps header contents + each .cos contents into a separate file. Note: now _SRCDIR and _DSTDIR must be different!!!

@EDIT2: Added the name/date functionality as requested in comment (I replaced the SPACE characters in date by HYPHEN s, so the date won't count as multiple columns if SPACE is a column separator). The file reading (handle_file) is copied (and adapted) from @jeb 's answer.

Community
  • 1
  • 1
CristiFati
  • 28,721
  • 9
  • 41
  • 63
  • 1
    If you're using Windows Vista or later, you can also use [`forfiles`](https://technet.microsoft.com/en-US/library/cc753551(v=ws.10).aspx) – Clijsters Sep 16 '16 at 10:14
  • Would this keep the original name of the .cos file into the desitination? – abs786123 Sep 16 '16 at 10:17
  • @abs786123: The destination is `_TARGETFILE`; it can be set to anything (currently: _target.txt_). – CristiFati Sep 16 '16 at 10:19
  • @Clijsters: Thank you, I know about `forfiles`, but personally I feel more comfortable using the old fashioned `for` (although some might say that it's reinventing the wheel on some degree). Also, sometimes I use and old PC running _XP_ :). – CristiFati Sep 16 '16 at 10:22
  • @CristiFati sorry, i just can't seem to get this working, i have added my directory as above, but when i open the file as a .bat it doesn't do anything. I know its something I am doing wrong. I have the code at the top of the post. – abs786123 Sep 16 '16 at 10:51
  • @abs786123, it does not make much sense to put a copy of this code in your question; anyway, you were setting the variables wrong, you need to replace the sample *values* `src` and `dst` by the true paths, but you replaced the variable *names*! – aschipfl Sep 16 '16 at 10:53
  • This line: `set C:\Users\hyea\Desktop\Testing\Source=src` and all the others are not OK. Use it like this: `set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source`, and `set _DSTDIR=C:\Users\hyea\Desktop\Testing\Target`, and no other modifications. Should I update my answer to have your specific paths? – CristiFati Sep 16 '16 at 10:54
  • Yes please if you could, I am completely useless at DOS as i never used it. – abs786123 Sep 16 '16 at 10:59
  • 3
    @Clijsters, I would almost always go for the classical `for` loop, unless some (relative) date filtering is needed (`forfiles /D`), because `forfiles` requires the commands to execute to be embedded in a separate `cmd` instance, which slows down the loop and may cause difficulties to build the correct syntax (some escaping might be necessary which makes the command line hardly legible, and if there are more than a few commands in the body, the line could become uncomfortably long)... – aschipfl Sep 16 '16 at 10:59
  • If you have got the source directory `C:\Users\hyea\Desktop\Testing\Source`, simply replace `set _SRCDIR=src` by `set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source` (the same for the other `set` command lines, of course)... – aschipfl Sep 16 '16 at 11:01
  • Thank you all, got it partly working in the sense where the header is preappended the the .cos. however how would i rename the target file to be the name of the .cos file? – abs786123 Sep 16 '16 at 11:08
  • Could this be altered slightly to loop through all cos file and pre-apprehend them individually opposed to all in one target file? So it will create a target file for each .cos file with the .cos file as the new name of the target document. thank t – abs786123 Sep 16 '16 at 11:12
  • So, you want to have a target file for each _.cos_ file? Currently what it does it dumps all the _.cos_ files content in the same target file. – CristiFati Sep 16 '16 at 11:13
  • Yes please, otherwise this does pretty much as what I posted in my original post, or atleast the output looks the same. – abs786123 Sep 16 '16 at 11:15
  • Thank you ever so much @CristiFati, I really appreciate your help and everyone else. I may have to learn DOS as i wasn't aware how powerful it actually is. One last thing , just want to know if its possible, is there anyway to add the name and creation date to the .cos file at all inside of the text file using the token this so it appears in the first two columns? – abs786123 Sep 16 '16 at 11:22
  • Glad to be helpful! For the name part yes it can be done, but this require extra processing: The file must be read line by line and each line processed and redirected to the target file. Check [this answer](http://stackoverflow.com/questions/39469919/find-replace-string-using-for-f-with-if-statement-and-variables/39475145#39475145) (and comments) regarding going over file contents. For the date part, I'm not sure how to get that from batch, one way is parse `dir` output, to get the date, but that shows when the file was last written (not created). – CristiFati Sep 16 '16 at 14:37
  • Thank you so much, any chance of just extracting the name of the file from the full path and having a comma straight after it? same for the last written date as well with the comma. I have tried all day without success! – abs786123 Sep 18 '16 at 18:08
  • Then you need to replace the line; `echo.%~1 !_TMPVAR0: =-! !_TMPVAR1!>> %2`, by: `echo.%~n1%~x1, !_TMPVAR0: =-!, !_TMPVAR1!>> %2`. – CristiFati Sep 19 '16 at 09:17