質問

@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

So for example this finds .mp3, jpg, png, exe files below Assign2, and prints out the results of what it finds. But it prints them out like this G:\Assign2\blah\blah\blah\blah\blah\Game.exe

I would like it to print out something like \blah\game.exe

Any ideas? I tried using %~nI, but I don't know how to use it and am new to batch.

役に立ちましたか?

解決

This should do what you need:

@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

他のヒント

this will work for you with some small modifications:

FOR %%a IN ("C:\USers\blah\blah\blah\blah\blah\Game.exe") do for %%b in ("%%~pa.") do echo \%%~nxb\%%~nxa

Endoro has provided an answer that gives you exactly what you have asked for. But I suspect that may not really be what you want. You have asked for just the last folder in the path, but that may not be enough to tell where it is coming from.

I suspect you want the relative path, starting from your current directory.

The FORFILES command provides relative paths directly:

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

Or without needing CD

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

The output would look like ".\blah\blah\file.mp3", where the . represents your starting location of G:\Assign2. (in other words, a relative path)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top