[英]Trouble creating a batch script to pull lines from an XML file
因此,我一直在尝试编写一个批处理脚本,该脚本将通过AndroidManifest.xml并提取应用程序中使用的权限,然后将其输出到.txt文件进行工作,而不必手动执行。 我的一部分工作是在开发人员的身后,对应用程序中使用的所有权限进行分类,以确保它们得到了实际使用,而不仅仅是无用的代码,因此拥有一系列权限跳转就可以使x100速度更快。
例如,在xml中有如下几行:
<uses-permission android:name="android.permission.VIBRATE"/>
我正在尝试将它们添加到输出文件中,如下所示:
android.permission.VIBRATE
到.txt文件。
当前,我在输出中获得正确的行数,但获得的不是许可:
!line!
我从在线示例和教程中编写的有关处理XML文件的类似内容的代码如下:
ECHO off
REM new line added to fix output below thanks to Compo
SETLOCAL enabledelayedexpansion
:start
ECHO Note: Find your list of permissions used in the permissions output file
ECHO Please input the file path to the AndroidManifest you'd like scrapped for permissions
SET /p manifestpath=
ECHO Verifying inputted path:
ECHO %manifestpath%
CD "%manifestpath%"
(FOR /f "usebackqtokens=1-7delims=<=>/ " %%a IN (AndroidManifest.xml) DO (
IF /i "%%~a%%~b"=="uses-permissionandroid:name" ECHO "%%~c"
)) > permissionsoutput.txt
START permissionsoutput.txt
ECHO to run again enter 'r'
ECHO to exit enter 'e'
SET /p nextaction=
if %nextaction%==r goto :start
if %nextaction%==e :eof
更新:太好了,它实际上返回的是!line以外的东西! 现在。 但是我需要怎么做才能将其从其当前输出中获取权限,例如:android:name =“ android.permission.WRITE_EXTERNAL_STORAGE” />到类似android.permission.WRITE_EXTERNAL_STORAGE的权限
更新:完全正常! 上面发布的代码对于需要它作为其他项目参考的任何人都应该可以正常使用。
@echo off
,插入新行
setlocal enabledelayedexpansion
是导致!var!
delayedexpansion
模式!var!
成为var
的当前值
要从发布的行中检索android.permission.WRITE_EXTERNAL_STORAGE
(请将实际的行编辑为您的原始问题-使用XML进行注释)
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q45656376.txt"
FOR /f "usebackqtokens=1-7delims=<=>/ " %%a IN ("%filename1%") DO (
IF /i "%%~a%%~b"=="uses-permissionandroid:name" ECHO "%%~c"
)
GOTO :EOF
为了方便起见,在我的系统上构造filename1
是包含一些随机XML,包括您的行。
读取文件,将<=> <=>/
作为分隔符; 选择第三个并删除引号。
此代码旨在演示如何从文件中提取所需的数据。 前4行只是设置了源文件名>我使用此结构进行测试,以便在出现任何问题时可以轻松地修改代码-并将与问题相关的所有数据保存在一组相关的文件中(因此, 我的批处理在这种情况下将是q45656376.bat
而任何相关的数据文件是q45656376.txt
等)
最后的goto :eof
是多余的代码-我习惯于减少将子例程添加到任何批处理中的问题。
因此,剩下的只是一个for
语句。 该语句应替换您例程中已有的for/f
。 然后,您需要更改以"AndroidManifest.xml"
目标的文件名,并删除%%~c
〜c周围的引号(如果您愿意)-我使用引号直观地验证了没有多余的空格正在输出。
请注意,您的for
语句is parenthesised
(for ...)> filename . This is used to redirect the
. This is used to redirect the
echo ed output from the
for . This is used to redirect the
to a file instead of to the console. You'd need to parenthesise the
to a file instead of to the console. You'd need to parenthesise the
相同的方式to a file instead of to the console. You'd need to parenthesise the
for`语句后加上to a file instead of to the console. You'd need to parenthesise the
以产生输出文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.