Sunday, August 31, 2014

Folders Updater with Batch

In this post, I present yet another Batch script related to Files and Folders updating but this time with extended functionality. This piece of work was intended to update portable Hard Disk Drives connected to a workstation in a network with a File Server that contains a list of folders with files that are updated and changed oftenly. These hard drives are used by Field Engineers to store important information required for daily field activities. Due to the type of instrument it serves as and the numerous factors affecting the day to day activities of a Field Engineer, it was important for the script to fulfill certain requisites.

First Functionality: Selectable Starting Point

Since said folders contain a collection of large files, the list of folders can take ridiculously long time to fully update (given that much data have been added/updated). Moreover, Field Engineers are often assigned jobs spontaneously, thus requiring them to prepare for the job in a short time span. Therefore, it was imperative that the user can prioritize which folders to update first, depending on the type of job. In light of these, the script has added functionality for the user to be able to choose from which letter to start updating. For example, if the user chooses to start from the letter 'M', then the script will update all folders with names starting with 'M' and onward (in alphabetical order) until the last folder is updated.

Figure 1. Screenshot of CMD at Prompt for Letter
Second Functionality: Auto-Update

Another added key functionality of this script is its ability to auto update itself from the File Server. For instance, a copy of the script must reside in each of the portable Hard Disk Drives. Thus it would be too tedious and redundant to frequently check if there is a newer version in the server and to update the script as necessary. If this functionality had not been there, then this manual process would have been done by whatever amount of users exists. Thankfully, the script is written such that it can look into a location within the File Server and checks whether a newer version exists, then it would update accordingly. This, naturally, saves the users time and work.

New Formatting and Style

Additionally, I will try something new: the code presentation. The code itself will be formatted and presented differently. Hopefully, this will be an improvement over the previously used style and format. At least it is for me; the code formatting is more automatic, because I upload the code to GitHub and let it do the formatting. Throughout the document, I will paste in snippets of code so that it's easier to follow.

So let's get started directly into the code...


Extended File Updater Script: Preparations and Housekeeping

Please keep in mind that the code has been slightly modified to fit into this blog ― not much, only some paths and filenames. So for starters, housekeeping and preparation codes before the real task.


1
@echo off
::Checks if PC is connected to the server. If not exit.
2
3
if NOT EXIST "\\mynetworklocation\mydocuments" exit
4
::Deletes any temp.bat in Z: which could be a local location or removable device.
5
if EXIST "Z:\temp.bat" del /F "Z:\temp.bat"
6
:: Checks the software creation dates.
7
for /F "Delims=" %%I In ('xcopy /DHYL \\where_script_is_located\Generic_Updater.bat Z:\Generic_Updater.bat ^|Findstr /I "File"') Do set /a Newer=%%I 2>Nul
8
9
if %Newer% == 1 goto Update
10
:: T: will be mapped to the folder mydocuments within the server.
11
subst T: "\\mynetworklocation\mydocuments"
12
:: Change directory to our recently mapped T:
13
cd /d T:
14
:: Loops to update the folder tree.
15
for /d %%A in (*) do if NOT EXIST Z:\mydocuments\%%A md "Z:\mydocuments\%%A\1offline_data\"

Here Lines 1-15 are some preparations work before actually executing the main task. Line 1 tells the interpreter not to print out the code to the terminal, while Line 2 checks that a connection to the network resource is possible and the folder is available. Line 5 checks whether the temporary file exists and if so, deletes it. This temporary file, temp.bat, is created by this script to execute additional code that will update the file if there is a newer version in the File Server.

More importantly, Lines 7-9 check if the file residing in the server location is newer or older than the current one in our Hard Drive, which is set to Z:\. As was mentioned, if there is a newer version, then temp.bat is created and executed to update to latest version.



Important: Please notice that I have set all the Hard Drives to be assigned the letter Z:\ in the computers to work with this script. This script assumes the Hard Drives to be in Z:.

Now that we have checked preconditions, we map our network location to T: (Line11-13) and check if all the folders present in T:\ are also in our Hard Drives. If this is not the case, then create said folders (Line 15).

Extended File Updater Script: Some Functions

This part consists of codes written as functions. The first function, Line 17-22, prompts the user for the letter from which he or she wishes to start updating.This function actually makes a call in Line 21 to another function― :toUpper which covers Line 24-35. This small function, taken from the mentioned source, converts lowercase character to uppercase to avoid discrepancies.

16
::Added functionality to ask for which letter to start.
set input = a
17
18
set /P input=Please enter which letter you want to start updating:
19
set letter=%input:~0,1%
20
echo %letter%
21
call :toUpper letter
22
goto F1
23
24
:toUpper str -- converts lowercase character to uppercase
25
::           -- str [in,out] - valref of string variable to be converted
26
:$created 20060101 :$changed 20080219 :$categories StringManipulation
27
:$source http://www.dostips.com
28
if not defined %~1 EXIT /b
29
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
30
            "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
31
            "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
32
            "ö=Ö" "ü=Ü") do (
33
    call set %~1=%%%~1:%%~a%%
34
)
35
EXIT /b
36
:F1





















The following snippet Line 39-43 is part of the main body with a very specific task: to loop through and update the folders in our Hard Drive, Z:\. Then Line 44 serves as flow control and redirects to the next line of code it should execute.

38
:: Loops through all folders inside Foreigner and update the folders in Z: (hard drive)
39
setlocal EnableDelayedExpansion
40
for /D %%C in (*) do (
41
        CALL :FUNC1 "%%C"
42
)
43
Endlocal
44
GOTO FOLLOW1








Notice that this piece of code makes call to :FUNC1, which is presented in Lines 47-52, below.

45
::This function will extract the first letter and compare it against the letter input by the user. Then update the folders with that starting letter.
46
47
:FUNC1
48
set "theword=%~1"
49
set theletter=!theword:~0,1!
50
echo %theletter%
51
if "%theletter%" GEQ "%letter%" robocopy /Z /E /MIR "%~1\1offline_data" "z:\mydocuments\%~1\1offline_data"
52
EXIT /b








As the comments describe, the above code checks if the letter entered by the user is "greater or equal to" the first letter of current folder's name. If it is not, then skip it and continue until we find folders with names that fulfill such condition.

Extended File Updater Script: Some Final Checks

We also have to make sure that folders deleted in our File Server has to be reflected in our Hard Drives. For instance, if the administrator deletes a folder in the File Server, then the script would not know that this change took place. So the following code takes care of that along with other finishing tasks.

54
:: Loops through HDD to check if Server has all the folders inside HDD mydocuments. If not, then it has been changed/deleted and should be removed from HDD as well.
55
56
:FOLLOW1
57
Z:
58
cd mydocuments
59
for /d %%B in (*) do if NOT EXIST T:\%%B rd /Q /S "Z:\mydocuments\%%B\"
60
:: Free up the mapping.
61
cd /d C:
62
subst T: /d
63
:: We are done at this point.
64
echo Update performed succesfully... Now program will exit...
65
Pause
66
Exit













At this point you might ask, where is the code for the temp.bat that actually updates the script from the server? Well, here below. Remember that this piece of code is executed only if the condition in Line 9 is true. In other words, %Newer% == 1.

67
:Update
68
::If File in server is newer, create a separate batch file to update the script and reexecute.
69
echo start /wait robocopy \\where_script_is_located\ Z:\ Generic_Updater.bat>Z:\temp.txt
70
echo attrib -h Z:\Generic_Updater.bat>>Z:\temp.txt
71
echo touch -d "+1 minutes" Z:\Generic_Updater.bat>>Z:\temp.txt
72
echo attrib +h Z:\Generic_Updater.bat>>Z:\temp.txt
73
echo Z:\Generic_Updater.bat>>Z:\temp.txt
74
Z:
75
ren temp.txt temp.bat
76
::Execute the newly written batch file.
77
Z:\temp.bat
78
Pause













In short, this code basically creates a file named temp.bat in Z:\ (our Hard Drive) with code that will copy the newer script from the File Server and paste it in our Hard Drive replacing the old script file. Finally, temp.bat is executed and at the very end, it executes our main script (Generic_Updater.batagain as shown in Line 73.

Notice: The Echo and the ">>" actually redirect text into whatever is to the right of >>. For example, Line 70 actually writes attrib -h Z:\Generic_Updater.bat into the textfile temp.txt. This is later renamed to temp.bat in Line 75.

Products Recommendations


If you think this might be a method you could use to backup files, then I suggest you get a reliable portable hard drive. I personally have a Seagate and a Western Digital and have found them to be quite reliable (5+ years old). You could check this Seagate Backup Plus Slim 2TB Portable External Hard Drive USB 3.0 or this other WD 1TB Elements Portable External Hard Drive - USB 3.0.

If you wanted to use something a bit more compact or start off with a less expensive device, USB drives could actually work as well. I have personally had SanDisk's USB drives  and never had any issue with it. Check out SanDisk Ultra 32GB USB 3.0 Flash Drive if you are interested.

And Concluding...

As usual, I am not responsible for any damage that this script may cause. So from here you can Copy/Paste all the snippets and place them in the order described by the line numbers. Numbers that are skipped are empty lines, so don't panic. This long piece of work exercises many useful concepts that can be used in other scripting projects. Unfortunately, you will have to understand the code fairly well in order to be able to adapt it to your environment. The way this is written is not particularly aimed to be flexible to change into other working environment. Nonetheless, you can always ask me. See you till next one!!!

Important: You use this at your own risk as I will not be responsible for any damage you may cause.