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

时间:2015-05-25 14:18:23

标签: batch-file networking cmd xcopy

@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

每当运行上面的批处理文件时,我都会收到此错误:

CMD不支持UNC路径作为当前目录。

Ps - 对不起格式化...代码不会全部变为灰色:(

2 个答案:

答案 0 :(得分:0)

可以使用CD命令而不是PUSHD。请记住,内部PUSHD将执行NET USE并使用操作系统中的“驱动器号”。如果这样做太多次,系统将耗尽“驱动器号”。

请在适当的时候记住POPD

更新:再看一遍,我怀疑你是否需要更改目录。文件存在于哪些位置MOVE到新目录?需要进行更多分析。

答案 1 :(得分:0)

以下是适用于您的任务的批处理代码:

@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

您需要相应地设置SourcePath。而且您需要用环境中的任何内容替换所有出现的ECF_Folder

使用 MOVE 命令移动文件,而不切换当前工作目录,因为这不需要。命令 MOVE 也支持通配符,因此不需要 FOR 循环。

如果批处理文件的用户输入的无效终端名称包含例如双引号,尖括号或其他在批处理文件中具有特殊含义的字符,则部分使用延迟环境变量扩展。打开命令提示符窗口并在此窗口set /?中执行,以获取有关延迟扩展的详细信息。

使用!TerminalName!一次!TargetPath!代替%TerminalName%%TargetPath%。在远程计算机上存在Google目录后,如果没有延迟扩展,则可以安全地引用TargetPath