Format %APPDATA% value so it can be used as a registry value

I am making a custom config batch file to for setting up user computers. I need to pull the users %APPDATA% directory and put it into a Regkey value. I figure I need to use something like this to add the extra "\" in the address so that it will work in the registry:

for /f "delims=\" %%f in ("%APPDATA%") Do call :ProcessAddress

If not "%1"=="" (
      Set APPdir=%APPdir% %APPdir

But this doesn't seem to work correctly. Anyone know the correct way to do this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This will do what you are trying to do:



:Begin batch
@echo off

set localappdata=%appdata%

      for /f "delims=\ tokens=1,*" %%f in ("%localappdata%") Do (
            set localtoken=%%f
            set localappdata=%%g
      if "%appdir%"=="" (
            set appdir=%localtoken%
      ) else (
            set appdir=%appdir%\\%localtoken%
if not "%localappdata%"=="" goto loop

echo %appdir%


goto :eof

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Trick here is the use of the asterisk.  See excerpt from help cmd below:

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.


noodleNTAuthor Commented:
I figured it out after a short break.

For anyone looking to do this here is the code for APPDATA and TEMP.

for /f "tokens=1-4* delims=\" %%1 in ("%APPDATA%") do Set APPdir=%%1\\%%2\\%%3\\%%4

for /f "tokens=1-5* delims=\" %%1 in ("%TEMP%") do Set TEMPdir=%%1\\%%2\\%%3\\%%4\\%%5

Thanks for the help!
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.


You will find that my solution is independent of the number of layers in the directory nesting -- in the event a system -- or a user profile has been configured uniquely.


noodleNTAuthor Commented:
WHAO.... How did I miss your first post????
Damn brain must be fritzen after looking at this code for so long!

Your right! Your way is the better way to do this! Thanks!

You get the points again my friend! =)
Mail server seems to be slow tonight -- Thanks and glad to help as usual!!


noodleNTAuthor Commented:
Just FYI mrdtn...

With your help I have put together a batch file that configures the Outlook Account, All OfficeXP settings, Word templates, FTP Program, Quick Launch bar, Start Button settings, Netmeeting, Some Power settigns, Track Point and Mouse settings, VPN Security Policy, Installs IM 5.0 and adds all Company Contacts, Network Drives, and Network Printers. All I have to do is click the batch file and type in what office the new user will be working from.  WHAT A TIME SAVER and any "Retared Monkey" can do it!

The only things I need to figure out now are how the Custom Toolbar Buttons in Outlook are saved along with setting up Send/Recieve Settings and a way for all the Power to be saved. Sooo Close!

Thanks again for your help!

Hey Noodle,

Glad to have helped!  

With regard to your question in the most recent post,

Hmmm food for thought . . no easy or immediate answer (post a q in the VB area perhaps?) . .

probably end up including a call to a vb script or something.  Unfortunately, I have no quick answer.  Glad to help you with some part of it.  Thanks for the points.


noodleNTAuthor Commented:
If I want to search for the Delim "   Do I need to put it in single quotes?


     for /f "delims=" tokens=1,*" %%f in ("%localappdata%") Do (
noodleNTAuthor Commented:
BTW... I need to use this to clear out the quotes where network share comments have quotes in them for that Network Resources script here:
Courtesy of Billious,


You would modify what you have with

set locappdata=%locappdata:"=,%
for /f "delims=, tokens=1,*" %%f in ("%localappdata%") Do (

as per the suggestion in the link provided.  This is assuming the comma works as a delimeter for you.  If there are possible commas in the comments, perhaps using single quotes:

set locappdata=%locappdata:"='%
for /f "delims=' tokens=1,*" %%f in ("%localappdata%") Do (
You know, also come to think of it, the solution I posted to your original question is way more complicated than it had to be:

@echo off
set appdir=%appdir:\=\\%
echo %appdir%


Woops, that's:

@echo off
set appdir=%appdata:\=\\%
echo %appdir%
noodleNTAuthor Commented:
HEHE..... yeah I thought about that after seeing that command. :) But using the IF statement is fun!!

well here is the problem I run into... I think it is related to the sloppy If statement I used to parse out the Printer names in the Net View results. Here is  the some of the script...

for /F "skip=%SkipLines% tokens=1-8*" %%a in ('net view "\\%Oldsvrname%"') do call :Verify %%a %%b %%c %%d %%e %%f %%g %%h

Then it runs through this If statement to create and check for the printer names....

Set V1=%1
Set V2=%2
Set V3=%3
Set V4=%4
Set V5=%5
Set V6=%6
Set V7=%7
Set V8=%8

If "%V1%"=="Print" (
      If "%V2%"=="Print" Set Printername=%Printername% %V1%
      Set Process=Process
      Goto Header
) ELSE (
      If not .%V1%.==.. Set Printername=%V1%

If "%V2%"=="Print" (
      Set Printername=%V1%
      If "%V3%"=="Print" Set Printername=%Printername% %V2%
      Set Process=Process
      Goto Header
) ELSE (
      If not .%V2%.==.. Set Printername=%Printername% %V2%

......... you get the point. Now the problem with throughing that :"=*% in there is that if the field is blank it then makes the field equal "=% which causes an error because of the single quote. So if I search for the blank before I run the removal quote script I get another syntax error because the script contains double quotes. AHHH you just get stuck in the spiral. Any Idease?
noodleNTAuthor Commented:
Never Mind!

I redid the code using the shift methode that someone posted awhile back. This seems to fix the problem with quotes. Here is the solution.
The DoubleCheck Option is only for one printer on the network where the share name was "Office Print" instead of "Office Printer". You can remove that option if you wish. This code is MUCH cleaner.

<---------------- Copy --------------->

SET Process=Start
:: *** share type "delimiter":
set ShareType=PRINT
:: *** Number of useless lines at the beginning of the "net view" command:
set SkipLines=7

ECHO *********************************
ECHO ***** ACAS Network Resources ****
ECHO ******    Version 5.2      ******
ECHO ******   By Nick Thoman    ******
ECHO *********************************
ECHO !!!!!!!! DO NOT CLOSE !!!!!!!!!!
ECHO ___________________________________________________________________________
Goto %Process%

ECHO Please wait...
set LineCount=0
set Option=delete
for /f "skip=%SkipLines% delims=" %%a in ('net view "\\%OldSvrName%"') do call :Action %%a %%b

ECHO Please wait...
set LineCount=0
set Option=add
for /f "skip=%SkipLines% delims=" %%a in ('net view "\\%NewSvrName%"') do call :Action %%a %%b
Set Process=Drive
goto Header

set /a LineCount+=1
set PrinterName=

if .%1.==.. goto NotFound
if /i %1==%ShareType% Goto DoubleCheck
set PrinterName=%PrinterName% %1
goto processloop

Echo Double Checking for bad printer name
If /i %2==%ShareType% (
      Echo Fixing Bad Printer Name.
      Set PrinterName=%PrinterName% %1
      goto Found
) Else (
      Echo Printer Name is Fine.
      goto Found

set PrinterName=%PrinterName:~1%
echo Line %LineCount%: Printer "%PrinterName%" found.
if %Option%==delete (
 echo Removing Printer "\\%OldSvrName%\%PrinterName%"
 rundll32 printui.dll,PrintUIEntry /dn /q /n"\\%OldSvrName%\%PrinterName%"
) else (
 echo Mapping Printer "\\%NewSvrName%\%PrinterName%"
 rundll32 printui.dll,PrintUIEntry /in /q /n"\\%NewSvrName%\%PrinterName%"
Set Process=Exit
Goto Header

echo Line %LineCount%: No print share found.
goto Exit

NET USE Z: /Delete
NET USE Z: \\%NewSvrName%\%sharename%


<------------- End Copy -------------->
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.