-1

i have a batch file to add shortcut application in startup menu , this batch file work finely but i don't get the access under a specific file to add it.

Here the code :

@echo off
  xcopy /y "C:\_MIT\Phoenix\PhoenixLoader.lnk" "%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs"

I want to add the shortcut "PhoenixLoader.lnk" in the " C:\ProgramData\startlayout\uppc.xml " file uppc.xml file code :

<LayoutModificationTemplate 
xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" 
xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" 
xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
<LayoutOptions StartTileGroupCellWidth="6" />
 <DefaultLayoutOverride>
<StartLayoutCollection>
  <defaultlayout:StartLayout GroupCellWidth="6">
    <start:Group Name="UPPC">
      <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\App1.lnk" />
      <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\App2.lnk" />
      <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" 
     </start:Group>
  </defaultlayout:StartLayout>
</StartLayoutCollection>

I have to access the file uppc.xml and add " PhoenixLoader.lnk " in the first line of the code instead of app1.lnk :

 DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start 
 Menu\Programs\PhoenixLoader.lnk"

1 Answers1

0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q74147544.txt"
SET "outfile=%destdir%\outfile.txt"

SET "replace=app1.lnk"
SET "replacement=PhoenixLoader.lnk"

(
FOR /f "usebackqtokens=1*delims=" %%b IN ("%filename1%") DO (
 SET "line=%%b"
 ECHO !line:%replace%=%replacement%!
)
)>"%outfile%"

FC "%filename1%" "%outfile%"

GOTO :EOF

Always verify against a test directory before applying to real data.

Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.

I tested with a file called q74147544.txt containing a copy of the uppc.xml file you posted.

The resultant file in outfile.txt was :

<LayoutModificationTemplate 
xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout" 
xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout" Version="1" 
xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification">
<LayoutOptions StartTileGroupCellWidth="6" />
 <DefaultLayoutOverride>
<StartLayoutCollection>
  <defaultlayout:StartLayout GroupCellWidth="6">
    <start:Group Name="UPPC">
      <start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\PhoenixLoader.lnk" />
      <start:DesktopApplicationTile Size="2x2" Column="2" Row="0" DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\App2.lnk" />
      <start:DesktopApplicationTile Size="2x2" Column="4" Row="0" 
     </start:Group>
  </defaultlayout:StartLayout>
</StartLayoutCollection>

You would need to move this file as appropriate for your installation, but the file used as the source file must be different from the file used as the destination file.

Essentially, read each line of the source ino %%b, then move %%b to a user-variable (line) for manipulation as string-substitution cannot be applied to %%b.

The string substitution is to substitute the replacement string for the replace string. Since the value of line is being changed within the parenthesised code block, you require delayedexpansion - see Stephan's DELAYEDEXPANSION link

The substitution semantics are described in the set help, accessible by executing set /? from the prompt.

Magoo
  • 77,302
  • 8
  • 62
  • 84