简体   繁体   中英

CMD escape spaces and exclude names containing specific string

The code is like this but it doesn't work as expected right now:

@ECHO OFF
for /f %%i in ('dir *.mov /b') do call :test %%i
goto continue
  :test
  if "%1"=="*compressed.mov" goto :eof
  echo "%~f1"
  goto :eof
:continue
pause

I would like to solve two problems here:

  1. How to escape spaces in file names? ie. I would like to include "video 1.mov" file for the test and not echo just the "Drive:\path\video" but instead "Drive:\path\video 1.mov"

  2. How to not echo any file containing string "compressed" at the end of it's name?

Perhaps something like this would suit:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For %%G In ("*.mov") Do If /I "%%~xG" == ".mov" (Set "_=%%~nG"
    SetLocal EnableDelayedExpansion
    If /I Not "!_:~-10!" == "compressed" (EndLocal & Echo "%%~fG"
    ) Else EndLocal
)
Pause

Alternatively use findstr.exe :

@For /F "EOL=? Delims=" %%G In ('Dir "*.mov" /A:-D /B 2^>NUL ^| %SystemRoot%\System32\findstr.exe /LIE ".mov" ^| %SystemRoot%\System32\findstr.exe /VLIE "compressed.mov"') Do @Echo "%__CD__%%%G"
@Pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM