0

I'm trying to create a batch file (via Windows XP Pro) that copies 2 files (.zpl) who's filename's vary in length. ZPL files relate to label printer code. File names are as follows:

FillXferDataPBHAMFill###########.zpl
FillFormatsPBHAMFill############.zpl

The pound signs represent a number associated with a particular label/job to be printed. These numbers are identical per job. From one job to the next the numbers vary in length and always change. The directory I'm trying to pull these from contains ZPL files from multiple locations, however, I just want the BHAM ones.

The batch will copy from: \Server\C:\Directory1\Directory2\Directory3
To be copied to: \Server\Directory1\Directory2

Not sure if this will complicate things further, but the batch file will be ran from a 3rd machine. Furthermore, I do not need to copy every file everytime. Whenever new print jobs are sent, supervisors will run the batch to copy the new print jobs within the last X amount of time. X being minutes. Here is what I have so far...

@echo off
SETLOCAL enableExtensions enableDelayedExpansion

SET sourceDir=Server\C:\Directory1\Directory2\Directory3
SET targetDir=Server\Directory1\Directory2

FOR %%a (FillFormatsPBHAM*.bat) DO (
SET "filename=%%a"
SET "folder=%targetDir%"
XCOPY "%%a" !folder!
)

FOR %%b (FillXferDataPBHAM*.bat) DO (
SET "filename=%%b"
SET "folder=%targetDir%"
XCOPY "%%b" !folder!
)

:END

I apologize for a lengthy post; just wanting to be as thorough as possible. I'm learning this on the fly so bare with any ignorance on my part. Thank you in advance for ANY help!!

StackOverFlow Material Reviewed: Reference1, Reference2 -- I've been looking everywhere over the past week and these were the 2 most helpful so far.

Community
  • 1
  • 1
  • 1
    That `\Server\C:\Directory1\Directory2\Directory3` path will definitely cause error. Drive letter specifier must always at the start of path name. e.g.: `C:\Server\Directory1\Directory2\Directory3`. And if the path is located in a remote server, it's usually like this: `\\Server\ShareName\Directory1\Directory2\Directory3`. – Jay Jul 27 '12 at 21:08

1 Answers1

1

I see some ways to fix or improve your BAT script.

  1. The FOR command syntax is FOR %%a IN (*.bat) DO (

  2. The sourcedir variable is set Server\C:\Directory1\Directory2\Directory3, which is not a correct path in Windows.

  3. you initialize but don't use %sourcedir% variabe neither in your FOR loop nor in your copy commnand

    you should either change the current drive and dir with a pushd %sourcedir% command, or specifying it in the FOR command.

  4. your FOR loop assigns a %filename% variable that is never used, you may skip this assignment.

  5. you FOR loop assigns a %folder% variable that is only then used in the copy command, you can skip this assignment and simply use %targetdir%

  6. but, to just copy all files from one folder to the other you don't need FOR to iterate over all of them, you might just copy them right.

So, take a look at this simple script to get you started..

SET sourceDir=\\servername\sharename\Directory1\Directory2\Directory3
SET targetDir=\\anotherserver\sharename\Directory1\Directory2
xcopy %sourceDir%\FillFormatsPBHAM*.bat %targetDir%
xcopy %sourceDir%\FillXferDataPBHAM*.bat  %targetDir%
PA.
  • 28,486
  • 9
  • 71
  • 95