从过去2天开始处理批处理文件但没有运气:(
我需要一个脚本(Bat),它将删除我的构建列表(xml)中未提及的所有项目(主要是vb)。
例如:我有一个名为C:\ 123的文件夹,里面有大约15个文件。 我有一个构建列表(XML文件),它有10个文件名,在C:\ 123中。现在我想要一个批处理文件脚本,它将删除C:\ 123中的其余5个文件,这些文件不在xml文件中。
任何帮助将不胜感激! 脚本试图比较2个文件夹并首先删除相同的文件(考虑稍后根据我的需要调整它但这不起作用)
@ECHO OFF
SET LOCALFOLDER=C:\123
SET OTHERFOLDER=D:\123
Pause
:LOCALKEYTEMP
SET FILE=DONE
:: THIS LINE SCANS THE LOCAL FOLDER FOR FILES,
:: WE CAN USE THIS TO SCAN SEPERATE FILES ONE AT A
TIME
FOR /F "TOKENS=*" %%G IN ('DIR/B ^"%LOCALFOLDER%
\*.*^"') DO SET FILE=%%G
Pause
ECHO %FILE%
pause
IF %FILE%==DONE GOTO END
pause
ECHO N|COMP "%LOCALFOLDER%\%FILE%"
"%OTHERFOLDER%\%FILE%" | FIND "FILES COMPARE OK" >
NUL
pause
IF ERRORLEVEL 1 GOTO DIFFERENTKEYS
IF ERRORLEVEL 0 GOTO DELETEBOTH
pause
DELETEBOTH
DEL /Q "%LOCALFOLDER%\%FILE%"
DEL /Q "%OTHERFOLDER%\%FILE%"
GOTO LOCALKEYTEMP
DIFFERENTKEYS
:: THIS LINE DELETES THE LOCAL FOLDERS FILES WHICH IS
NECCESSARY FOR THIS SCRIPT
DEL /Q "%LOCALFOLDER%\%FILE%"
GOTO LOCALKEYTEMP
:END
ECHO ALL FILES SHOULD BE DELETED FROM
%LOCALFOLDER%
pause
ECHO ALL DIFFERENT FILES SHOULD BE LEFT ON
%OTHERFOLDER%
PAUSE
EXIT
xml文件如下所示:
<ProjectsToBuild>
<Project>C:\123\Clients\Direct\App1.vbproj</Project>
<Project>C:\123\Clients\Direct\App2.vbproj</Project>
</ProjectsToBuild>
Aacini批处理脚本的结果:
Press any key to continue . . .
List of existent files:
fileName[D:\123\Subfolder1\a1.txt.txt]=1
fileName[D:\123\Subfolder1\a2.txt.txt]=1
fileName[D:\123\Subfolder1\a3.txt.txt]=1
fileName[D:\123\Subfolder1\a4.txt.txt]=1
fileName[D:\123\Subfolder1\buildList.xml]=1
fileName[D:\123\Subfolder2\a1.txt.txt]=1
fileName[D:\123\Subfolder2\a2.txt.txt]=1
fileName[D:\123\Subfolder2\a3.txt.txt]=1
fileName[D:\123\Subfolder2\a4.txt.txt]=1
fileName[D:\123\Subfolder2\buildList.xml]=1
Press any key to continue . . .
Keep these files:
Press any key to continue . . .
Remove these files:
Press any key to continue . . .
Press any key to continue . . .
这会删除文件夹中的所有文件。
答案 0 :(得分:1)
对不起。在您的问题中,您说“我有一个名为C:\ 123的文件夹,其中包含大约15个文件 ”;但是,构建列表只有两个名称,位于C:\ 123下面的文件夹两个级别。实际上,C:\ 123文件夹中没有构建列表中提到的任何文件。我假设您要删除构建列表中未提及的任何级别的C:\ 123 中的所有文件(因为构建列表提到C:\ 123下面两级的文件)。如果这不是您想要的,则必须修改批处理文件。
@echo off
setlocal
rem Create a list of existent file names in C:\123 *at any level*
for /R "C:\123" %%a in (*.*) do set "fileName[%%a]=1"
ECHO List of existent files:
SET fileName[
ECHO/
rem Process the build list and remove found names from existent files list
ECHO Keep these files:
for /F "tokens=2-4 delims=<>" %%a in (buildList.xml) do (
if "%%a" == "Project" if "%%c" == "/Project" (
ECHO fileName[%%b]
set "fileName[%%b]="
)
)
ECHO/
ECHO Remove these files:
rem Remove the remaining files
for /F "tokens=2 delims=[]" %%a in ('set fileName[') do (
ECHO del "%%a"
)