2017-04-02 148 views
-3

我想编辑出已inputed到使用的.bat的文本文件引号。如何使用批处理文件从文本文件中删除引号?

echo %name%>./User_Records/%username%.txt 
在文本文件

它保存为

"Firstname Lastname" 

我试图添加到批处理文件,以便将编辑* .txt文件,并删除引号,如果它们被保存在那个文本文件中。

任何人都可以帮助我吗?

我一直在尝试了几个星期做到这一点。我想输出看起来像

Firstname Lastname 
+0

回答这些问题? http://stackoverflow.com/questions/5328131/removing-double-quotes-in-a-batch-program-in-windows-2003 –

回答

1

尝试使用Environment variable substitution更换的"所有实例%name%变量(参见set /?更多)

@echo off 
set "name=%name:"=%" 
echo %name%>./User_Records/%username%.txt 

如果你想更换引号后文本文件已保存,则参考此previous question

0
echo %name:"=%>.\User_Records\%username%.txt 

应该在记录之前去掉引号,如果这就是你的问题所在。

请注意在Windows路径分隔符是\/

但是 - 如果你的问题是关于已经存在,那么文件可能是最简单的方法是使用你的编辑器。取决于需要处理的文件数量 - 您还没有指定。

0

如果从文本文件中读取的名称使用set name="..."语法分配到环境变量名,那么这就是在输出字符串中的双引号的原因。

如果将分配给环境变量的字符串用双引号括起来,或者使用命令SET的整个参数字符串,它将产生很大的差异。阅读关于Why is no string output with 'echo %var%' after using 'set var = text' on command line?答案,以获取有关使用set "variable=string"set variable="string"相差很大知识。

但是,当然也可以通过使用一个字符串替换删除从当前分配给环境变量name字符串的所有双引号:

set "name=%name:"=%" 

name"所有出现由一个空字符串替换并将结果字符串再次分配给环境变量name

但要注意,此命令行

echo %name%>"./User_Records/%username%.txt" 

可能导致不必要的行为,例如,如果分配到环境变量name名称是C&A。在扩展环境变量name之后以及执行命令ECHO之后,Windows命令解释程序找到的&符号现在被解释为AND运算符,而不再是作为文字字符的输出ECHO

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

@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 

另一种解决方案是使用命令FOR为隐式延迟扩展:上Batch: Auto escape special characters

@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 /?
  • for /?
  • set /?
  • setlocal /?

阅读也是微软文章关于Using Command Redirection Operators

0

您正在使用的bat文件应该被改变,特别是随着行你从它提供了一些问题,(大多已经提到)

SetLocal EnableDelayedExpansion 
(Echo=!name:"=!)>"User_Records\%username%.txt" 
EndLocal 

如果你有超过该bat文件没有控制那么这样的事情应该做你想要什么:

@For /F "UseBackQ Delims=" %%A In ("User_Records\%username%.txt") Do @Echo(%%~A 

或者:

@Set/P "FullName="<"User_Records\%username%.txt" 
@Echo(%FullName:"=% 
相关问题