Anyone have a shell script to remove controlM character from all the files in a directory

Recommended Answers

All 14 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.

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.

You can use dos2unix. Just run dos2unix *.cpp (or whatever the suffix is).

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.

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

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

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

I'm working in AIX.

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.

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.

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

Also note that if you run stripcr against an exe you WILL destroy it.

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)

Unfortunately there is no easy way to read binary in vbScript. Too bad because I like to use vbScript for prototyping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.