IoT 6: Use Command Prompt in Windows

Last Updated on December 7, 2020

When backuping my data, we have the several steps, insert the media to your PC, click & drop files, wait a time, confirm the backuped media, and eject it. It's so costful on spending your working time. Although Windows offers the backup tool, I'm using Command Prompt.


* These processes only backup files, but not the system. If you want backup overall in your PC, use the backup tool in Windows.


1. Preparation: Insert the media to your PC (I assume the target drive letter is "F").


2. In the search box in Windows 10, type "cmd". Run Command Prompt.


3. Type as follows.


ROBOCOPY C:\Users\%Username%\Documents\source F:\target /MIR /XJ /J /X /ETA /NDL


Make sure to understand the detail of options, use the help option.


ROBOCOPY /?


Note that %Username% is an environmental variable. You can define one of environmental variables and it's useful on your batch files.


@ECHO OFF

:: Author: Kenta Ishii

:: Differential Backup

:: Open Notepad (command "notepad" on Command Prompt), save as "backup.bat", and click the batch file.

SET SRC=C:\Users\%Username%\Documents\source

SET DST=F:\target

SET OPTION=/MIR /XJ /J /X /ETA /NDL

ROBOCOPY %SRC% %DST% %OPTION%

:: Get a new line and bye.

ECHO.

ECHO -----------------------Bye!---------------------------

:: Stop to close Command Prompt after the end of the batch commands.

PAUSE


ROBOCOPY can delete files in the target directory. Whereas, XCOPY can't delete files in the target directory, and this command is useful on addition to a folder.


@ECHO OFF

:: Author: Kenta Ishii

:: Copy contents in "update" directory (folder) in Desktop directory to the target directory.

:: XCOPY can't delete files in the target directory.

:: XCOPY /?

:: Batch commands are not case-sensitive.

SET SRC=C:\Users\%Username%\Desktop\update

ECHO Files in '%SRC%' will be copied to the target directory.

SET OPTION=/E /W /I

SET DST=

SET /P DST=Target directory:

IF '%DST%'=='' GOTO end

ECHO The target directory is '%DST%'.

SET isOK=

SET /P isOK=Type any to go copy:

IF '%isOK%'=='' GOTO end

XCOPY %SRC% %DST% %OPTION%

ECHO ---------------------List of Directories and Files in '%DST%'------------------------

::%DST_VOLUME%:

::CD %DST_DIR%

::DIR /O:D

DIR %DST% /O:D

ECHO ------------------End of List of Directories and Files in '%DST%'--------------------

ECHO.

ECHO -------------------------List of Updates Today in '%DST%'----------------------------

ECHO Wait for process...

FORFILES /P %DST% /S /D +0 /C "CMD /C IF @ISDIR==FALSE (ECHO @PATH is modified on @FDATE @FTIME.) ELSE (ECHO @PATH is a directory created on @FDATE @FTIME.)"

ECHO ----------------------End of List of Updates Today in '%DST%'------------------------

ECHO.

:end

ECHO ----------------------------------------Bye!-----------------------------------------

ECHO.

PAUSE


Note that commented 3 lines at "List of Directories and Files in '%DST%'" show commands in another method. CD (change directory) can't cross over the drive. Therefore, you need to command the volume letter before CD. However, in the actual commands, I don't distinguish between the volume and the directory for the target. DIR shows the list crossing over the drive by indicating the path. The "/O:D" option for DIR is to sort the output, and the order for sorting is by date/time, oldest first. FORFILES searches files and executes a task for searched files. CMD is just Command Prompt, and you can see the inner commands which are amazingly constructed like concurrent computer languages.


The first question in the commands types the path for the target. You can copy the address in File Explorer. To select the absolute path of a folder, left click the space of the address bar at the top in File Explorer. Then right click and copy, or Ctrl+c. Reselect the window of Command Prompt by left click, and Ctrl+v or right click to paste the path to the line in the window.


Task Scheduler is useful to schedule backuping. However, I don't use it because I shutdown the PC frequently in a day. It's why I use batch files by clicking.


* Event Viewer have the GUI that resembles the one of Task Scheduler, and it's useful to check error logs in Windows. If you feel any odd in your usage of the OS, you can check error logs before restoring the OS.


Command Prompt can be lunched from File Explorer. Left click the space of the address bar, type "cmd", and press Enter key. This is useful to open Command Prompt at a specific directory.


The output messages can be routed to a text file. For example in Command Prompt:


backup.bat > log.txt


In this case above, you need to press Enter key on Command Prompt once because the last line has "PAUSE". If you command "SET /P" for any input, you need to type on Command Prompt, but any prompting message doesn't show because all messages are routed to the text file. Other actions on Command Prompt may be needed without any message depending on commands or executable files.


Note that in the command line of Linux, rsync is useful to backup.


# Backup Contents in SRC Folder to DST Folder

rsync -rvit --delete /SRC/ /DST/


You may wonder how about ejection of your media. Ejection of the media on command prompt is needed to be bypassed through a PowerShell script. Besides, the PowerShell script doesn't guarantee to complete ejection. PowerShell is developed after Windows is released, i.e., the script for substituting actions in GUI. Whereas, Command Prompt is made for the compatibility with MS-DOS in Windows. I think Command Prompt is stronger than PowerShell in view of accessing the system. For example, DISKPART and MOUNTVOL in Command Prompt can mount or unmount drives (the hardware to drive a disk storage). So, Command Prompt is just like another system in Windows. However, the development of Command Prompt has almost done. Nowadays, many media, such as USB and SSD, include drives with storages. In a decade ago, disk storages were popular and a drive for disk storages was fixed in the PC. DISKPART and MOUNTVOl assume disk storages, but not USB/SSD media. If you use these command to eject the modern media, the action becomes force-ejection.


Batch commands are very good on writing and its speed. It's like BASIC, but many things can be done in the commands. If you write codes for using in a batch file, you may find that it's simple rather than the usage with GUI. Coding is very fun. I used to code several languages in my hobby. In physical, sports, and health science, however, coding with your PC for long hours takes your opportunity to have time for regular physical activities to maintain your body. If you code, make sure to limit hours for coding and have time for physical activities for your future.


* The 2020 General Election in the U.S. has an outcome. Media for both sides seem to be shocked and reporting irrelevantly. Checking elections for House, Senate, and President; and comparing the difference of electoral systems for the three, I can say a hypothesis. The motivation in organizations at urban areas made the result for the presidency, and these organizations kept neutrality before the 2020 election. Economists seem to tell this election from Benford's law that is used to find out accounting fraud, but I think this election has validity because of voting blocs. For example, the New England Journal of Medicine publicly addressed to members about voting against the incumbent in the 2020 election. In my opinion, I'm sorry for popular President who will depart from White House. One of his promises doesn't seem to be achieved. The promise is to rebuild infrastructures in the U.S. I should tell that the rebuilding of infrastructures takes over 4 years, and the action now gets fully into progress, needing a lot of people to work. The work is tougher than coding in front of your PC, and needs physical power to be worked out. I may have the steady flow of Mississippi river close to my eyes in the future because I play blues guitar.


* In addition, organizations kept neutrality may also include public services. The union for postal service workers endorsed a candidate for the 2020 election amid restructuring the service by the federal government. Although, the union is basically for political actions. Note that the postal service is a significant public service because someone can transmit a fact to another by this service through a sealed letter. Political sociology can't claim about the legitimacy of any regime, and its study for the U.S. would be delayed. Participation in politics by public services has been considered of its powerful ability to affect the politics. Members of public services have voting rights, and nobody should criticize their actions. However, the democracy can be functioned through feedbacking all voices of people, and listening biased voices causes the class conflict. I believe that the political system of the U.S. has been functioned for the democracy, and it will be stable as well as today. As a tool, recent politicians tends to approach to members of public services. It's noticeable because the approach was found in several eras of nations, and it used to be declined by any method or any opportunity. The 2020 election is held for three types of powers that are elected by different methods. The difference now guarantees the legitimacy of the election even if any action occurs from any group, and the system for the democracy that is planned in the past can be functioned well.

Please free to contact me by E-mail if you have any opinion or comment on this site.
Link: E-mail Adress
Push the "Search!" button to show results. How to search for a word?