Как напечатать файлы с именем текущего каталога вместо полного пути

269
as jak

dir /b /s

Это печатает:
D:\stuff\tmp>dir /b /s D:\stuff\tmp\A.java D:\stuff\tmp\AAA D:\stuff\tmp\B.java D:\stuff\tmp\C.java D:\stuff\tmp\Why.class D:\stuff\tmp\Why.java D:\stuff\tmp\XX.class D:\stuff\tmp\XX.java D:\stuff\tmp\XX.txt D:\stuff\tmp\AAA\aaa.kkk

Мне нужно напечатать:
D:\stuff\tmp>???? tmp\A.java tmp\AAA tmp\B.java tmp\C.java tmp\Why.class tmp\Why.java tmp\XX.class tmp\XX.java tmp\XX.txt tmp\AAA\aaa.kkk

Как я могу это сделать?

2
Какую проблему ты пытаешься решить? dsolimano 8 лет назад 0
Я нашел это: [Команда TechNet DIR] (https://technet.microsoft.com/en-us/library/cc755121.aspx). У меня нет времени на это, но там должно быть сказано, можете ли вы это сделать или нет. Dooley_labs 8 лет назад 0

1 ответ на вопрос

0
GuitarPicker

This works for me, if you put the following in a CMD file:

@echo off SETLOCAL EnableDelayedExpansion for /f "usebackq delims=" %%A in (`cmd /c dir /s /b`) DO ( set NAME=%%A set SHORT=!NAME:%cd%\=! echo.!SHORT! ) 

It takes the output of your original dir command and uses string substitution to remove the current directory. See set /? for more information on this. If you prefer to pass the directory as an argument instead of using the current directory, try this modification of the variables:

@echo off SETLOCAL EnableDelayedExpansion for /f "usebackq delims=" %%A in (`cmd /c dir /s /b %1`) DO ( set NAME=%%A set SHORT=!NAME:%~1\=! echo.!SHORT! ) 

Beware of filenames and directories which have an exclamation mark in the filename, as they tend to get stripped out with this method. If you need something more robust which can handle this, look into installing GNU sed or grep. You can also investigate using the GNU find command if you don't mind the filenames coming back UNIX style with slashes instead of backslashes, and a leading ./ in front of each entry.

There is also another answer which may give you ideas on handling exclamation marks in CMD processing: https://stackoverflow.com/questions/32873578/how-to-read-a-string-with-from-a-file-in-windows-batch-script/32874195#32874195