Solved

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

Posted on 2003-10-29
15
617 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:noodleNT
  • 8
  • 7
15 Comments
 
LVL 6

Accepted Solution

by:
mrdtn earned 500 total points
ID: 9646903
This will do what you are trying to do:

--

mrdtn

:Begin batch
@echo off
setlocal

set localappdata=%appdata%

:loop
      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%

pause

goto :eof

0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9646915
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
0
 

Author Comment

by:noodleNT
ID: 9647321
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!
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9647334
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
0
 

Author Comment

by:noodleNT
ID: 9647344
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! =)
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9647350
Mail server seems to be slow tonight -- Thanks and glad to help as usual!!

--

mrdtn
0
 

Author Comment

by:noodleNT
ID: 9647409
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 6

Expert Comment

by:mrdtn
ID: 9647458
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
0
 

Author Comment

by:noodleNT
ID: 9652978
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 (
0
 

Author Comment

by:noodleNT
ID: 9653001
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:

http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20763220.html#9530746
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9653761
Courtesy of Billious,

http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20568784.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 (
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9653819
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
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9653824
Woops, that's:

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

Author Comment

by:noodleNT
ID: 9661039
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?
0
 

Author Comment

by:noodleNT
ID: 9661358
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 -------------->
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now