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

Time:03-10

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 ?

CodePudding user response:

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

CodePudding user response:

The issue is simply that you need to expand the variable first, the simplest way to do that would be to run it within another for loop, where the content would be sent through to another cmd instance.

If your file content is only ever going to be directories, I would advise you do it more like this:

@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

Or maybe like this:

@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

Alternatively, for a possible mixture of files and directories, I'd do it more like this:

@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

or like this:

@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
  • Related