Doubts with conditional commands in Batch files

Asked

Viewed 1,106 times

1

The Intent is to copy files .jpg to a subfolder called \image, and the others, I mean, different files from .jpg, to the folder \New folder (2)\.

Only it’s not working, which could be wrong?

Below the code batch that I’m using:

cls
@echo COPIANDO ARQUIVOS JPEG

for %%f in (*.*) do (
    if %%f equ *.jpg ( 
       copy %%f "C:\Users\anezio\Desktop\Nova pasta (2)\imagem"
      ) else (
       copy %%f "C:\Users\anezio\Desktop\Nova pasta (2)"
      )
 )
pause
  • What mistake are you encountering? What do you expect to happen, and what is happening?

  • Actually the file, when executed, does not meet the first condition (where you should copy .jpg files to the image folder), running only the copy after 'Else''.

3 answers

2

You need to use a modifier to get the file extension. You will never have a file named *.jpg, what is what you are comparing. Using the modifier ~x as shown below should solve your problem.

cls
@echo COPIANDO ARQUIVOS JPEG

for %%f in (*.*) do (
if %%~xf equ .jpg ( 
copy %%f "C:\Users\anezio\Desktop\Nova pasta (2)\imagem"
) else (
copy %%f "C:\Users\anezio\Desktop\Nova pasta (2)"
)
)
pause
  • Thanks for the strength and attention Carlosfigueira, but it did not work friend =/

  • If you could explain to me how this mod scheme works, I’d be most grateful.

  • Take a look at the documentation of the command for of the batch: for /?. You will find the list of all modifiers it supports. For example: %~xf: extension; %~nf: file name...

1

inserir a descrição da imagem aqui

1) loop for listing all files in folders and subfolders (dir /s ..)

2) check if eh a jpg and if positive/negative, copy the target folder attending the filter

3) implemented a jpg, other and total p counter

@echo off & setlocal enableextensions enabledelayedexpansion

cd /d "%~dp0" & cls && color 0a & mode con cols=100 lines=8

set _cnt_jpg=0
set _cnt_outros=0
set /a _cnt_total=!_cnt_jpg! + !_cnt_outros!

for /f "tokens=* delims= " %%f in ('dir /a:-d /b /s *.*') do ( 

    echo/ && Title Arquivos Copiados !_show_cnt_total:~-7! ...

    (

    <nul echo/%%f | findstr /i /l /c:".jpg" 

    ) 2>nul 
    if "!errorlevel!" == "0"  (

         set /a "_cnt_jpg=!_cnt_jpg! + 1" & copy "%%~f" "%userprofile%\Desktop\Nova pasta (2)\imagem\" /v /y >nul 2>nul

        ) else (

         set /a "_cnt_outros=!_cnt_outros! + 1" & copy "%%~f" "%userprofile%\Desktop\Nova pasta (2)\" /v /y >nul 2>nul

        )

      echo. && echo/ COPIANDO ARQUIVOS JPEG... 

      set /a _cnt_total=!_cnt_jpg! + !_cnt_outros!
      set "_show_cnt_jpg=000000!_cnt_jpg!"&& echo/ Arquivos .jpgs copiados: !_show_cnt_jpg:~-7!
      set "_show_cnt_outros=000000!_cnt_outros!"&& echo/ Arquivos n jpg copiados: !_show_cnt_outros:~-7!
      Echo/ --------------------------------
      set "_show_cnt_total=000000!_cnt_total!"&& echo/ Totalizado jpg + outros: !_show_cnt_total:~-7!

    )

1

::issae

    cls
    @echo COPIANDO ARQUIVOS JPEG

    for %%f in (*.*) do (
            if "%%~xf" equ ".jpg" ( 
                    copy %%f "C:\Users\anezio\Desktop\Nova pasta (2)\imagem"
                    ) else (
                    copy %%f "C:\Users\anezio\Desktop\Nova pasta (2)"
                    )
            )
    pause

Browser other questions tagged

You are not signed in. Login or sign up in order to post.