Page 1 of 1

Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 02 Sep 2020 15:22
by Ich bin´s selbst
Hello,

this is my final version of the Script with the following funktion:

1. 2 Folders - Copy a file from one to the other directory ( source to target )

2. Ask how often den copied file shall be duplicated - if yes...no...correct...cancel...automatically "YES" after a certain time + a few times "Echo"

3. Copy as often as wanted and rename + a marvelous continuous numerating

4. Well done thanks the helping hand of aGerman, Compo and last but not least OJBakker


This is the script ( German text version ):

Code: Select all

@ECHO OFF

SET Quelle=C:\000Quelle
SET Ziel=C:\000Ziel
SET Zahl=0
Set Z=1
Set ZE=0

SETLOCAL ENABLEDELAYEDEXPANSION

if "%1"=="" goto eingabe
set /Anzahl=%1
goto ausgabe

:eingabe
set /p Anzahl=So oft soll kopiert werden:
goto ausgabe

:ausgabe
echo Ausgabe zur Kontrolle:
echo Anzahl=%Anzahl%

choice /C JNA /D J /T 6 /M "J = Ja, N = Nein oder A = Abbrechen oder einfach warten, dann geht automatisch weiter"
if errorlevel 3 goto EXIT
if errorlevel 2 goto NEIN
if errorlevel 1 goto Zahl
:NEIN
        echo.
        echo Falsche Eingabe, Du Depp
        echo.
REM pause
        timeout /T 9
        cls
        GOTO eingabe
:Zahl
    Set /A ZE=%ZE%+1
    Set /A Z=%Z%+1
    
FOR /F "usebackq tokens=*" %%i IN (`DIR %Quelle% /B "*.*"`) DO (
	ECHO Alt: %Quelle%\%%i
	SET /A Zahl=!Zahl!+1
	ECHO Neu: %Ziel%\!Zahl!!
	ECHO.
	COPY %Quelle%\%%i "%Ziel%\%%~ni !Zahl!%%~xi"
	if %ZE%==%Anzahl% goto End
    Goto Zahl
        
	:End
	ECHO.
	)
This is the script ( English text version ):

Code: Select all

@ECHO OFF

SET Source=C:\000Source
SET Target=C:\000Target
SET Number=0
Set Z=1
Set ZE=0

SETLOCAL ENABLEDELAYEDEXPANSION

if "%1"=="" goto input
set /Amount=%1
goto output

:input
set /p Amount=How often would You like to duplicate:
goto output

:output
echo Check the Value here:
echo Amount=%Amount%

choice /C YNC /D Y /T 6 /M "Y = Yes, N = No or C = Cancel - or just wait a few seconds and the show goes on"
if errorlevel 3 goto EXIT
if errorlevel 2 goto No
if errorlevel 1 goto Number
:No
        echo.
        echo Oh, a little mistake, Mr. Trump
        echo.
REM pause
        timeout /T 9
        cls
        GOTO input
:Number
    Set /A ZE=%ZE%+1
    Set /A Z=%Z%+1
    
FOR /F "usebackq tokens=*" %%i IN (`DIR %Source% /B "*.*"`) DO (
	ECHO Alt: %Source%\%%i
	SET /A Number=!Number!+1
	ECHO Neu: %Target%\!Number!!
	ECHO.
	COPY %Source%\%%i "%Target%\%%~ni !Number!%%~xi"
	if %ZE%==%Amount% goto End
    Goto Number
        
	:End
	ECHO.
	)
What I want:

A check whether the entered value is a number


Is there anyone, lending me a helping hand?

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 02 Sep 2020 16:14
by Squashman
A common way to check if a variable is a number can partially be achieved by using the DELIMS option of the FOR command.

Code: Select all

FOR /F "delims=0123456789" %%G IN ("%var%") DO echo %var% is NOT a number
But you also need to check for a leading zeros.

Code: Select all

IF "%var:~0,1%"=="0" echo %var% is NOT a number

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 02 Sep 2020 16:32
by aGerman
Ich bin´s selbst wrote:
02 Sep 2020 15:22
What I want:

A check whether the entered value is a number
Well, many different values are numbers. -4711 is one, 0x2A is one (decimal value 42), 012 is one (decimal value 10), 0.456 is one (but batch does only recognize integers by default).
However, based on the context I assume you want to check if a positive decimal integer was entered, and I guess you rather want to limit it because otherwise the biggest supported value would be 2147483647.

Maybe a little function like this one is what you're looking for. (Probably still more complicated than you would have expected :lol: )

Code: Select all

@echo off &setlocal

set "inp="
set /p "inp=> "

:: first argument is the variable name (not its value), second argument is the minimum, third argument is the maximum
call :check inp 1 5
if errorlevel 1 (echo Error!) else echo %inp% is valid.

pause
exit /b


:check VarName Min Max
setlocal EnableDelayedExpansion
:: Only digits allowed.
for /f "delims=1234567890 eol=" %%i in ("!%~1!") do (endlocal &exit /b 1)
:: Valid number (everything where SET /A doesn't fail).
2>nul set /a "dummy=!%~1!" || (endlocal &exit /b 1)
:: Decimal integer (no octal number with a preceding 0).
if "!%~1!" neq "%dummy%" (endlocal &exit /b 1)
:: Minimum check
if %dummy% lss %~2 (endlocal &exit /b 1)
:: Maximum check
if %dummy% gtr %~3 (endlocal &exit /b 1)
endlocal &exit /b 0
Steffen

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 02 Sep 2020 18:27
by Ich bin´s selbst
Hello Steffen,

indead, I´m not so familiar with the syntax.

Meanwhile, I use a lot of different ways, but there is always a malfunction.

Can You please put the code into my script?

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 03 Sep 2020 12:57
by Aacini
Sorry. I will not read any message that have the full text posted in colors and/or bold text with no reason...

Antonio

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 03 Sep 2020 14:57
by aGerman
Not tested:

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "src=C:\000Quelle"
set "dst=C:\000Ziel"
set "upperlimit=100"

if "%~1" neq "" (
  set "number=%~1"
) else (
  call :input
)

:output
echo Check the input:
setlocal EnableDelayedExpansion
echo number=!number!
endlocal
choice /C YNC /D Y /T 6 /N /M "Y = Yes, N = No or C = Cancel - or just wait a few seconds and the show goes on: "
if errorlevel 3 exit /b
if errorlevel 2 (
  echo wrong input
  timeout /T 9
  cls
  call :input
  goto output
)

call :check number 1 %upperlimit%
if errorlevel 1 (
  echo valid input: 1..%upperlimit%
  timeout /T 9
  cls
  call :input
  goto output
)

set "file="
for /f "tokens=*" %%i in ('2^>nul dir /a-d /b "%src%\*.*"') do (
  set "file=%%i"
  set "fname=%%~ni"
  set "fext=%%~xi"
)

if not defined file (
  echo file not found
  timeout /T 9
  exit /b
)

for /l %%i in (1,1,%number%) do (
  echo old: "%src%\%file%"
  echo new: "%dst%\%fname% %%i%fext%"
  copy "%src%\%file%" "%dst%\%fname% %%i%fext%"
)
pause
exit /b
:: END OF MAIN CODE HERE

:: SUBROUTINES:
:input
set "number="
set /p "number=Number of copies: "
exit /b

:check VarName Min Max
setlocal EnableDelayedExpansion
for /f "delims=1234567890 eol=" %%i in ("!%~1!") do (endlocal &exit /b 1)
2>nul set /a "dummy=!%~1!" || (endlocal &exit /b 1)
if "!%~1!" neq "%dummy%" (endlocal &exit /b 1)
if %dummy% lss %~2 (endlocal &exit /b 1)
if %dummy% gtr %~3 (endlocal &exit /b 1)
endlocal &exit /b 0
You only need GOTO to generate kind of a WHILE loop (which doesn't exist in the batch syntax). Excessive use of the GOTO command creates code without reasonable flow. Always think twice and try to find a better way without GOTO.

Steffen

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 03 Sep 2020 17:47
by Ich bin´s selbst
Hello Steffen,

thanks for Your help.

The point is indeed the syntx..

The code You posted recently didn´t work properly and I cannot find the error...

This new code:

I put in "11" for the number of duplicated files.
The result: 442 copies...now I try to find out, where the problem is. And most of the time it´s only a very litte syntax mistake.


Now I started the batch again and it works: 11 = 11 copies; 23 = 23 copies; aa = errorhandling...so, I didn´t do anything, but the result is a working script?!

I hope Your are patient with me and be sure, I will try to find out the better way to create scripts...but that lasts a little when you´re a novice.

Best regards

- Eric -

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 04 Sep 2020 01:17
by aGerman
Weird. Now that I testet the script it does work as expected. Did you modify it somehow? Because I can't reproduce the malfunction.
I hope Your are patient with me and be sure, I will try to find out the better way to create scripts...but that lasts a little when you´re a novice.
No problem. It's just to protect you from looking at the script in half a year and you're not even able anymore to follow your own code flow. That's the reason why the majority of modern languages don't even support things like a GOTO. It generates Spagetti code. Try to follow a Spagetti in the tomato sauce on the table. You see some ends but you won't be able to recognize which ends belong to a certain Spagetti and you can't see the route it takes :lol:

Steffen

Re: Copy, duplicate, rename...all that works, but the valuecheck is missing

Posted: 04 Sep 2020 01:32
by Ich bin´s selbst
Hello Steffen,

no, the first code ( that I was not able to insert ) didn´t work over here. I don´t know why.

With Your last script everything is perfect. Just the first start brought far more than "11" copies. Without any modification the second try was perfect.

Thanks a lot and now I do what i use to do all the time: UNDERSTANDING the code!

Best regards

- Eric -


Image