Windows批处理脚本用2个空格替换选项卡

时间:2017-07-10 07:13:25

标签: windows batch-file

我需要使用Windows批处理脚本在指定路径上的所有文件中用2个空格替换tab。我发现将标签字符正确输入批处理文件并不容易。谁能帮忙。感谢

1 个答案:

答案 0 :(得分:1)

编辑更正了前导空格的输出。

棘手的部分是抓住tab字符。可能是......

@echo off
setlocal EnableDelayedExpansion

rem Grab TAB character 
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=7898
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"

rem set desired number of spaces
set "SPACES=  "

pushd "your_desired_path"

for /f "delims=" %%f in ('dir /B *.txt') do ( 
  > "output_%%~nxf" (
    for /f "tokens=* delims=" %%i in (%%~nxf) do (
      set "myData=%%i"
      set "myData=!myData:%TAB%=%SPACES%!"
      echo !myData!
    )
  )
  echo copy /y "output_%%~nxf" "%%~nxf" 
  echo del "output_%%~nxf" >NUL 2>NUL
)    

popd

endlocal
exit/B

从上面的echocopy行中移除del,如果它似乎可以正常工作。