網上有相似的回答,我手機不方便,先給你轉來。。。有要求再提。。spa
個人代碼,文件已經上傳,後面下載.net
@echo off&color 2borm
echo [請拖入你要處理的**文件夾**]token
for /f "tokens=* delims=" %%e in ('dir %cdstr%\*.txt /s /b') do (get
for /f "tokens=1* delims= " %%i in ('findstr /n ".*" "%%e"') do (it
echo %%j>>%%~dpe%%~ne.bakio
)class
move "%%~dpe%%~ne.bak" "%%~dpe%%~ne.txt"test
)百度
***************************************
網上其餘人的
@echo off & setlocal enabledelayedexpansion
set file=test.txt
:choose
echo.&echo 1 - 去除左邊空格
echo.&echo 2 - 去除右邊空格
echo.&echo 3 - 去除首尾空格
echo.&echo 4 - 去除所有空格
echo.&echo 5 - 去除所有空行
echo.&echo 6 - 去除所有空格和空行
echo.&echo 0 - 退出批處理
echo.&set /p choose=請選擇序號:
cls
if %choose% equ 0 exit
if %choose% equ 1 (call :1&goto :exit)
if %choose% equ 2 (call :2&goto :exit)
if %choose% equ 3 (call :3&goto :exit)
if %choose% equ 4 (call :4&goto :exit)
if %choose% equ 5 (call :5&goto :exit)
if %choose% equ 6 (call :6&goto :exit)
cls&echo.&echo 輸入的序號有誤,請從新選擇!&goto :choose
:exit
pause>nul
exit
:1
echo.&echo 1 - 去除左邊空格
for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
set /a num+=1
set "var=%%a"
set var=!var:*:=!
if not "!var!"=="" (call ::front)
echo."!var!"
if !num! equ %line% (set /a n+=1&set num=0)
)
goto :eof
:2
echo.&echo 2 - 去除右邊空格
for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
set /a num+=1
set "var=%%a"
set var=!var:*:=!
if not "!var!"=="" (call :later)
echo."!var!"
if !num! equ %line% (set /a n+=1&set num=0)
)
goto :eof
:3
echo.&echo 3 - 去除首尾空格
for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
set /a num+=1
set "var=%%a"
set var=!var:*:=!
if not "!var!"=="" (call ::front&call : later)
echo."!var!"
if !num! equ %line% (set /a n+=1&set num=0)
)
goto :eof
:4
echo.&echo 4 - 去除所有空格
for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
set /a num+=1
set "var=%%a"
set var=!var:*:=!
if not "!var!"=="" (set var=!var: =!)
echo."!var!"
if !num! equ %line% (set /a n+=1&set num=0)
)
goto :eof
:5
echo.&echo 5 - 去除所有空行
for /f "delims=" %%a in ('type "%file%"') do echo."%%a"
goto :eof
:6
echo.&echo 6 - 去除所有空格和空行
for /f "delims=" %%a in ('type "%file%"') do (
set str=%%a
set str=!str: =!
if not "!str!"=="" echo."!str!"
)
goto :eof
:front
if "!var:~0,1!"==" " (set var=!var:~1!&goto :front)
goto :eof
:later
if "!var:~-1!"==" " (set var=!var:~0,-1!&goto :later)
goto :eof