2
@echo off
REM - Check to see if there are any non-school related files on the system (mp3, jpg, png, exe)
REM - And print them out #bigbrother1984

cd G:\Assign2\

FOR /F "tokens=*" %%G IN ('dir /s /b "G:\Assign2\*.mp3"') DO echo %%G >> Found_MusicFiles.txt

FOR /F "tokens=*" %%G IN ('dir /s /b "G:\Assign2\*.JPG"') DO echo %%G >> Found_ImageFiles.txt

FOR /F "tokens=*" %%G IN ('dir /s /b "G:\Assign2\*.PNG"') DO echo %%G >> Found_ImageFiles.txt

 FOR /F "tokens=*" %%G IN ('dir /s /b "G:\Assign2\*.exe"') DO echo %%G >>  Found_GamesFiles.txt

例如,这会在 Assign2 下找到 .mp3、jpg、png、exe 文件,并打印出它找到的结果。但它会像这样打印出来 G:\Assign2\blah\blah\blah\blah\blah\Game.exe

我希望它打印出类似 \blah\game.exe

有任何想法吗?我尝试使用 %~nI,但我不知道如何使用它并且是批处理新手。

4

3 回答 3

2

这应该做你需要的:

@echo off
REM - Check to see if there are any non-school related files on the system (mp3, jpg, png, exe)
REM - And print them out #bigbrother1984

pushd "G:\Assign2\"

FOR /F "delims=" %%a IN ('dir /s /b /a-d *.mp3 *.jpg *.png *.exe ') DO (
for %%b in ("%%~pa.") do >>"%userprofile%\desktop\Found_MusicFiles.txt" echo %%~nxb\%%~nxa
)
popd
于 2013-09-27T08:58:53.703 回答
1

Endoro 提供了一个答案,可以准确地为您提供您所要求的内容。但我怀疑这可能不是你想要的。您只要求提供路径中的最后一个文件夹,但这可能不足以判断它来自何处。

我怀疑您想要从当前目录开始的相对路径。

FORFILES 命令直接提供相对路径:

cd G:\Assign2
forfiles /s /m *.mp3 /c "cmd /c echo @relpath"
etc.

或者不需要CD

forfiles /s /p "G:\Assign2" /m *.mp3 /c "cmd /c echo @relpath"
etc.

输出看起来像".\blah\blah\file.mp3",其中.代表您的起始位置G:\Assign2。(换句话说,相对路径)

于 2013-09-27T11:53:21.017 回答
1

这将对您进行一些小的修改:

FOR %%a IN ("C:\USers\blah\blah\blah\blah\blah\Game.exe") do for %%b in ("%%~pa.") do echo \%%~nxb\%%~nxa
于 2013-09-26T21:59:00.377 回答