简体   繁体   English

使用批处理文件通过网络复制文件

[英]Copying files over the network using Batch File

@ECHO OFF

set /p TerminalName= Enter the PC you wish to relocate ECFs on: 

ECHO Do you wish to relocate the ECFs on %TerminalName% ?

PAUSE

IF NOT EXIST "\\%TerminalName%\c$\Program Files\Google" (

ECHO You don't have Google installed

) ELSE (

ECHO You have Google installed!
ECHO Relocating the ECF Files! Here we go...

    IF EXIST "\\%TerminalName%\c$\Program Files\Google" (

    ECHO The ECF Folder already exists..
    ECHO Moving ECFs now

    cd \\%TerminalName%\c$\Program Files\Google
    FOR %%f IN (*.ecf) DO move /y "%%f" "\\%TerminalName%\c$\Program Files\Google" 


    ) ELSE (

    ECHO No ECF Folder exists...Let's sort that out!
    MKDIR "\\%TerminalName%\c$\Program Files\Google"

    cd \\%TerminalName%\c$\Program Files\Google
    ECHO Moving ECFs now
    FOR %%f IN (*.ecf) DO move /y "%%f" "\\%TerminalName%\c$\Program Files\Google" 
    )

)


ECHO Finished!

PAUSE

Whenever running the above Batch file i get this error: 每当运行上述批处理文件时,都会出现此错误:

CMD does not support UNC paths as current directories. CMD不支持将UNC路径用作当前目录。

Ps - Sorry for the formatting...it won't all go into the grey for code :( Ps-抱歉,格式化...对于代码来说,它不会全部变成灰色:(

Instead of CD , the PUSHD command can be used. 可以使用PUSHD命令代替CD Remember, that internally PUSHD will do a NET USE and consume a "drive letter" from the OS. 请记住,在内部PUSHD将进行NET USE并消耗OS中的“驱动器号”。 If this is done too many times, the system will run out of "drive letters." 如果这样做太多次,系统将用完“驱动器号”。

Remember to POPD at an appropriate time. 请记住在适当的时间进行POPD

UPDATE: Looking at this again, I doubt if you ever need to change directories. 更新:再次查看此内容,我怀疑您是否需要更改目录。 Where do the files exist that are to be MOVE 'd to the new directory? MOVE到新目录的文件在哪里? More analysis needs to be done. 需要做更多的分析。

Here is a batch code which should work for your task: 这是一个适合您的任务的批处理代码:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET /P "TerminalName=Enter the PC you wish to relocate ECFs on: "
ECHO Do you wish to relocate the ECFs on !TerminalName! ?
PAUSE

SET "SourcePath=\\RemoteComputer\\c$\Program Files\Google\ECF_Folder"
SET "TargetPath=\\!TerminalName!\c$\Program Files\Google"

IF NOT EXIST "!TargetPath!" (
    ECHO You don't have Google installed.
    GOTO EndMoveECF
)

ECHO You have Google installed!
ECHO Relocating the ECF files! Here we go...

IF EXIST "%TargetPath%\ECF_Folder" (
    ECHO The ECF folder already exists..
) ELSE (
    ECHO No ECF Folder exists... Let's sort that out!
    MKDIR "%TargetPath%\ECF_Folder"
    IF ERRORLEVEL 1 (
        ECHO Failed to create ECF folder "%TargetPath%\ECF_Folder".
        GOTO EndMoveECF
    )
)
ECHO Moving ECF files now ...
MOVE /Y "%SourcePath%\*" "%TargetPath%\ECF_Folder\"
ECHO Finished!

:EndMoveECF
ENDLOCAL
PAUSE

You need to set SourcePath accordingly. 您需要相应地设置SourcePath And you need to replace all occurrences of ECF_Folder by whatever is right in your environment. 而且,您需要用您环境中合适的任何东西替换所有出现的ECF_Folder

Moving the files is done with command MOVE without switching current working directory as this is not needed. 文件移动是通过命令MOVE完成的,无需切换当前工作目录,因为不需要这样做。 And command MOVE supports also wildcards and therefore no need for a FOR loop. 并且命令MOVE也支持通配符,因此不需要FOR循环。

Delayed environment variable expansion is used partly in case of user of batch file enters an invalid terminal name containing for example a double quote, angle brackets or other characters with a special meaning in batch files. 如果批处理文件的用户在批处理文件中输入无效的终端名称(例如包含双引号,尖括号或其他具有特殊含义的字符),则部分使用延迟的环境变量扩展。 Open a command prompt window and execute in this window set /? 打开命令提示符窗口并在该窗口中执行set /? for details about delayed expansion. 有关延迟扩展的详细信息。

There is once !TerminalName! 曾经有一个!TerminalName! and once !TargetPath! 然后一次!TargetPath! used instead of %TerminalName% and %TargetPath% . 代替%TerminalName%%TargetPath% After existence of Google directory on remote computer is positive verified, it should be safe to reference TargetPath without delayed expansion. 在对远程计算机上的Google目录存在进行了肯定验证之后,应可以安全地引用TargetPath而不会延迟扩展。

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

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