views:

1669

answers:

5

Does anyone know of a way (say Powershell, or a tool) in Windows that can recurse over a directory and convert any unix files to windows files.

I'd be perfectly happy with a way in Powershell to at least detect a unix file.

It's easy do this for one single file, but I'm after something a bit more scalable (hence leaning towards a Powershellish solution).

+7  A: 

There is dos2unix and unix2dos in Cygwin.

Hemal Pandya
I'd recommend this technique as the unix utils will do a better job maintaining the original file encoding (UTF-8, ASCII, etc). I've had problems with PS in the past when I intended to keep ASCII files ASCII.
Peter Seale
or msys and then you can use the utilities from cmd.
Pod
+1  A: 

If Cygwin isn't for you, there are numerous standalone executables for unix2dos under Windows if you Google around, or you could write one yourself, see my similar (opposite direction for conversion) question here.

ninesided
+3  A: 

download vim, open your file and issue

:se fileformat=dos|up

Batch for multiple files (all *.txt files in C:\tmp - recursive):

:args C:\tmp\**\*.txt
:argdo se fileformat=dos|up
soulmerge
can you do this for a folder or as a batch job?
ninesided
Or download Eclipse, open the file and convert line delimeters to Unix. vim is no doubt a great tool and I use it every day. But don't you think it is a bit of overkill to use for converting endofline?
Hemal Pandya
It's just the first thing that came to my mind, it's on every box I own/administer. Btw: are you actually suggesting using eclipse (85MB) and doing it file-by-file instead of using vim (8.5MB) and doing it all at once?
soulmerge
+1  A: 

You can use Visual Studio. File -> Advanced Save Options...

jeffamaphone
+6  A: 

Here is the pure PowerShell way if you are interested.

Finding files with atleast one UNIX line ending (PowerShell v1):

dir * -inc *.txt | %{ if (gc $_.FullName -delim "`0" | Select-String "[^`r]`n") {$_} }

Here is how you find and covert UNIX line endings to Windows line endings. One important thing to note is that an extra line ending (\r\n) will be added to the end of the file if there isn't already a line ending at the end. If you really don't want that, I'll post an example of how you can avoid it (it is a bit more complex).

Get-ChildItem * -Include *.txt | ForEach-Object {
    ## If contains UNIX line endings, replace with Windows line endings
    if (Get-Content $_.FullName -Delimiter "`0" | Select-String "[^`r]`n")
    {
     $content = Get-Content $_.FullName
     $content | Set-Content $_.FullName
    }
}

The above works because PowerShell will automatically split the contents on \n (dropping \r if they exist) and then add \r\n when it writes each thing (in this case a line) to the file. That is why you always end up with a line ending at the end of the file.

Also, I wrote the above code so that it only modifies files that it needs to. If you don't care about that you can remove the if statement. Oh, make sure that only files get to the ForEach-Object. Other than that you can do whatever filtering you want at the start of that pipeline.

JasonMArcher
Does this maintain ASCII encoding for ASCII files? ...
Peter Seale
By default PowerShell works in "Unicode". I'm no expert on text encoding, but I haven't run into problems with the defaults yet. If you wish, you can explicitly set an encoding for the Get-Content and Set-Content commands with the -Encoding parameter. Get-Help Get-Content -Parameter Encoding
JasonMArcher