相关文章推荐
买醉的篮球  ·  Adding an Owl ...·  1 年前    · 
急躁的数据线  ·  python - How can mypy ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to save %USERPROFILE% as a string read from regedit to a text file,but the cmd changes to "C:\Users\Administrator",does anybody know how to fix it? What I want is saves like this :

"%USERPROFILE%\AppData\Local\Microsoft" in a.txt

"C:\Users\Administrator\AppData\Local\Microsoft" in a.txt

The Code:

echo %USERPROFILE%\\AppData\\Local\\Microsoft >> c:\a.txt
                % is a deference operator for variable so you have to prevent the % it self. Try this: "%% you variable %%" . may be works.
– Shakiba Moshiri
                Jan 29, 2017 at 14:15
                Also as you didn't answer the question referencing regedit, I can only assume you're writing the output for use as input to a .reg file. Are you aware that if you use reg.exe you can add the registry entry directly with the normal path, there will be no need to double up the back slashes for instance.
– Compo
                Jan 30, 2017 at 14:41

You double up the percents to prevent the expansion:

Echo="%%LocalAppData%%\Microsoft">a.txt

Also, notice the changed environment variable, which defines the location you were looking for.

I just want to get the regedit string value and then save it to a text via cmd,and want it keep as the same as regedit string. – momo Jan 29, 2017 at 14:28 There was nothing at all about regedit in your question. Please update your original question with the real task and details, so that we can provide a more specific response. – Compo Jan 29, 2017 at 15:10

Update Solution

Finally,I find a solution myself,thanks for all kindly help,the codes here:

Set _a=%
Set _b=USERPROFILE
Echo %_a%%_b%%_a%\\AppData\\Local\\Microsoft >> c:\a.txt

There will be works correctly in any machine,hope this can help someone who needed it like me.

In general, precede an awkward character like &<> etc., with a caret ^ to use echo to show it.

Naturally, % is the exception. You need to escape % with % thus : %%

Thanks for help,you meant:echo %%USERPROFILE%%\\AppData\\Local\\Microsoft >> c:\a.txt?Am I wrong?the output text is %C:\Users\Administrator%\\AppData\\Local\\Microsoft – momo Jan 29, 2017 at 14:21 echo %%USERPROFILE%%\\AppData\\Local\\Microsoft >> c:\a.txt produces %USERPROFILE%\\AppData\\Local\\Microsoft in the file on my machine... – Magoo Jan 29, 2017 at 14:31

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.