Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何使用批处理文件在文本文件中的所有字符之间添加空格?_Windows_Batch File - Fatal编程技术网

Windows 如何使用批处理文件在文本文件中的所有字符之间添加空格?

Windows 如何使用批处理文件在文本文件中的所有字符之间添加空格?,windows,batch-file,Windows,Batch File,我有一个文本文件,其中列出了字母数字代码,如下所示: 17YYUIO QUICK77 RTY8900 1 7 Y Y U I O P Q U I C K 7 7 7 R T Y 8 9 0 0 Q 我想要一个批处理文件,它要么修改原始文本文件,使所有内容都是双倍行距的,要么只是将结果回显到屏幕上,如下所示: 17YYUIO QUICK77 RTY8900 1 7 Y Y U I O P Q U I C K 7 7 7 R T Y 8 9 0 0 Q 基本上,我正在尝试让Microsoft

我有一个文本文件,其中列出了字母数字代码,如下所示:

17YYUIO
QUICK77
RTY8900
1 7 Y Y U I O P
Q U I C K 7 7 7
R T Y 8 9 0 0 Q
我想要一个批处理文件,它要么修改原始文本文件,使所有内容都是双倍行距的,要么只是将结果回显到屏幕上,如下所示:

17YYUIO
QUICK77
RTY8900
1 7 Y Y U I O P
Q U I C K 7 7 7
R T Y 8 9 0 0 Q
基本上,我正在尝试让Microsoft叙述者从文本文件中读取单个字符,而不是让叙述者尝试将字符发音为单词


关于是否可以使用批处理文件在文本文件中的每个字符之间添加空格,您有什么想法吗?

这在PowerShell中是微不足道的,但批处理有点。。。呃。。。凌乱的:

@echo off
setlocal
set INPUT_FILE=c:\temp\in.txt
for /f "tokens=*" %%L in (%INPUT_FILE%) do call :DO_LINE %%L
endlocal
goto END

:DO_LINE
set LINE_IN=%*
set LINE_OUT=
:DL_LOOP
if "%LINE_IN%" == "" echo %LINE_OUT%&&goto END
if "%LINE_OUT%" == "" (
    set LINE_OUT=%LINE_IN:~0,1%
) else ( 
    set LINE_OUT=%LINE_OUT% %LINE_IN:~0,1%
)
set LINE_IN=%LINE_IN:~1%
goto DL_LOOP
goto END

:END
您需要修改set INPUT_文件行


祝你好运

这在PowerShell中是微不足道的,但批处理有点。。。呃。。。凌乱的:

@echo off
setlocal
set INPUT_FILE=c:\temp\in.txt
for /f "tokens=*" %%L in (%INPUT_FILE%) do call :DO_LINE %%L
endlocal
goto END

:DO_LINE
set LINE_IN=%*
set LINE_OUT=
:DL_LOOP
if "%LINE_IN%" == "" echo %LINE_OUT%&&goto END
if "%LINE_OUT%" == "" (
    set LINE_OUT=%LINE_IN:~0,1%
) else ( 
    set LINE_OUT=%LINE_OUT% %LINE_IN:~0,1%
)
set LINE_IN=%LINE_IN:~1%
goto DL_LOOP
goto END

:END
您需要修改set INPUT_文件行


祝你好运

以下是此任务的完整批处理代码:

@echo off
setlocal EnableExtensions

rem Define the name of the file to encode by inserting a space
rem character between each alphanumeric character in the file.
set "CodeFile=Test.txt"

rem Define the name of a temporary file with the encoded lines.
set "TempFile=%CodeFile%.tmp"

rem Make sure the temporary file does not already exist.
del "%TempFile%" 2>nul

rem Process each non blank line from file to encode.
for /F "usebackq delims=" %%I in ("%CodeFile%") do call :EncodeLine "%%~I"

rem Overwrite the file to encode with the temporary file
rem with the encoded lines if there was one created at all.
if exist "%TempFile%" move /Y "%TempFile%" "%CodeFile%"

rem Restore previous command environment end exit processing
rem of the batch file without falling through to subroutine.
endlocal
goto :EOF

rem The subroutine EncodeLine inserts a space between each
rem character of the string passed to the subroutine and
rem appends the encoded line to the temporary file.

rem This is done by appending the first character from input
rem line to the current output line after an additionally
rem added space character and removing from input line the
rem just copied first character until the input line does
rem not contain anymore any character.

rem Then output the encoded line without first space character
rem with redirecting the output from handle STDERR to the
rem temporary file with appending the line to temporary file.

rem It is important here to have the redirection operator
rem and the temporary file name on left side before the
rem command ECHO to get correct output if the output line
rem ends with 1 to 9 and avoid adding a trailing space on
rem each output line on writing the line into the file.

rem goto :EOF at end results in exiting the subroutine
rem and returning to FOR command line above.

:EncodeLine
set "InputLine=%~1"
set "OutputLine="

:NextChar
set "OutputLine=%OutputLine% %InputLine:~0,1%"
set "InputLine=%InputLine:~1%"
if not "%InputLine%" == "" goto NextChar

>>"%TempFile%" echo %OutputLine:~1%
goto :EOF
注意:此批处理代码不适用于任何文本文件内容,仅适用于仅包含字母数字字符和行结束符的ANSI编码文本文件

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • del/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • 移动/?
  • rem/?
  • 设置/?
  • setlocal/?

另请参阅Microsoft文章。

这里是此任务的完整批处理代码:

@echo off
setlocal EnableExtensions

rem Define the name of the file to encode by inserting a space
rem character between each alphanumeric character in the file.
set "CodeFile=Test.txt"

rem Define the name of a temporary file with the encoded lines.
set "TempFile=%CodeFile%.tmp"

rem Make sure the temporary file does not already exist.
del "%TempFile%" 2>nul

rem Process each non blank line from file to encode.
for /F "usebackq delims=" %%I in ("%CodeFile%") do call :EncodeLine "%%~I"

rem Overwrite the file to encode with the temporary file
rem with the encoded lines if there was one created at all.
if exist "%TempFile%" move /Y "%TempFile%" "%CodeFile%"

rem Restore previous command environment end exit processing
rem of the batch file without falling through to subroutine.
endlocal
goto :EOF

rem The subroutine EncodeLine inserts a space between each
rem character of the string passed to the subroutine and
rem appends the encoded line to the temporary file.

rem This is done by appending the first character from input
rem line to the current output line after an additionally
rem added space character and removing from input line the
rem just copied first character until the input line does
rem not contain anymore any character.

rem Then output the encoded line without first space character
rem with redirecting the output from handle STDERR to the
rem temporary file with appending the line to temporary file.

rem It is important here to have the redirection operator
rem and the temporary file name on left side before the
rem command ECHO to get correct output if the output line
rem ends with 1 to 9 and avoid adding a trailing space on
rem each output line on writing the line into the file.

rem goto :EOF at end results in exiting the subroutine
rem and returning to FOR command line above.

:EncodeLine
set "InputLine=%~1"
set "OutputLine="

:NextChar
set "OutputLine=%OutputLine% %InputLine:~0,1%"
set "InputLine=%InputLine:~1%"
if not "%InputLine%" == "" goto NextChar

>>"%TempFile%" echo %OutputLine:~1%
goto :EOF
注意:此批处理代码不适用于任何文本文件内容,仅适用于仅包含字母数字字符和行结束符的ANSI编码文本文件

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • del/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • 移动/?
  • rem/?
  • 设置/?
  • setlocal/?
另请参阅Microsoft文章

此解决方案充分利用了
cmd.exe
中的
/U
开关创建Unicode输出这一事实,即每个输入字符转换为两个输出字节,其中每对的第一个字节为二进制零。这些“0字符”对由
find
命令读取,该命令将每个零作为行尾标记。最后的结果是,
%%b
for命令中的
可替换参数以非常简单的方式获取输入行的每个字符

该程序从输入文件中删除感叹号;如果需要,此点可能是固定的

编辑:修改为回复评论的方法

我修改了原始方法,因此它现在可以管理几乎所有的特殊字符(引号除外):

第二次编辑:我根据dbenham的建议进一步修改了该方法,现在它可以管理所有字符

input.txt:

1 7<Y>IO
QU|C"K&7
;T Y!9^0
1   7 < Y > I O
Q U | C " K & 7
; T   Y ! 9 ^ 0
此解决方案充分利用了
cmd.exe
中的
/U
开关创建Unicode输出这一事实,即每个输入字符转换为两个输出字节,其中每对的第一个字节为二进制零。这些“0字符”对由
find
命令读取,该命令将每个零作为行尾标记。最后的结果是,
%%b
for
命令中的
可替换参数以非常简单的方式获取输入行的每个字符

该程序从输入文件中删除感叹号;如果需要,此点可能是固定的

编辑:修改为回复评论的方法

我修改了原始方法,因此它现在可以管理几乎所有的特殊字符(引号除外):

第二次编辑:我根据dbenham的建议进一步修改了该方法,现在它可以管理所有字符

input.txt:

1 7<Y>IO
QU|C"K&7
;T Y!9^0
1   7 < Y > I O
Q U | C " K & 7
; T   Y ! 9 ^ 0

这是可以做到的。我们将使用一个FOR循环,其中嵌套一个子例程。 读取行,子例程使用
LineSpacer
将行隔开

@echo OFF

REM - Set your source and destination files.
Set SourceFilePath=C:\Users\Admin\Desktop\SourceFile.txt
Set OutputFilePath=C:\Users\Admin\Desktop\OutputFile.txt

REM Blank out the OutputFile
TYPE nul > %OutputFilePath%

REM For every line in the source text file call linespacer subroutine.
FOR /F "tokens=*" %%A IN (%SourceFilePath%) DO (
    SET line=%%A
    SETLOCAL EnableDelayedExpansion
    SET spacedline=
    CALL :LineSpacer
    ECHO !spacedline! >> %OutputFilePath%
    ENDLOCAL    
)

REM - Get the first character of the line, add a space to it
REM - Reset the line variable to one less character, repeat. 
:LineSpacer
    IF DEFINED line (
       SET char=!line:~0,1!
       SET "charpluspace=!char! "
       SET spacedline=!spacedline!!charpluspace!
       SET "line=!line:~1!"
       IF "%line%" EQU "!line:~1!" EXIT /b
       GOTO linespacer
    ) 
EXIT /b

上面的批处理文件将满足您的需要。为了更好地理解它是如何完成的,请尝试我发布的两个链接中的一些示例。

这是可以做到的。我们将使用一个FOR循环,其中嵌套一个子例程。 读取行,子例程使用
LineSpacer
将行隔开

@echo OFF

REM - Set your source and destination files.
Set SourceFilePath=C:\Users\Admin\Desktop\SourceFile.txt
Set OutputFilePath=C:\Users\Admin\Desktop\OutputFile.txt

REM Blank out the OutputFile
TYPE nul > %OutputFilePath%

REM For every line in the source text file call linespacer subroutine.
FOR /F "tokens=*" %%A IN (%SourceFilePath%) DO (
    SET line=%%A
    SETLOCAL EnableDelayedExpansion
    SET spacedline=
    CALL :LineSpacer
    ECHO !spacedline! >> %OutputFilePath%
    ENDLOCAL    
)

REM - Get the first character of the line, add a space to it
REM - Reset the line variable to one less character, repeat. 
:LineSpacer
    IF DEFINED line (
       SET char=!line:~0,1!
       SET "charpluspace=!char! "
       SET spacedline=!spacedline!!charpluspace!
       SET "line=!line:~1!"
       IF "%line%" EQU "!line:~1!" EXIT /b
       GOTO linespacer
    ) 
EXIT /b
上面的批处理文件将满足您的需要。为了更好地理解它是如何完成的,请尝试我发布的两个链接中的一些示例。

我会使用my,因为该解决方案比任何纯批处理解决方案更简单、更快(忽略实用程序iteslf的复杂性)

写入屏幕

jrepl "(?!^|$)" " " /f file.txt
写入新文件

jrepl "(?!^|$)" " " /f file.txt /o new.txt
改写原文

jrepl "(?!^|$)" " " /f file.txt /o -
但是,如果您想要一个纯批处理解决方案来替换原始文件,那么我认为这是您可以获得的最快速度,并且仍然支持除null以外的所有字符,保留空白,并支持最大长度为4090行(插入空格后为8180行)。逻辑是非常直接的,一旦你添加了。但是你可以有效地使用:strlen,即使你不了解它是如何工作的

@echo off
setlocal disableDelayedExpansion
set "file=%~1"

>"%file%.new" (
  for /f "delims=" %%A in ('findstr /n "^" "%file%"') do (
    set "in=%%A"
    setlocal enableDelayedExpansion
    set "in=!in:*:=!"
    call :strlen in len
    set /a len-=1
    set "out="
    for /l %%N in (0 1 !len!) do set "out=!out! !in:~%%N,1!"
    if defined out (echo(!out:~1!) else echo(
    endlocal
  )
)
move /y "%file%.new" "%file%" >nul
exit /b

:strlen <stringVar> <resultVar>
(
  setlocal EnableDelayedExpansion
  set "s=!%~1!#"
  set "len=0"
  for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if "!s:~%%P,1!" NEQ "" (
      set /a "len+=%%P"
      set "s=!s:~%%P!"
    )
  )
)
(
  endlocal
  set "%~2=%len%"
  exit /b
)
@echo关闭
setlocal disableDelayedExpansion
设置“文件=%~1”
>%file%.new(
对于/f“delims=“%%A in('findstr/n“^”“%file%”)do(
设置“in=%%A”
setlocal enableDelayedExpansion
设置“in=!in::=!”
电话:l区斯特伦