Search code examples
windowsbatch-filecmdforfiles

IF multiple conditions


F.e. there is some folder on user's desktop. I want to remove any files within this folder except shortcuts. There are two types of shortcuts - to some local\network resources and shortcuts to some sites.

"shortcuts on some local\network resources" have .lnk extension

"shortcuts to some sites" have .url extension

I've already found how to do this task separately

forfiles /p "%userprofile%\Desktop\folder" /s /m *.* /c "cmd /c for %G in (@path) do @if /I [%~xG] neq [.lnk] del /F /Q %G"

and

forfiles /p "%userprofile%\Desktop\folder" /s /m *.* /c "cmd /c for %G in (@path) do @if /I [%~xG] neq [.url] del /F /Q %G"

But how to combine these conditions into one string? Something like if /I [%~xG] neq [.lnk] AND [.lnk] ...


Solution

  • There's absolutely no need to use a within your /C command.

    Try either;

    ForFiles /P "%UserProfile%\Desktop\folder" /S /C "Cmd /C If /I Not @ext==\"lnk\" If /I Not @ext==\"url\" Del /A /F @path"
    

    Or use the hex codes as shown in the help information, available at the Command Prompt by entering, forfiles /?

    ForFiles /P "%UserProfile%\Desktop\folder" /S /C "Cmd /C If /I Not @ext==0x22lnk0x22 If /I Not @ext==0x22url0x22" Del /A /F @path"