batch file to copy numbered files from one dir to another

Posted on 2014-04-21
Last Modified: 2014-04-22
I have a site that has a file folder that I need to create new files for an older program to work.
I have this a VB program that collects the necessary file numbers and will call the batch file.

The client has requested the batch file as they can change as needed on the fly

It should run the following code at a command line; ADD1 3681 1 100


Tahnk you Experts...Help!!
rem Add1.bAT File for file creation
rem repeating loop to create files for future work 
rem 1st var Starting file number
rem  2nd var is the # of time to run the sequence
Rem 3rd var is the number to add to the starting number
rem ---------------- For loop
rem 1,1,200 Means:
rem	start = 1
rem	increment per step = 1
rem	end = 200
rem *************************************
rem start of code

setlocal EnableDelayedExpansion
@echo off 

set IntCopy=%1.*
set /a result=%1+%3

FOR /L %%A IN (1,1,%2) DO (

Rem **** none of the below  work
Rem **** they return "result.*
Rem **** I need result1 = result +1 & ".*"
rem set result1=!result!.*
rem set result1=!result.*
set result1=%result.*
rem set result1=%result%.*

echo These are the calcs
echo result is; %result% 
echo  3 result1 is; %result1% 
echo  IntCopy is; %IntCopy%

copy %IntCopy% %Result1%

Rem **** Then I need to increment by 1
set result=!result+%3
echo result B4 Loop is %result% 

Open in new window

Question by:mdlp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 100 total points
ID: 40013254
the 'set' below ... increment by 1 is adding to 'result' instead of 'result1', it should be:

set /a result1=!result1+%3

(and the echo command is echoing result instead of result1 as well)

Author Comment

ID: 40013313
Gerwin, thank you , but I also need set result1=%result.* to be 3681.*, could you help me here also?

Author Comment

ID: 40013326
I am sorry for the name being wrong, I started in one direction and then changed, without changing the name of the question.
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40013394
No problem, did you see I've added the /a - that's probably why it isn't adding.

Author Comment

ID: 40013447
Yes, I actually deleted the /a I had it in an earlier version, but changed it during one of my attempts to fix it.

But the line I need help on also is the first line after the FOR command:
I tried it with both % and !, I left all my attempts in the code I posted.

set result1=%result.*

Should take the value of the incremented number that your orig post fixed and then append ".*" to the end

I have attached the screen shot of what my problem is, instead of this returning: the incremented number of result with .* appended to it. It returns  "result.*" in alpha not the numeric form. Please see the 3rd from the bottom line of the screen print

Thank you again
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40013516
You forgot to post a screenshot?

But about your goal: you want 100 copies of a file named 3681 like this:


Author Comment

ID: 40013560
ADD1 3681 1 100

3681 in this example is the master file number

I have 3681.100 -3681.150
I need a 100 of what ever the count is of the selected file.

I would then have 3682 to 3782.100 - 150

(100 * the file count of 3681) for each 3682 - 3782

I know it seems way off, but that is what I need.

It is an old Photo archive program that a client uses.

Thank you again, I am sorry, I was not very clear.

Author Comment

ID: 40013564

Author Comment

ID: 40013947
I do not understand, did I do something wrong?
did I place this in the wrong place?
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40014470
I'm still having trouble understanding what you require.

When you say Master File Number 3681, you mean the file name is 3681 and when you say 3681.100 -3681.150 you mean that you already have 51 files named: 3681.100, 3681.101, 3681.102 ... 3681.149 and 3681.150

Then if you do: ADD1 3681 1 100 - you expect to get another 100 files that are copies of the existing 3681.100-150 series to a new series starting with 3682, like this:

3681.100 copy ->  3682.100
3681.101 copy ->  3682.101
3681.150 copy ->  3682.150

I don't get where  you've got the 3782 from.

Maybe you can post an simple list (Excel) that has your requirements with the existing file names and the new series, with for example 10 new files (so: ADD1 3681 1 10).

LVL 84

Accepted Solution

oBdA earned 400 total points
ID: 40014542
Try if this script does what you want. It's currently in test mode and will only display the "copy" commands it would normally run. Remove the uppercase ECHO in front of "copy" in line 24 to run it for real.
@echo off 
setlocal enabledelayedexpansion

if "%~3"=="" (
	echo Syntax:
	echo %~nx0 ^<Source file number^> ^<Target offset^> ^<Iterations^>
	pause&goto :eof
set SourceFileNumber=%~1
set TargetOffset=%~2
set Iterations=%~3
REM *** '%~dp0' expands to the script's folder, including a traling backslash
set SourceFolder=%~dp0

if not exist "%SourceFolder%%SourceFileNumber%.*" (
	echo No source file matching '%SourceFolder%%SourceFileNumber%.*' found.
	pause&exit /b 1
for /l %%i in (%TargetOffset%, 1, %Iterations%) do (
	set /a CurrentIndex = %%i - TargetOffset + 1
	set /a TargetFileNumber = SourceFileNumber + %%i
	for %%a in ("%SourceFolder%%SourceFileNumber%.*") do (
		echo [!CurrentIndex!/%Iterations%] Creating '!TargetFileNumber!%%~xa' ...
		ECHO copy "%%a" "%SourceFolder%!TargetFileNumber!%%~xa"

Open in new window

And note that if you create 100 copies starting at 3682 , the last index will be 3781, not 3782.

Author Closing Comment

ID: 40015173
I am sorry, this is the worst part of EE, having to split points, Gerwin, you worked so hard and did give me the initial /a that was plaguing me.  

ObDa, you hit the nail on the head, it worked perfectly!

 so if I have split incorrectly please let me know.  I wish I could give both of you 500, but EE will not let us!-(
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40015257
@mdlp - No need for apologies, you are the customer :)

Author Comment

ID: 40015319
Thank you, but I am also a business man that always wants to reward hard work, thank again

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question