unix2dos 批處理實現

來源:互聯網
上載者:User

DOS/Windows和Linux/Unix的檔案換行斷行符號格式不同,基於 DOS/Windows 的文字檔在每一行末尾有一個 CR(斷行符號)和 LF(換行),而 UNIX 文本只有一個換行。

在這兩個作業系統下傳輸檔案時需要進行格式轉換,Unix環境下的檔案格式轉換工具有dos2unix命令,

Windows環境下可以用UltraEdit進行轉換。

也可以用一個批處理指令碼實現

實現的想法來源於 csdn

http://topic.csdn.net/u/20100330/15/805aa786-eb37-4ec9-bd52-2c6b7936cd34.html?seed=1752333021&r=72909758#r_72909758

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::    一。 準備工作

:: 要先輸入命令 cmd /v:on  開啟環境變數延遲。因為預設情況下環境變數延遲是關閉的

@echo off

:: 轉換成中文字碼頁

chcp 936

::設定要轉換檔格式的”根“
檔案夾。也是記錄檔所在路徑

set /p route=請指定要轉換的單個檔案的名字(包括路徑)
或者批量轉換
檔案所在目錄
:

:: 抑制錯誤輸出

cd /d %route% 2>nul

if errorlevel 1 goto single

::得到”所輸入路徑下” 的 “所有目錄名”, 存放於 建立的臨時檔案夾 ###temp

rd /s/q %route%/###temp

md %route%/###temp

for /r %%i in (.) do echo %%i >> %route%/###temp/ddd.txt

for /f "delims=." %%i in (%route%/###temp/ddd.txt) do echo %%i >> %route%/###temp/ddd2.txt

del %route%/###temp/ddd.txt

ren %route%/###temp/ddd2.txt ddd.txt

::::::::::::::::::::::::::::::::::::::::::

::取 各個目錄下的檔案名稱, 不帶路徑。同時排除所有目錄名 dir /b /a-d

:: ”每一個目錄下“ 的 “所有檔案名稱” 組成一個文字檔, 即 1.txt 2.txt...

set sequence=1001

for /f %%i in (%route%/###temp/ddd.txt) do (

dir /b /a-d %%i >> %route%/###temp/!sequence!.txt

set /a sequence+=1

)

echo 準備工作完成 !

echo.

echo 轉換開始。詳細情況請看日誌 %route%/###temp/unix2dos.log

echo 請稍等 ……

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::    二。 記錄日誌,轉換開始。

::        思路:取 “每一個目錄” ---》 

::                取 “每一個目錄” 裡的 “每一個檔案” ---》

::                         取 “每一個檔案” 的 “每一行”

::        即 三層 for 迴圈

echo %date% %time% >> %route%/###temp/unix2dos.log

echo Translation Begin Now ! >> %route%/###temp/unix2dos.log

:: 所有路徑名 所在檔案 ddd.txt 中 每一行 對應一個 號, 即 sequence

set sequence=1001

for /f %%i in (%route%/###temp/ddd.txt) do (

cd /d %%i

for /f %%j in (%route%/###temp/!sequence!.txt) do (

for /f "tokens=1,2* delims=@" %%k in ('type %%j') do (

echo %%k >> ###%%j

)

:: ###%%j 乃是臨時所用檔案名稱 *^__^*

del %%j

ren ###%%j %%j

echo %date% %time% File %%i%%j                  has been translated successfully >> %route%/###temp/unix2dos.log

)

set /a sequence+=1

echo. >> %route%/###temp/unix2dos.log

echo
%date% %time%  All the file in the Directory %%i         have been
translated successfully >> %route%/###temp/unix2dos.log

echo. >> %route%/###temp/unix2dos.log

echo. >> %route%/###temp/unix2dos.log

)

goto success

:single

if not exist %route% goto end

for /f "tokens=1,2* delims=@" %%i in ('type %route%') do echo %%i%%j >> %route%###

move /y %route%### %route%

:success

echo 轉換完成

:end

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.