批处理文件,用于根据文件名的一部分将文件从一台服务器复制到另一台服务器 [英] Batch file to copy files based on part of filename from one server to another

查看:170
本文介绍了批处理文件,用于根据文件名的一部分将文件从一台服务器复制到另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个批处理文件(通过Windows XP Pro),该文件复制文件名长度不同的2个文件(.zpl). ZPL文件与标签打印机代码有关.文件名如下:

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

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

井号代表与要打印的特定标签/作业相关的数字.这些数字对于每个作业都是相同的.从一项工作到下一项工作,数字的长度各不相同,并且总是在变化.我试图从中提取这些文件的目录包含来自多个位置的ZPL文件,但是,我只需要BHAM文件.

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.

批处理将从以下位置复制:\ Server \ C:\ Directory1 \ Directory2 \ Directory3
要复制到:\ Server \ Directory1 \ Directory2

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

不确定这是否会使事情进一步复杂化,但是批处理文件将从第三台计算机上运行.此外,我不需要每次都复制每个文件.每当发送新的打印作业时,主管将运行该批处理以在最近的X时间内复制新的打印作业. X为分钟.这是我到目前为止所拥有的...

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资料: Reference1 Reference2 -过去一周我到处都在寻找,这是迄今为止最有用的2个.

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

推荐答案

我看到了一些修复或改进BAT脚本的方法.

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

  1. FOR命令语法为FOR %%a IN (*.bat) DO (

将sourcedir变量设置为Server\C:\Directory1\Directory2\Directory3,这在Windows中不是正确的路径.

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

您进行初始化,但不要在FOR循环或复制命令中都使用%sourcedir%variabe

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

您应该使用pushd %sourcedir%命令更改当前驱动器和目录,或者在FOR命令中指定它.

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

您的FOR循环分配了一个从未使用的%filename%变量,您可以跳过此分配.

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

您为FOR循环分配了%folder%变量,该变量仅在复制命令中使用,您可以跳过此分配,而只需使用%targetdir%

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%

但是,要将所有文件从一个文件夹复制到另一个文件夹,则不需要FOR遍历所有文件,只需复制正确即可.

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%

这篇关于批处理文件,用于根据文件名的一部分将文件从一台服务器复制到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆