1

I need to be able to go back and forth between CentOS and Windows for C++ projects that I am developing on both systems. I want to be able to convert all files of a given type such as *.cpp, *.h or *.txt from Unix to Dos.

I have a bash script that converts a single file, but I'd like to be able to type in either just the extension of the file type or *.FILETYPE to convert all the files in a given directory of that type.

Here is the working script, what do I need to do to make it work for more than one file at a time. I've tried using a for loop but I get syntax errors.

#! /bin/sh

# Convert a text based file from Unix Format to Dos Format
# The Output file should be the name of the input file with
# WF inserted before the file extention
#
# Examples
#    convert Unix format abc.txt to abcWF.txt Windows Format 
#    convert Unix format abc.cpp to abcWF.cpp Windows Format 

echo "In u2w FileToConvert = $1"
    FileBase=$(basename $1)
    FileExtension="${FileBase##*.}"
    FileBase="${FileBase%.*}"
    OutputSuffix="WF.$FileExtension"
    OutputFile="$FileBase$OutputSuffix"
    commandment="unix2dos -n $1 $OutputFile"
    echo $commandment
    unix2dos -n $1 $OutputFile
    echo "diff -w $1 $OutputFile"
    diff -w "$1" "$OutputFile"

exit

Example run

$  bd2w TestResults.txt
In u2w FileToConvert = TestResults.txt
unix2dos -n TestResults.txt TestResultsWF.txt
unix2dos: converting file TestResults.txt to file TestResultsWF.txt in DOS format ...
diff -w TestResults.txt TestResultsWF.txt
  • I'm worried you're not using version control. You shouldn't need copies of the files with similar names; just run unix2dos/dos2unix (which already except wildcards) as needed to change line endings in-place. – drewbenn Sep 16 '16 at 22:09
  • 2
    "I've tried using a for loop but I get syntax errors." if you need help, you need to say what you tried and what the errors were. Did you not try for f in *txt; do bd2w "$f"; done? – drewbenn Sep 16 '16 at 22:26
  • @drewbenn I use github for version control, unfortunately this project isn't ready to go into github yet, because I don't have any private repositories and the code isn't RFUBO. – pacmaninbw Sep 16 '16 at 22:30
  • I would suggest to make a policy to use exactly one file format. Use editors which can handle both file formats. Slap everyone who violates your policy. – Martin Nyolt Sep 17 '16 at 8:42
4

Unix to DOS

To convert, for example, all *.h files to DOS format:

sed -i '/\r/! s/$/\r/' *.h

The condition /\r/! tells sed to skip all lines that already have a carriage return. Because of this, it is safe to run this command multiple times on the same file.

The substitution command, s/$/\r/, adds a carriage return to the end of each line.

The option -i tells sed to change the file in-place. If one wanted to keep backups of the originals:

sed -i.bak '/\r/! s/$/\r/' *.h

DOS to Unix

To convert in-place all *.h files from DOS to Unix:

sed -i 's/\r$//' *.h

This command is also safe to run multiple times on the same files.

OSX/BSD

For BSD(OSX) sed, the argument to the -i option is mandatory, not optional. Thus, use -i.bak to make a backup with extension .bak, or use -i '' to change the files in-place without a backup.

1

Use the find to search for the files you want to convert and add the -exec (or -execdir) action to run the conversion program. For example, to run bd2w on all .cpp and .h files starting from the current directory, use the following command:

find . -type f -name "*.cpp" -o -name "*.h" -exec bd2w '{}' \;

I must say I share the concerns expressed by the commenters above; aren't you making your life unnecessarily hard by going through hoops like this? Even if you are the only developer, if the code lives on two different computers, I'd suggest you set up git locally. Put the repo on a removable device to/from you can push/pull the sources. This will prevent any mistakes trying to keep the sources up to date on both machines and it will solve your encoding problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.