批处理去掉每行前面的空格
网上有类似的回答,我手机不方便,先给你转来。。。有要求再提。。
我的代码,文件已经上传,后面下载
@echo off&color 2b
echo [请拖入你要处理的**文件夹**]
for /f "tokens=* delims=" %%e in ('dir %cdstr%\*.txt /s /b') do (
for /f "tokens=1* delims= " %%i in ('findstr /n ".*" "%%e"') do (
echo %%j>>%%~dpe%%~ne.bak
)
move "%%~dpe%%~ne.bak" "%%~dpe%%~ne.txt"
)
***************************************
网上其他人的
:: 将以下代码保存为.BAT或.CMD格式的批处理后运行
:: By LJ_SunTB【百度知道 BAT-GO 团】
@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