对文件夹中的多个文件使用批处理删除马车 Returns
Remove Carriage Returns using batch for multiple files in folder
我在一个文件夹中有 30 个文本文件。我需要删除所有数据中的 breaks/carriage 行中断。我已经在使用批处理来完成其他任务,所以继续使用它会很好。现在批处理正在将所有扩展名从 .csv 重命名为 .txt,然后启动一个 excel 文件。
我搜索了很多,但找不到我需要的任何安静的东西。我只是简单地涉足了批处理脚本,所以用蜡笔画出来,这样我就能理解发生了什么会很棒。
您可以使用 built-in 可执行文件 certutil
将文件从 ASCII 转换为十六进制,使用 for /f
循环处理生成的文件,去除任何 0d
(这是十六进制的回车 return 字符)从每一行开始,并从剩余的十六进制代码重建文件。我发誓这比我说的容易。
请注意,由于 certutil
的限制,输入文件的最大大小限制为 71 MB,大于 2 MB 的文件可能需要一段时间才能处理,但至少一切都是原生的至 Windows,因此您无需安装任何东西或学习一门全新的语言。
@echo off
setlocal enabledelayedexpansion
:: Ensure a file was passed in
if "%~1"=="" (
echo Please provide a file to process.
pause
exit /b
)
:: Ensure file is under the certutil input limit
if %~z1 GTR 74472684 (
echo This file exceeds the maximum file size of 74472684 bytes ^(71 MB^)
echo Please use a smaller file.
pause
exit /b
)
:make_rand
:: Generate a random number to reduce the risk of filename collision
set rand=%RANDOM%%RANDOM%
set "temp_file=%~dpf1_%rand%.tmp"
set "hex_file=%~dpf1_%rand%.hex"
set "new_file=%~dpf1_new.%~x1"
if exist %temp_file% goto :make_rand
if exist %hex_file% goto :make_rand
if exist %new_file% choice /c:YN /M "%new_file% already exists. Overwrite? "
if %errorlevel% equ 1 del %new_file%
if %errorlevel% equ 2 exit /b
certutil -encodehex "%~1" "%temp_file%"
:: The script will break if you have spaces in your file path.
:: This is a feature, not a bug. Names your paths correctly.
for /f "tokens=1,*" %%A in (%temp_file%) do (
set "line=%%B"
set "hex_substring=!line:~0,48!"
set "no_carriage=!hex_substring:0d=!"
echo !no_carriage! >>%hex_file%
)
certutil -decodehex "%hex_file%" "%new_file%"
:: Temp file cleanup
del /q %hex_file%
del /q %temp_file%
我在一个文件夹中有 30 个文本文件。我需要删除所有数据中的 breaks/carriage 行中断。我已经在使用批处理来完成其他任务,所以继续使用它会很好。现在批处理正在将所有扩展名从 .csv 重命名为 .txt,然后启动一个 excel 文件。
我搜索了很多,但找不到我需要的任何安静的东西。我只是简单地涉足了批处理脚本,所以用蜡笔画出来,这样我就能理解发生了什么会很棒。
您可以使用 built-in 可执行文件 certutil
将文件从 ASCII 转换为十六进制,使用 for /f
循环处理生成的文件,去除任何 0d
(这是十六进制的回车 return 字符)从每一行开始,并从剩余的十六进制代码重建文件。我发誓这比我说的容易。
请注意,由于 certutil
的限制,输入文件的最大大小限制为 71 MB,大于 2 MB 的文件可能需要一段时间才能处理,但至少一切都是原生的至 Windows,因此您无需安装任何东西或学习一门全新的语言。
@echo off
setlocal enabledelayedexpansion
:: Ensure a file was passed in
if "%~1"=="" (
echo Please provide a file to process.
pause
exit /b
)
:: Ensure file is under the certutil input limit
if %~z1 GTR 74472684 (
echo This file exceeds the maximum file size of 74472684 bytes ^(71 MB^)
echo Please use a smaller file.
pause
exit /b
)
:make_rand
:: Generate a random number to reduce the risk of filename collision
set rand=%RANDOM%%RANDOM%
set "temp_file=%~dpf1_%rand%.tmp"
set "hex_file=%~dpf1_%rand%.hex"
set "new_file=%~dpf1_new.%~x1"
if exist %temp_file% goto :make_rand
if exist %hex_file% goto :make_rand
if exist %new_file% choice /c:YN /M "%new_file% already exists. Overwrite? "
if %errorlevel% equ 1 del %new_file%
if %errorlevel% equ 2 exit /b
certutil -encodehex "%~1" "%temp_file%"
:: The script will break if you have spaces in your file path.
:: This is a feature, not a bug. Names your paths correctly.
for /f "tokens=1,*" %%A in (%temp_file%) do (
set "line=%%B"
set "hex_substring=!line:~0,48!"
set "no_carriage=!hex_substring:0d=!"
echo !no_carriage! >>%hex_file%
)
certutil -decodehex "%hex_file%" "%new_file%"
:: Temp file cleanup
del /q %hex_file%
del /q %temp_file%