Link to home
Start Free TrialLog in
Avatar of noodleNT
noodleNT

asked on

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


:ProcessAddress
If not "%1"=="" (
      Set APPdir=%APPdir% %APPdir
)


But this doesn't seem to work correctly. Anyone know the correct way to do this?
ASKER CERTIFIED SOLUTION
Avatar of mrdtn
mrdtn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrdtn
mrdtn

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.

--

mrdtn
Avatar of noodleNT

ASKER

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!
noodleNT,

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.

--

mrdtn
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!!

--

mrdtn
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!

noodleNT
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.

--

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

Example:

     for /f "delims=" tokens=1,*" %%f in ("%localappdata%") Do (
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:

https://www.experts-exchange.com/questions/20763220/Remove-all-network-printers-with-a-batch-file.html#9530746
Courtesy of Billious,

https://www.experts-exchange.com/questions/20568784/Can-FOR-command-use-double-quote-as-a-delimeter.html

--

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%

--

mrdtn
Woops, that's:

@echo off
set appdir=%appdata:\=\\%
echo %appdir%
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?
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

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


:Start
ECHO READING OLD SERVER.
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 READING NEW SERVER
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


:Action
set /a LineCount+=1
set PrinterName=

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


:DoubleCheck
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
)


: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


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


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


:Exit



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