A DOS Command To Map A Network Drive

Just a quick post with DOS commands to map a network drive connecting as a specific user and logging the results.

echo Start %date% %time% >> D:\logs\drivemap.txt
net use z: /delete /yes >> D:\logs\drivemap.txt
echo Mid %time% >> D:\logs\drivemap.txt
net use z: \\10.10.10.10\INVOICEFILES /user:USERNAME PASSWORD /persistent:yes >> D:\logs\drivemap.txt
dir z: >> D:\logs\drivemap.txt
echo End %time% >> D:\logs\drivemap.txt

This is something we did for a project a while backĀ and want to keep a note for future.

Compare Files In Folders And Copy If File Size Is Different

The Problem
I have a remote folder containing nearly 8000 images and a local folder containing just over 300 images.
Rather than performing a blank copy of files from the local to remote folder I needed a way to check if the file exists in the remote folder and take a different action:

  • If a file exists in the local folder but not the remote folder, the file should be copied to the remote folder.
  • If a file of the same name does already exist in the remote folder I only wanted to copy the file if the new file in the local folder was smaller in size.

I started on Google searching for some way of conditionally copy files but most of the results described features of an application that would need to be installed.
Although these applications are free, installing an application is not an option as I do not have administrator level permissions, corporate security and all that.
My next option was to find some clever DOS commands and so I returned to Google.

Possible Solution

A possible solution was found in this post on stackoverflow by (and full credit to) mojo. Below is the solution mojo posted.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET REMOTE_DIR="REMOTE_FOLDER_PATH"
SET LOCAL_DIR="LOCAL_FOLDER_PATH"

FOR /F %%f IN (files_to_compare.txt) DO (
    SET "REMOTE_FILE=%REMOTE_DIR%\%%~nxf"
    SET "LOCAL_FILE=%LOCAL_DIR%\%%~nxf"
    FOR %%l IN (!LOCAL_FILE!) DO (
        @ECHO [LOCAL] %%~nxl: %%~zl
        FOR %%r IN (!REMOTE_FILE!) DO (
            @ECHO [REMOTE] %%~nxr: %%~zr
            IF %%~zr GTR %%~zl (
                @ECHO %%~nxr: Remote file IS larger [%%~zr] ^> [%%~zl]
                COPY /Y "%%~r" "%%~l"
            ) ELSE (
                @ECHO %%~nxr: Remote file is not larger [%%~zr] ^<= [%%~zl]
            )
        )
    )
)

Working Version

To get a working solution

1.) Copy the above code (provided by mojo) to a file named files.bat
2.) Specify the remote and local folder paths labelled REMOTE_FOLDER_PATH and REMOTE_FOLDER_PATH
3.) Create a text file called files_to_compare.txt containing the file names in the local folder, one filename per line.

Creating the list of local files in the files_to_compare.txt file can be done by changing to the local folder in a dos command window (using the cd command) and running the following. The /b switch suppresses the heading or summary information from being included. The > (greater than) redirects the output of the command to a destination specified in this case as a file rather than directing the output to the command window. In my case the text file was being created one level above the local folder so I could use a relative path to specify the desired location of the text file.

dir /b > ..\files_to_compare.txt

4.) Run the batch file and reveiw the result

Run the batch file and direct the ouptut to a text file.

files > results.txt

The results.txt file now contains the results of the comparison.

If you want to perform a test run without copying any files add an @ECHO instruction before the Copy command on line 16.

@ECHO COPY /Y "%%~r" "%%~l"

Reviewing the results file will highlight which files will be copied.

Extending the Solution
What the script is actually doing is this:

For each file in the files_to_compare file
Look for the file in the local folder and get its details
Look for the file in the remote folder and get its details
If the size of the remote folder is greater than the size of the local folder
Copy the file from the local folder to the remote folder
Otherwise
Do nothing because the file in the remote folder is smaller

The script is currently set to check of the size of the file in the remote folder is greater than the file in the local folder. This can be seen on line 14

IF %%~zr GTR %%~zl (

It may be that you do not want the comparison to be greater than, here are some alternatives
EQU – equal
NEQ – not equal
LSS – less than
LEQ – less than or equal
GTR – greater than
GEQ – greater than or equal

On the whole this solution worked for what I needed on this occasion and I can see will be useful in the future.