Condition of entry into "IF" and exit

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Condition of entry into "IF" and exit

#1 Post by ceccozambu » 25 Feb 2015 06:21

Hello everyone !
I'm new to this forum but I thank in advance anyone help me ..

I am writing to ask for help ... I need to create a .Bat file that performs the following tasks:

If in the folder there is only .txt files I have to play the first "IF" if there are more than one then I have to play the second.
If there are “zero” files (no file) do not have to do anything.

The program inside the first “IF” if taken individually works, but I Can not set the condition of 'IF and the condition to end.

Someone can help me?

Thanks in advance

Code: Select all

@echo off
REM enter into directory
@CD %c:\Test%

REM count the .txt file present
set cnt=0
for %%A in (*.txt) do set /a cnt+=1
echo File count = %cnt%

REM if the number is equal to "1" run of operations. If it is different from "1" move all the files to another directory



IF %cnt% == 1 end
(

        FOR %%i IN (*.txt)DO SET var=%%i
      REM if there is at least one file with extension .txt

      IF %var%== GOTO end
       REM creo una nuova variabile per eseguire un "Substring"
       SET nuovovar=%var:~2,-4%
       REM e tramite essa creo i nuovi files

       COPY NUL CUST_%nuovovar%.dat
       COPY NUL INVN_%nuovovar%.dat
      COPY NUL LOG_%nuovovar%.dat
       COPY NUL M_%nuovovar%.dat
       COPY NUL P_%nuovovar%.dat
       COPY NUL PO_%nuovovar%.dat
       COPY NUL storeqty_%nuovovar%.dat
       COPY NUL VEND_%nuovovar%.dat


       REM rename file .txt
       REN %var% %var:~0,-4%.dat
   
       REM move file
    

       COPY . %dest%\.
       ERASE/Q .
      :end
      REM activation  READY OK

      REN %dest%\READYTX READYRX
         )
:end



IF NOT %cnt%==1 end2
 (
         REM enter into the directory where I move all files
         @SPOST %c:\Test\Spostati%
          
         REM sposto il contenuto della
          REM directory nella sua destinazione definitiva


          COPY . %SPOST%\.
          ERASE/Q .
      )

:end2
Last edited by Squashman on 25 Feb 2015 07:32, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Condition of entry into "IF" and exit

#2 Post by Squashman » 25 Feb 2015 07:38

You have lots of syntax errors.
START by fixing these.

Code: Select all

IF "%cnt%"=="1" GOTO end
IF "%var%"=="" GOTO end
IF NOT "%cnt%"=="1" GOTO end2


Not really understanding the flow of your program.

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Condition of entry into "IF" and exit

#3 Post by ceccozambu » 26 Feb 2015 03:04

Thank you very much Squashman! :-)

Let me explain better

The program is designed to prepare the files for a simple application of my family business.

1) counts the files in the folder "C: \ Test" and save the nuemro in the variable "cnt"

2) if there is only one file, then copy it to a new destination and creates others with particular names (procedure that already works)

3) if the folder there are more than one file, or "cnt"> 1 then has to move all the files in a new directory and then lascire empty the source folder.

4) if there is no file "cnt = 0" needs to do nothing


Really thanks in advance!

Code: Select all

@echo off
REM enter into directory
@CD %c:\Test%

REM count the .txt file present
set cnt=0
for %%A in (*.txt) do set /a cnt+=1
echo File count = %cnt%

REM if the number is equal to "1" run of operations. If it is different from "1" move all the files to another directory c:\Test\Moved

IF "%cnt%"=="1" GOTO end
(

        FOR %%i IN (*.txt)DO SET var=%%i
       REM if there is at least one file with extension .txt

       IF "%var%"== GOTO end
       REM I create a new variable to perform a "Substring"
       SET nuovovar=%var:~2,-4%
       REM and through it I create new files

       COPY NUL CUST_%nuovovar%.dat
       COPY NUL INVN_%nuovovar%.dat
       COPY NUL LOG_%nuovovar%.dat
       COPY NUL M_%nuovovar%.dat
       COPY NUL P_%nuovovar%.dat
       COPY NUL PO_%nuovovar%.dat
       COPY NUL storeqty_%nuovovar%.dat
       COPY NUL VEND_%nuovovar%.dat


       REM rename file .txt
       REN %var% %var:~0,-4%.dat
   
       REM move file
   

       COPY . %dest%\.
       ERASE/Q .
      :end
      REM activation  READY OK

      REN %dest%\READYTX READYRX
         )
:end



IF NOT "%cnt%"=="1" end2
 (
         REM enter into the directory where I move all files
         @moved %c:\Test\Moved%
         
         REM I move the contents of the directory C:\Test in its final
 
          COPY . %moved%\.
          ERASE/Q .
      )
Last edited by Squashman on 26 Feb 2015 09:28, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Condition of entry into "IF" and exit

#4 Post by Squashman » 26 Feb 2015 09:29

You didn't fix all of the problems I addressed. Looks like you missed 2nd and third.

You can remove the parentheses as well.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Condition of entry into "IF" and exit

#5 Post by foxidrive » 26 Feb 2015 09:47

Test this on some sample dummy test files. Note that you have not set the dest variable anywhere in this script.


Code: Select all

@echo off
REM enter into directory
CD /d "c:\Test"

REM count the .txt file present
set "cnt=0"
for %%A in (*.txt) do set /a cnt+=1

if %cnt% EQU 0 goto :EOF
if %cnt% GTR 1 (
   md "c:\Test\Moved" 2>nul
   move *.* "c:\Test\Moved" >nul
   goto :EOF
)

       FOR %%i IN (*.txt) DO SET "var=%%i"
       REM I create a new variable to perform a "Substring"
       SET "nuovovar=%var:~2,-4%"
       REM and through it I create new files

  for %%a in (
       "CUST_"
       "INVN_"
       "LOG_"
       "M_"
       "P_"
       "PO_"
       "storeqty_"
       "VEND_"
    ) do break >"%%~a%nuovovar%.dat"

       REM rename file .txt
       REN "%var%" "%var:~0,-4%.dat"
   
       REM move file
       move *.* "%dest%\"
      REN "%dest%\READYTX" "READYRX"

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Condition of entry into "IF" and exit

#6 Post by Squashman » 26 Feb 2015 11:11

Nice. I have never thought about using break to create an empty file. Have always used TYPE NUL.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Condition of entry into "IF" and exit

#7 Post by foxidrive » 27 Feb 2015 03:56

I learned it from one of the blokes here.

npocmaka I think, maybe carlos.

I could be wrong on both counts too - but it's short to type which is why I like it too.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Condition of entry into "IF" and exit

#8 Post by Squashman » 27 Feb 2015 06:46

foxidrive wrote:I could be wrong on both counts too - but it's short to type which is why I like it too.

You old guys just have no patience. :D

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Condition of entry into "IF" and exit

#9 Post by penpen » 27 Feb 2015 09:07

You could save one additional character by using "rem:" instead.

penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Condition of entry into "IF" and exit

#10 Post by Squashman » 27 Feb 2015 10:41

penpen wrote:You could save one additional character by using "rem:" instead.

penpen

:lol:
We need a snort laugh emoticon!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Condition of entry into "IF" and exit

#11 Post by foxidrive » 27 Feb 2015 10:52

penpen wrote:You could save one additional character by using "rem:" instead.

penpen


:D

Yes, but you have to press the shift key on my keyboard.

Just having a little play here and "rem." works too though.

ceccozambu
Posts: 15
Joined: 25 Feb 2015 06:15

Re: Condition of entry into "IF" and exit

#12 Post by ceccozambu » 27 Feb 2015 11:02

Now it works perfectly!
Thanks so much for the help!

Post Reply