[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

batch file to copy numbered files from one dir to another

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

  • 8
  • 5
2 Solutions
Gerwin Jansen, EE MVETopic Advisor Commented:
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)
mdlpAuthor Commented:
Gerwin, thank you , but I also need set result1=%result.* to be 3681.*, could you help me here also?
mdlpAuthor Commented:
I am sorry for the name being wrong, I started in one direction and then changed, without changing the name of the question.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Gerwin Jansen, EE MVETopic Advisor Commented:
No problem, did you see I've added the /a - that's probably why it isn't adding.
mdlpAuthor Commented:
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
Gerwin Jansen, EE MVETopic Advisor Commented:
You forgot to post a screenshot?

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

mdlpAuthor Commented:
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.
mdlpAuthor Commented:
mdlpAuthor Commented:
I do not understand, did I do something wrong?
did I place this in the wrong place?
Gerwin Jansen, EE MVETopic Advisor Commented:
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).

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.
mdlpAuthor Commented:
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!-(
Gerwin Jansen, EE MVETopic Advisor Commented:
@mdlp - No need for apologies, you are the customer :)
mdlpAuthor Commented:
Thank you, but I am also a business man that always wants to reward hard work, thank again

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now