echo before deleting file through batch file

2019-09-18 17:21发布

问题:

I am running something like

for %j in (c:\user\data) do if not %j == Important del %i

so that all files get deleted except this one Important however, before I run this I wanted to make sure that script will only delete which I meant to hence, I wanted to see the files being affected before I run so I tried using echo as

for %i in ("c:\user\data") do echo %i

but this does not seem working, I am expecting to see all the files in this directory so that I would be sure about the files being affected but I do not see anything, no error or anything. It just looks CMD opens and closes very fast. Can someone please help me..

回答1:

You simply need to refer to the contents of the directory in your condition, rather than merely the directory itself. From command line:

for %i in (c:\user\data\*) do echo %i

And if you're doing it inside a batch file, then you need to add double percent signs for your loop variable:

for %%i in (c:\user\data\*) do echo %%i

More info on syntax here.



回答2:

I managed to get your script to work like this:

cd "C:/user/data"

for /r %%i in (*) do (
    echo %%i
)