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,我试图用.bat编辑输入到文本文件中的引号 echo %name%>./User_Records/%username%.txt 在文本文件中,它另存为 "Firstname Lastname" 我试图添加到批处理文件中,以便它编辑*.txt文件,并删除保存在该文本文件中的引号 有人能帮我吗 我已经试着这么做了好几个星期了。我希望输出像这样 Firstname Lastname 尝试使用环境变量替换来替换%name%变量中“的所有实例(有关详细信息,请参见设置/?) 如果您试图在保存文

我试图用.bat编辑输入到文本文件中的引号

echo %name%>./User_Records/%username%.txt
在文本文件中,它另存为

"Firstname Lastname"
我试图添加到批处理文件中,以便它编辑*.txt文件,并删除保存在该文本文件中的引号

有人能帮我吗

我已经试着这么做了好几个星期了。我希望输出像这样

Firstname Lastname

尝试使用
环境变量替换来替换
%name%
变量中
的所有实例(有关详细信息,请参见
设置/?

如果您试图在保存文本文件后替换引号,请参阅此

如果你的问题是这样的话,你应该在录音前去掉这些引语

请注意,windows中的路径分隔符是
\
而不是
/


但是-如果您的问题是关于已经存在的文件,那么最简单的方法可能是使用编辑器。这在一定程度上取决于您必须处理的文件数量-您没有指定这些文件。

如果从文本文件读取的名称使用
set name=“。。。“
语法,则这是输出字符串中出现双引号的原因

如果将分配给环境变量的字符串用双引号括起来,或者将commandSET的整个参数字符串括起来,则会产生很大的差异。阅读下面的答案,了解使用
set“variable=string”
set variable=“string”
之间的巨大区别

但当然可以使用字符串替换从当前分配给环境变量
name
的字符串中删除所有双引号:

set "name=%name:"=%"
名称
字符串中出现的所有
都将替换为空字符串,生成的字符串将再次分配给环境变量
名称

但是请注意,这个命令行

echo %name%>"./User_Records/%username%.txt"
可能会导致不必要的行为,例如,如果分配给环境变量
name
的名称是
C&A
。Windows命令解释器在展开环境变量
name
后和执行命令ECHO之前找到的符号现在被解释为and运算符,而不再是and运算符s要通过回声输出的文字字符

一种解决方案是使用延迟扩展,例如:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Environment variable name is defined with delayed expansion disabled
rem making it possible to assign an exclamation mark as literal character
rem without the need to escape it as it would be necessary when delayed
rem expansion would be enabled already here at this time.

set "name=C&A!"

rem Enable delayed expansion which results in pushing on stack current
rem state of command extensions and of delayed expansion, the current
rem directory path and the pointer to current environment variables list
rem before creating a copy of all environment variables being used further.

setlocal EnableDelayedExpansion

rem Output the value of environment variable name using delayed expansion
rem with redirection into a text file being named like the currently used
rem user account name which of course can contain a space character or
rem other characters requiring enclosed file name with relative path in
rem double quotes.

echo !name!>".\User_Records\%username%.txt"

rem Delete all environment variables, restore pointer to previous set of
rem environment variables, restore current working directory from stack
rem restore states of command extension and delayed expansion from stack.
endlocal

rem Explicitly call ENDLOCAL once again for initial SETLOCAL command.
rem That would not be necessary because Windows command interpreter
rem runs implicitly ENDLOCAL for each local environment still being
rem pushed on stack.

endlocal
另一种解决方案是使用命令进行隐式延迟扩展:

@echo off
set "name=C&A!"
set "name=%name:"=%"
for /F "delims=" %%I in ("%name%") do echo %%I>".\User_Records\%username%.txt"
set "name=
另请参见上的答案

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

  • echo/?
  • endlocal/?
  • 获取/?
  • 设置/?
  • setlocal/?

另外,请阅读有关Microsoft的文章。

您正在使用的bat文件确实应该更改,特别是您从中提供的行有一些问题(大部分已经提到)

SetLocal EnableDelayedExpansion
(Echo=!name:“=!”>“用户记录\%username%.txt”
端部
如果您无法控制该bat文件,那么类似的操作应该可以满足您的需要:

@For/F“UseBackQ Delims=“%A In”(“User\u Records\%username%.txt”)Do@Echo(%%~A
或者:

@Set/P“FullName=”这些答案中有没有?
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Environment variable name is defined with delayed expansion disabled
rem making it possible to assign an exclamation mark as literal character
rem without the need to escape it as it would be necessary when delayed
rem expansion would be enabled already here at this time.

set "name=C&A!"

rem Enable delayed expansion which results in pushing on stack current
rem state of command extensions and of delayed expansion, the current
rem directory path and the pointer to current environment variables list
rem before creating a copy of all environment variables being used further.

setlocal EnableDelayedExpansion

rem Output the value of environment variable name using delayed expansion
rem with redirection into a text file being named like the currently used
rem user account name which of course can contain a space character or
rem other characters requiring enclosed file name with relative path in
rem double quotes.

echo !name!>".\User_Records\%username%.txt"

rem Delete all environment variables, restore pointer to previous set of
rem environment variables, restore current working directory from stack
rem restore states of command extension and delayed expansion from stack.
endlocal

rem Explicitly call ENDLOCAL once again for initial SETLOCAL command.
rem That would not be necessary because Windows command interpreter
rem runs implicitly ENDLOCAL for each local environment still being
rem pushed on stack.

endlocal
@echo off
set "name=C&A!"
set "name=%name:"=%"
for /F "delims=" %%I in ("%name%") do echo %%I>".\User_Records\%username%.txt"
set "name=