Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#1 Post by Ich bin´s selbst » 29 Aug 2020 22:35

Hello,
I´m the new guy in town. So, here comes my first request and I hope You can help me:

This is what i already got:



Code: Select all

@ECHO OFF

REM The taks is the following:
REM Copy a single file from Quelle into Ziel ( name of the 2 directories )
REM duplicate ( in this case 34 time + continuous numerating
REM keep the file extension

REM Lokale Parameter
SET Quelle=C:\000Quelle
SET Ziel=C:\000Ziel
REM Start at 0
SET Zahl=0
Set Z=1
Set ZE=0

REM Switch on "Delayed Expansion" is a must do
SETLOCAL ENABLEDELAYEDEXPANSION

REM Use Dir...
: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
	SET Zielname=%%i
	ECHO Neu: %Ziel%\!Zahl!!Zielname:~-4!
	ECHO.
	COPY %Quelle%\%%i %Ziel%\!Zahl!!Zielname:~-4!
	if %ZE%==35 goto End
    Goto Zahl
	:End
	ECHO.
	)
[/color][/b]

So, what happens here?

The file of the origin directory ( Quelle ) will becopied and moved to the destination folder ( Ziel )

The numeration works also fine: 1 ~> 35


What does NOT work here:

The filename is not copied and so the new name of the files are only the numeration from 1~> 35

Can anyone please give me a helping hand?


OJBakker
Expert
Posts: 38
Joined: 12 Aug 2011 13:57

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#2 Post by OJBakker » 30 Aug 2020 06:23

In your code you only keep the last 4 characters of the file. This is probably an attempt to just keep the extension including separator dot between filename and extensions.
This is wrong because the extension length can vary between zero (no extension) and a lanrge number, example file: a.bcdefghijklmnopqrstuvwxyz
I have removed the :Zahl label and moved the :End label outside of the for-command block ().
See the code below (untested).

Code: Select all

rem only the last part of the above code

REM Use Dir...
FOR /F "usebackq tokens=*" %%i IN (`DIR %Quelle% /B "*.*"`) DO (
       Set /A ZE=%ZE%+1
       Set /A Z=%Z%+1
	ECHO Alt: %Quelle%\%%i
	SET /A Zahl=!Zahl!+1
	ECHO Neu: %Ziel%\!Zahl!%%i
	ECHO.
	COPY %Quelle%\%%i %Ziel%\!Zahl!%%i
	if %ZE%==35 goto End
)
:End
ECHO.

Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#3 Post by Ich bin´s selbst » 30 Aug 2020 06:57

Hello OJBakker,

thanks a lot for the fast reply.

I tested the script already, but there is something I cannot manage alone.

The result is displayed in the following way:

counter+filename+extension

I´d like the way:

filename+space+counter+extension

I worked already a little around, but the result is poor:

filename+extension+NO space+counter ( when the extension is not at the very end, that is not good for the function, because it means "NO extension" at all as You know.

Can You please give me an additional "helping hand"?

aGerman
Expert
Posts: 3934
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#4 Post by aGerman » 30 Aug 2020 08:21

Try
"%Ziel%\%%~ni !Zahl!%%~xi"
Make sure you keep the quotation marks aroud it because of the space in the name.

Steffen

Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#5 Post by Ich bin´s selbst » 30 Aug 2020 08:51

H(e/a)llo Steffen,

that works fine with me and my script. Many thanks!

In the meantime I took a little parts of Your code from a recent post and tried to manage it alone.
The result was: It also works, but I had a battle with the space between the filename and the number when Your perfect code arrived.

See what I was doing so far:


FOR /F "usebackq tokens=*" %%i IN (`DIR %Quelle% /B "*.*"`) DO (
Set /A ZE=%ZE%+1
Set /A Z=%Z%+1
Set "fname=%%~ni"
Set "fext=%%~xi"
ECHO Alt: %Quelle%\%%i
SET /A Zahl=!Zahl!+1
ECHO Neu: %Ziel%\%%i!Zahl!
ECHO.
COPY %Quelle%\%%i %Ziel%\"!fname!!Zahl!!fext!"
if %ZE%==35 goto End
Goto Zahl
:End
ECHO.
)

...cutting filename and extension, the insert the numeration as well...just without "space"

Here You are the final version with the help of OJBakker and You:


Code: Select all

@ECHO OFF

REM The taks is the following:
REM Copy a single file from Quelle into Ziel ( name of the 2 directories )
REM duplicate ( in this case 34 time + continuous numerating
REM keep the file extension

REM Lokale Parameter
SET Quelle=C:\000Quelle
SET Ziel=C:\000Ziel
REM Start at 0
SET Zahl=0
Set Z=1
Set ZE=0

REM Switch on "Delayed Expansion" is a must do
SETLOCAL ENABLEDELAYEDEXPANSION

REM Use Dir...
:Zahl
    Set /A ZE=%ZE%+1
    Set /A Z=%Z%+1
    
FOR /F "usebackq tokens=*" %%i IN (`DIR %Quelle% /B "*.*"`) DO (
       Set /A ZE=%ZE%+1
       Set /A Z=%Z%+1
	ECHO Alt: %Quelle%\%%i
	SET /A Zahl=!Zahl!+1
	ECHO Neu: %Ziel%\%%i!Zahl!
	ECHO.
	COPY %Quelle%\%%i "%Ziel%\%%~ni !Zahl!%%~xi"
	if %ZE%==35 goto End
    Goto Zahl
	:End
	ECHO.
	)
[/color][/b]

Sorry, but there is a bug: This code brings only 18 instead of the requested 35 item to the destination folder!

I changed this script into the following and now it works:


Code: Select all

@ECHO OFF

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

SETLOCAL ENABLEDELAYEDEXPANSION

: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%==35 goto End
    Goto Zahl
	:End
	ECHO.
       )
[/color][/b]

Thanks once more and stay healthy!

- Eric -

Compo
Posts: 505
Joined: 21 Mar 2014 08:50

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#6 Post by Compo » 30 Aug 2020 11:18

Unless I've misunderstood exactly what you're doing, especially as I've no idea what %Z% is being used or incremented for, it seems that there's an simpler method of doing this:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

REM The task is the following:
REM Copy a single file from %Quelle% into %Ziel% ( name of the 2 directories )
REM duplicate ( in this case 34 time + continuous numerating )
REM keep the file extension

REM Lokale Parameter
SET "Quelle=C:\000Quelle"
SET "Ziel=C:\000Ziel"

REM Use Dir...
FOR /F "EOL=? Delims=" %%G IN ('DIR /B /A:-D "%Quelle%"') DO (
	ECHO Alt: %Quelle%\%%G
	FOR /L %%H In (1 1 34) DO (
		ECHO Neu: %Ziel%\%%~nG %%H%%~xG
		ECHO=
		COPY /Y "%Quelle%\%%G" "%Ziel%\%%~nG %%H%%~xG"
	)
)

Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#7 Post by Ich bin´s selbst » 30 Aug 2020 13:37

Hello Compo,

there are sooo many people who are unable to know what I want or what I´m doing...that´s ok with me. Most of the time I join them. Then I do not feel so alone

So, I checked Your solution and I cannot help saying a big "Thank You".

The first solution that worked, but only duplicated 18 instead of 35 is a heavy task for my computer and lasts right long.
The last 2 scripts are working quickly and their results are rather the same.

Now, I will check the code of these 2 scripts to learn how Batch can be useful practiced by me. I never learned Batch and on the web they are always using new ways like PowerShell and anything "better".

My wishes are not so big. Therefore I think something like Batch and to find out how to handle the syntax is what I will do. With PowerShell I use already a tiny selfmade notifier for ClawsMail. Opimizing that script will be my next step.

Thanks a lot for helping and stay healthy!

Compo
Posts: 505
Joined: 21 Mar 2014 08:50

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#8 Post by Compo » 30 Aug 2020 14:57

Ich bin´s selbst, just to clarify to you, and any future readers, what my code above is supposed to do:

Make thirty four copies of each file within the %Quelle% directory to the %Ziel% directory, each with the increment appended to its basename.

For example:

Code: Select all

C:\000Quelle\FileName.ext » C:\000Ziel\FileName 1.ext
                            C:\000Ziel\FileName 2.ext
                            C:\000Ziel\FileName 3.ext
                            C:\000Ziel\FileName 4.ext
                            C:\000Ziel\FileName 5.ext
                            C:\000Ziel\FileName 6.ext
                            C:\000Ziel\FileName 7.ext
                            C:\000Ziel\FileName 8.ext
                            C:\000Ziel\FileName 9.ext
                            C:\000Ziel\FileName 10.ext
                            C:\000Ziel\FileName 11.ext
                            C:\000Ziel\FileName 12.ext
                            C:\000Ziel\FileName 13.ext
                            C:\000Ziel\FileName 14.ext
                            C:\000Ziel\FileName 15.ext
                            C:\000Ziel\FileName 16.ext
                            C:\000Ziel\FileName 17.ext
                            C:\000Ziel\FileName 18.ext
                            C:\000Ziel\FileName 19.ext
                            C:\000Ziel\FileName 20.ext
                            C:\000Ziel\FileName 21.ext
                            C:\000Ziel\FileName 22.ext
                            C:\000Ziel\FileName 23.ext
                            C:\000Ziel\FileName 24.ext
                            C:\000Ziel\FileName 25.ext
                            C:\000Ziel\FileName 26.ext
                            C:\000Ziel\FileName 27.ext
                            C:\000Ziel\FileName 28.ext
                            C:\000Ziel\FileName 29.ext
                            C:\000Ziel\FileName 30.ext
                            C:\000Ziel\FileName 31.ext
                            C:\000Ziel\FileName 32.ext
                            C:\000Ziel\FileName 33.ext
                            C:\000Ziel\FileName 34.ext

Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#9 Post by Ich bin´s selbst » 30 Aug 2020 15:15

Hello Compo,

the problem is, that I´ve too less experiences with such codes.

The other versions are more clearly for me to "read" and "follow the steps".

I simply do not understand that quality code at all, but the code from aGerman and others I´m better able to understand what will happen over there.

So, if I´m willing to add or manupulate something, the only thing in Your script is the 35...and of cause, that the "z" is in this version sensless and I just forgot to erase these lines.

So, You must be a bit patient with me. Sooner or later I´l gonna find find what´s going on with this - how do they call it? - "Batch". Crazy, sounds like "Coral Island"

Best regards

Compo
Posts: 505
Joined: 21 Mar 2014 08:50

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#10 Post by Compo » 30 Aug 2020 15:59

Ich bin´s selbst, I have absolutely no intention of tutoring you through my code, however, I will say that it's considerably simpler, shorter, quicker, and easier to follow than what you or OJBakker have posted, and aGerman and others haven't posted any code, (Steffen provided only advice for your poor methodology in capturing the extension). In my code there is no delayed expansion needed, no variables are being defined within the loop and there's no set /a incrementing either! All of the information you need for determining how it works is available very quickly, by typing for /? in a Command Prompt window, and pressing the [ENTER] key. If you want an honest opinion, only a fool would choose your/OJBakker's code over mine for any reason whatsover.

Also, you can test the code without actually copying anything, by just adding REM in front of COPY, and adding a PAUSE on a new bottom line, to prevent the window from closing before you've read the output.

The only thing which confused me about your code/posts was whether you wanted 34 copies, as mentioned in the code itself, or 35 copies as mentioned elsewhere. If you want 35, just change 34 in my code for 35

Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#11 Post by Ich bin´s selbst » 30 Aug 2020 16:36

Hello Compo,

the first to change was the 34 into 35, but I didn´t want to correct You or start a discussion.

You don´t understand it!

I need in Your eyes simple or stupid scripts, because I can follow perfect ones.

You mentioned the reason way by Yourself: You do not use variables and other common things for bloody beginners.

Me - I have to! Otherwise I cannot "see" what´s going on in the script and where I am. In a simple code I can get the intention of the autor and if I´m not sure, i put a "Pause" in and confirm my kinda understanding.

OJBakker helped me, Steffen helped me, You helped me. But when I do not understand the meaning and purpose of a code I will have to ask foolish questions again next time.

Remember:
From the codes of the other two "helping hands" I made a few steps further with success by myself and quite alone. When I see the probably best version - for instant Yours - I cannot honour it, because I cannot understand the scripting / syntax.

And what more...next time I´ll start with the foolish script again and try to reach my aim that way. Afterwards I will checkout a "better" version and compare.

Otherwise a best solution script for a beginner is like "throwing pearls in front of the pig"...

Best regards

Compo
Posts: 505
Joined: 21 Mar 2014 08:50

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#12 Post by Compo » 31 Aug 2020 09:15

I've already told you, but you seem unable to either listen or comprehend, the very, very, basic syntax I've used, is fully explained under the help information for the command.

And please don't try to absolve yourself of blame for a confusing question, your code specifically states right within it, REM duplicate ( in this case 34 time. It is not my mistake, you wrote 34, it's yours.

Feel free to continue to make it more difficult for yourself to learn batch file scripting by using less understandable, harder, and slower methodologies, that's your perogative.
Last edited by Compo on 01 Sep 2020 02:52, edited 1 time in total.

Ich bin´s selbst
Posts: 14
Joined: 29 Aug 2020 21:53

Re: Copy Filename - Copy File to other directory - duplicate file - filename+continuous numerating

#13 Post by Ich bin´s selbst » 31 Aug 2020 09:28

Hello Gottfried,

now I´m working hard on my request concierning a DIY - EMail - Notification for Claws - Mail in PowerShell...I have even less idea of ​​that...

Best Regards

- Eric -


Image

Post Reply