3
Contributors
14
Replies
29
Views
4 Months
Discussion Span
Last Post by Reverend Jim
Featured Replies
  • Easily done, but what do you do if the folder contains an executable or an image file?You don't want to delete ^M in those. Read More

1

Easily done, but what do you do if the folder contains an executable or an image file?You don't want to delete ^M in those.

0

Thanks for the response. The folders only contains C++ code files. Lets say here we have 10 folders and each folder contains 50 c++ code files.

0

Forgot to ask. Is this for Windows or for linux? I would imagine linux or there would be no need to strip cr but you never know.

Edited by Reverend Jim

0

This is for windows. dos2unix will not work in this case. shell script may help here

0

What do you mean by "shell script" then?
What environment are you working in?
Can you run bash? If so, you could run the following one-liner in the directory with the cpp files. First make a new directory in it called "fixed" where the fixed files will go.

for f in *.cpp; do cat "$f" | tr -d '\r' > "fixed/$f"; done

Edited by John_191

0

The code files are in windows local. When I move them to the server (File transfer - binary) and check them using below command, I couls see the ^M characters in all the files.

command = vi filename

0

In that case, you shouldn't be transferring them with FTP in binary mode! Use text mode and it will automatically fix the line endings for you.

Also, that one-liner should work on AIX.

Edited by John_191

0

yes you are correct, but we are using the code files in other application after transferring them as binary. We are getting control M character error on that application. so trying to create a script file to remove all the hidden ^M character.

0

In that case you can run the following script on Windows. Copy the code into a file, stripcr.vbs. Run it in the current folder by specifying the file extensions that you want to process like stripcr cpp h txt. Note that if you haven't previously done

cscript //h:cscript //save

to set cscript as the default you will have to run the script as

cscript stripcr.vbs cpp h txt

or you will get a MsgBox popup for every file processed (really annoying).

set fso = CreateObject("Scripting.FileSystemObject")

for each ext in wscript.arguments

    ext = lcase(ext)

    for each file in fso.GetFolder(".").Files
        if lcase(fso.GetExtensionName(file)) = ext then
            Wscript.Echo "Processing",file.Name
            text = fso.OpenTextFile(file.Name).ReadAll
            text = Replace(text,vbCr,"")
            fso.OpenTextFile(file.Name,2).Write text
        end if
    next

next
0

You could add some protection against stripcr mangling binary files by searching for byte values above 127 (and 0 byte values).

Too bad he couldn't use GNU sed:

sed -i 's/\r//' *.cpp

(although I guess he'd be able to use dos2unix in that case)

Edited by John_191

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.