简体   繁体   中英

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.

Ps - Sorry for the formatting...it won't all go into the grey for code :(

Instead of CD , the PUSHD command can be used. Remember, that internally PUSHD will do a NET USE and consume a "drive letter" from the OS. If this is done too many times, the system will run out of "drive letters."

Remember to POPD at an appropriate time.

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? 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. And you need to replace all occurrences of ECF_Folder by whatever is right in your environment.

Moving the files is done with command MOVE without switching current working directory as this is not needed. And command MOVE supports also wildcards and therefore no need for a FOR loop.

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 /? for details about delayed expansion.

There is once !TerminalName! and once !TargetPath! used instead of %TerminalName% and %TargetPath% . After existence of Google directory on remote computer is positive verified, it should be safe to reference TargetPath without delayed expansion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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