通过读取列表文件打印文件列表在检查读取路径的实际存在时不起作用
Printing list of files by reading a list-file not working when checked for actual existence of read paths
正如我在 Q-title 中提到的,我正在尝试从列表文件中读取一些我打算稍后删除的文件路径,该列表文件包含由 newline
分隔的这些路径,如下所示[ChromeJunks.lst]:
%LOCALAPPDATA%\Google\Chrome\User Data\Default\History
%LOCALAPPDATA%\Google\Chrome\User Data\Default\History-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Network Action Predictor
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Network Action Predictor-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Top Sites
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Top Sites-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Visited Links
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Favicons
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Favicons-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Web Data
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Web Data-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Log
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Log.old
%LOCALAPPDATA%\Google\Chrome\User Data\Default\QuotaManager-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\QuotaManager
%LOCALAPPDATA%\Google\Chrome\User Data\Default\LOCK
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Bookmarks.bak
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Affiliation Database
现在这个列表文件和我的批处理脚本在同一个文件夹中,它试图逐行读取这个列表文件并检查这些路径是否确实存在,如果存在则打印这些文件-路径(这些文件路径我打算稍后删除),批处理代码如下:
@echo off && setlocal enabledelayedexpansion
if exist "%~dp0ChromeJunks.lst" ( set "Junks=%~dp0ChromeJunks.lst" )
for /f "tokens=* delims=," %%a in (%Junks%) do ( if exist "%%a" echo "%%a" )
endlocal && pause && exit
但这不会打印任何路径,因为我试图逐行检查这些路径是否实际存在,if exist
,为什么?是因为 Env 变量在从文本文件中读取时不起作用吗?如果我删除 if exist "%%a"
那么它打印得很好,但这是一个交易破坏者。 chrome(即使在完全退出后)也不会自行删除这些文件,所以相信我这些列出的文件就在那里和这就是我尝试打印这些的原因。
我很茫然,谁能帮忙完成这项工作?
当文件路径存储在 %%a
中时,%LOCALAPPDATA%
永远不会扩展,因为常规变量在 for
循环变量之前扩展(有关详细信息,请参阅 How does the Windows Command Interpreter (CMD.EXE) parse scripts?) .为了把%LOCALAPPDATA%
展开成有意义的东西,我们需要用call
再进行一轮展开。
@echo off
setlocal enabledelayedexpansion
if exist "%~dp0ChromeJunks.lst" set "Junks=%~dp0ChromeJunks.lst"
for /f "tokens=* delims=," %%a in (%Junks%) do (
call set "filename=%%~a"
if exist "!filename!" echo !filename!
)
问题很简单,您需要先扩展变量,最简单的方法是在另一个 for
循环中 运行 它,内容将通过该循环发送到另一个 cmd 实例。
如果您的文件内容只是目录,我建议您更像这样:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Echo("%%~G"'
) Do If Exist "%%~H\*" Echo "%%~G"
Pause
EndLocal
Exit /B
或者像这样:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Set /P "=%%~G" 0^<NUL ^& Echo('
) Do If Exist "%%~H\*" Echo "%%~G"
Pause
EndLocal
Exit /B
或者,对于文件和目录的可能混合,我更愿意这样做:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Echo("%%~G"'
) Do If "%%~aH" GEq "d" (Echo %%G is an existing directory.
) Else If "%%~aH" GEq "-" (Echo %%G is an existing file.
) Else Echo %%G does not exist.
Pause
EndLocal
Exit /B
或者像这样:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Set /P "=%%~G" 0^<NUL ^& Echo('
) Do If "%%~aH" GEq "d" (Echo %%G is an existing directory.
) Else If "%%~aH" GEq "-" (Echo %%G is an existing file.
) Else Echo %%G does not exist.
Pause
EndLocal
Exit /B
正如我在 Q-title 中提到的,我正在尝试从列表文件中读取一些我打算稍后删除的文件路径,该列表文件包含由 newline
分隔的这些路径,如下所示[ChromeJunks.lst]:
%LOCALAPPDATA%\Google\Chrome\User Data\Default\History
%LOCALAPPDATA%\Google\Chrome\User Data\Default\History-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Network Action Predictor
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Network Action Predictor-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Top Sites
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Top Sites-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Visited Links
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Favicons
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Favicons-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Web Data
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Web Data-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Log
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Log.old
%LOCALAPPDATA%\Google\Chrome\User Data\Default\QuotaManager-journal
%LOCALAPPDATA%\Google\Chrome\User Data\Default\QuotaManager
%LOCALAPPDATA%\Google\Chrome\User Data\Default\LOCK
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Bookmarks.bak
%LOCALAPPDATA%\Google\Chrome\User Data\Default\Affiliation Database
现在这个列表文件和我的批处理脚本在同一个文件夹中,它试图逐行读取这个列表文件并检查这些路径是否确实存在,如果存在则打印这些文件-路径(这些文件路径我打算稍后删除),批处理代码如下:
@echo off && setlocal enabledelayedexpansion
if exist "%~dp0ChromeJunks.lst" ( set "Junks=%~dp0ChromeJunks.lst" )
for /f "tokens=* delims=," %%a in (%Junks%) do ( if exist "%%a" echo "%%a" )
endlocal && pause && exit
但这不会打印任何路径,因为我试图逐行检查这些路径是否实际存在,if exist
,为什么?是因为 Env 变量在从文本文件中读取时不起作用吗?如果我删除 if exist "%%a"
那么它打印得很好,但这是一个交易破坏者。 chrome(即使在完全退出后)也不会自行删除这些文件,所以相信我这些列出的文件就在那里和这就是我尝试打印这些的原因。
我很茫然,谁能帮忙完成这项工作?
当文件路径存储在 %%a
中时,%LOCALAPPDATA%
永远不会扩展,因为常规变量在 for
循环变量之前扩展(有关详细信息,请参阅 How does the Windows Command Interpreter (CMD.EXE) parse scripts?) .为了把%LOCALAPPDATA%
展开成有意义的东西,我们需要用call
再进行一轮展开。
@echo off
setlocal enabledelayedexpansion
if exist "%~dp0ChromeJunks.lst" set "Junks=%~dp0ChromeJunks.lst"
for /f "tokens=* delims=," %%a in (%Junks%) do (
call set "filename=%%~a"
if exist "!filename!" echo !filename!
)
问题很简单,您需要先扩展变量,最简单的方法是在另一个 for
循环中 运行 它,内容将通过该循环发送到另一个 cmd 实例。
如果您的文件内容只是目录,我建议您更像这样:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Echo("%%~G"'
) Do If Exist "%%~H\*" Echo "%%~G"
Pause
EndLocal
Exit /B
或者像这样:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Set /P "=%%~G" 0^<NUL ^& Echo('
) Do If Exist "%%~H\*" Echo "%%~G"
Pause
EndLocal
Exit /B
或者,对于文件和目录的可能混合,我更愿意这样做:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Echo("%%~G"'
) Do If "%%~aH" GEq "d" (Echo %%G is an existing directory.
) Else If "%%~aH" GEq "-" (Echo %%G is an existing file.
) Else Echo %%G does not exist.
Pause
EndLocal
Exit /B
或者像这样:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Junks=%~dp0ChromeJunks.lst"
If Not Exist "%Junks%" Exit /B
For /F UseBackQ^ Tokens^=*^ Delims^=^ EOL^= %%G In ("%Junks%"
) Do For /F Tokens^=*^ Delims^=^ EOL^= %%H In ('Set /P "=%%~G" 0^<NUL ^& Echo('
) Do If "%%~aH" GEq "d" (Echo %%G is an existing directory.
) Else If "%%~aH" GEq "-" (Echo %%G is an existing file.
) Else Echo %%G does not exist.
Pause
EndLocal
Exit /B