Search code examples
listbatch-filecmdwindows-10echo

Printing list of files by reading a list-file not working when checked for actual existence of read paths


As I mentioned in Q-title, I am trying to read some file-paths, which I intend to delete later, from a list-file which contains these paths delimited by newline as below[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

Now this list-file is in same folder as my batch script, which is attempting to read this list-file line-by-line and check if whether these paths actually exist, and if so then print those file-paths(these file-paths I intend to delete later), the batch code as follows:

@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

But this doesn't print any path, because I am trying to check if these paths actually exist line-by-line, with if exist, why ? Is it because of Env variables won't work when read from text-file ? If I remove if exist "%%a" then it prints just fine, but that's a deal-breaker. And chrome(even after exited fully) doesn't delete these files by itself, so trust me these listed files are there and that's why I am trying to print those.

I am at loss, can anyone help out make this work ?


Solution

  • When the file path is stored in %%a, %LOCALAPPDATA% is never expanded because regular variables are expanded before for loop variables (see How does the Windows Command Interpreter (CMD.EXE) parse scripts? for more details). In order to expand %LOCALAPPDATA% into something meaningful, we need to use call to perform another round of expansion.

    @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!
    )