简体   繁体   English

Windows批处理文件-一次处理一个文件

[英]Windows batch file - process one file at a time

Odd question - but it's driving me a bit crazy. 奇怪的问题-但这使我有些疯狂。 I have a directory where multiple files can be dumped via FTP, then I need to process them one at time. 我有一个目录,可以通过FTP转储多个文件,然后需要一次对其进行处理。 So basically in this directory I could have 1.txt, 2.txt, 3.txt etc then I need to: 所以基本上在这个目录中我可以有1.txt,2.txt,3.txt等,然后我需要:

copy the file to an archive (if exist copy to archive) 将文件复制到存档(如果存在,则复制到存档)

move the file to one specific filename one at a time - data.txt <--- this is what's getting me 一次将文件移到一个特定的文件名-data.txt <---这就是我的意思

run a command on a legacy backend system client using that specific filename (data.txt) 使用特定的文件名(data.txt)在旧版后端系统客户端上运行命令

run another command on legacy client using data.txt 使用data.txt在旧版客户端上运行另一个命令

delete data.txt 删除data.txt

Move on to the next file and repeat 移至下一个文件并重复

So far I've tried several methods of do loops without any luck - they all get hung up on trying to rename multiple files into one file, and that just kills me. 到目前为止,我已经尝试了几种没有任何运气的do循环方法-它们都挂在试图将多个文件重命名为一个文件的过程中,这简直让我丧命。 I'd long ago since given up on batch files but annoyingly, this application has to use windows, and Server 2003 to boot. 我很久以前就放弃了批处理文件,但是很烦人的是,此应用程序必须使用Windows和Server 2003进行引导。

EDIT: Here's what I've tried- 编辑:这是我尝试过的-

  1. This works to do one file at a time: 可以一次执行一个文件:

    if exist c:\\jail\\ftp*.txt copy c:\\jail\\ftp*.txt w:\\scans\\archive*.txt 如果存在c:\\ jail \\ ftp * .txt复制c:\\ jail \\ ftp * .txt w:\\ scans \\ archive * .txt
    if exist c:\\jail\\ftp*.txt move c:\\jail\\ftp*.txt w:\\data.txt 如果存在c:\\ jail \\ ftp * .txt移动c:\\ jail \\ ftp * .txt w:\\ data.txt
    if exist w:\\data.txt C:\\temp\\rmtcmdb.exe 如果存在w:\\ data.txt C:\\ temp \\ rmtcmdb.exe
    if exist w:\\data.txt del w:\\data.txt 如果存在w:\\ data.txt删除w:\\ data.txt

  2. I've tried multiple for loops without success, here is the latest (NOTE - I'm just trying to get past the move stage on this one, once I'm done with that I'll add in the rest): 我已经尝试了多个for循环,但都没有成功,这是最新的(注意-我只是试图通过这一阶段的移动阶段,一旦完成,我将添加其余部分):

    setlocal ENABLEDELAYEDEXPANSION setlocal ENABLEDELAYEDEXPANSION
    FOR /f %%a IN ("c:\\jail\\ftp\\") DO ( FOR / f %% a IN(“ c:\\ jail \\ ftp \\”)DO(
    CALL SET /A x = !x! 呼叫设置/ A x =!x! +1 +1
    if !x! 如果!x! == 1 ( == 1(
    CALL copy %%aw:\\scans\\archive*.txt 呼叫复制%% aw:\\ scans \\ archive * .txt
    CALL move %%aw:\\data.txt 呼叫移动%% aw:\\ data.txt
    )
    )

I've also tried some very basic for loops, and again - nothing is getting past the move stage. 我还尝试了一些非常基本的for循环,再说一次-移动阶段没有任何进展。

Any suggestions? 有什么建议么?

@echo off

    setlocal enableextensions

    for %%a in ("c:\jail\ftp*.txt") do (

        set "fileName=%%~na"
        setlocal enabledelayedexpansion
        copy "%%~fa" "w:\scans\!fileName:*ftp=archive!%%~xa"
        endlocal

        move /y "%%~fa" "w:\data.txt"

        start "" /wait "c:\temp\rmtcmdb.exe"

        if exist "w:\data.txt" del /s "w:\data.txt" >nul 2>nul 
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM