我已经找到了如何在中用单个空格替换多个空格--一个文件,但是我有40个文件要做同样的事情(根据我的分析,可以做更多的,这是不同的)。
该程序在“列”之间保存具有不同空间的txt文件。
前:
Position Depth SI
*****************************************
655699.24 65.61 1.00
655559.32 176.41 1.00
655391.55 289.25 1.00
655205.71 428.69 1.00
655094.18 518.95 1.00
653459.86 1611.16 1.00
653277.26 1743.64 1.00
653525.66 1602.15 1.00
653596.39 1573.19 1.00
653585.57 1591.86 1.00
653548.80 1628.71 1.00
653523.38 1655.96 1.00
653494.68 1688.01 1.00
653427.01 1729.50 1.00
653327.08 1782.66 1.00
653208.49 1832.50 1.00我用过:
@ECHO OFF
SETLOCAL
::
:: delete first line/lines
:: and replace each occurrence of 2 or more spaces
:: by a delimiter
::
DEL output.txt 2>nul /F /Q
:: replace with ->
SET delim=
:: set number of lines to delete
FOR /f "skip=1delims=" %%i IN (L1.txt) DO (
SET line=%%i
(SET newline=)
SET count=0
CALL :change
)
GOTO :eof
:CHANGE
SET c1=%line:~0,1%
SET line=%line:~1%
IF "%c1%"==" " (SET /a count+=1) ELSE (
IF %count%==0 (SET newline=%newline%%c1%) ELSE (
IF %count%==1 (SET newline=%newline% %c1%) ELSE (
SET newline=%newline%%delim%%c1%)
SET count=0
)
)
IF DEFINED line GOTO CHANGE
::
:: You may want to preserve trailing spaces
:: or convert them...
::
IF %count%==0 GOTO print
IF %count%==1 SET newline=%newline% &GOTO print
SET newline=%newline%%delim%
:PRINT
>>output.txt ECHO %newline%
GOTO :eof$使用此代码,我将输入和输出文件的名称替换了40次,是否有任何自动替换代码中不需要手动替换输入和输出文件名的空格的方法?
有什么想法吗?
发布于 2018-06-18 14:23:20
您还可以使用批处理文件中的PowerShell,将每个系列的多个空格字符减少为一个字符:
@PowerShell -NoP "GCI 'C:\Users\kvratto\MyFolder' -Fi *.txt|Select -Exp FullName|%%{(GC $_) -Replace '\s+',' '|SC $_}"只需将C:\Users\kvratto\MyFolder调整为包含.txt文件的持有目录
https://stackoverflow.com/questions/50910509
复制相似问题