-3

I am trying to edit out the quotation marks that have been inputed into a text file using .bat.

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

in the text file it is saving as

"Firstname Lastname"

I am trying to add to the batch file so that it will edit the *.txt file and delete the quotation marks if they are saved in that text file.

Can anyone help me?

I have been trying to do this for weeks. I want the output to look like

Firstname Lastname
Mofi
  • 46,139
  • 17
  • 80
  • 143

4 Answers4

1

Try replacing all instances of " in the %name% variable by using Environment variable substitution (see set /? for more)

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

If you are trying to replace the quotation marks after the text file has been saved, then refer to this previous question

Sam Denty
  • 3,693
  • 3
  • 30
  • 43
0
echo %name:"=%>.\User_Records\%username%.txt

should strip the quotes before they are recorded, if that's what your question is.

Note that path-separators in windows are \ not /.

But - if your question is about files that already exist then probably the easiest way is to use your editor. Depends a little on quite how many files you have to process - which you haven't specified.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

If the name read from text file is assigned to environment variable name using set name="..." syntax, then this is the cause of the double quotes in output string.

It makes a big difference if string assigned to environment variable is enclosed in double quotes or the entire parameter string of command SET. Read answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? to get knowledge about the big difference between using set "variable=string" versus set variable="string".

But it is of course possible to remove all double quotes from string assigned currently to environment variable name by using a string substitution:

set "name=%name:"=%"

All occurrences of " in string of name are replaced by an empty string and resulting string is assigned again to environment variable name.

But be aware that this command line

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

could result in an unwanted behavior, for example if the name assigned to environment variable name is C&A. The ampersand found by Windows command interpreter after expanding environment variable name and before execution of command ECHO is interpreted now as AND operator and not anymore as literal character to output by ECHO.

One solution is using delayed expansion, for example:

@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

An alternate solution is using command FOR for an implicit delayed expansion:

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

See also the answers on Batch: Auto escape special characters.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?

Read also the Microsoft article about Using Command Redirection Operators.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

The bat file you are using should really be altered, especially as the line you've provided from it has some issues, (mostly already mentioned).

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

If you have no control over that bat file then something like this should do what you want:

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

Alternatively:

@Set/P "FullName="<"User_Records\%username%.txt"
@Echo(%FullName:"=%
Compo
  • 36,585
  • 5
  • 27
  • 39