wget usage using batch file

17,432

Solution 1

I agree with PA that if the file is sensitive then hiding the password in the console output won't do much in terms of security. However, to answer the OP's question:

1. The title command is capable of changing the current window's title.

title My CMD Window

If the problem you're encountering is that a popup window for the wget command is coming up and displaying the username and password there, you can use the following. I apologize, I am not familiar with wget so I'm not sure on its behavior, hopefully this helps.

start "My WGET Window" wget -N ftp://[email protected]/file.jpg >nul 2>&1

2. A couple of checks before and after will be beneficial. First, check if the file exists and don't even bother with the wget if it does already exists (assuming from the wording of your post that you do not want to overwrite the file).

if exist file.jpg echo File already exists!&pause&goto :EOF
:: run wget here
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)

We don't need to check for all three conditions as the file either exists before hand (in which case the batch exits) or it does not in which case we test for a successful download. Note that it will be difficult to check for a partial file.

So, putting it all together:

@echo off
blah blah
.....
title My CMD Window
if exist file.jpg echo File already exists!&pause&goto :EOF
echo please wait...
start "My WGET Window" wget -N ftp://[email protected]/file.jpg >nul 2>&1
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)

Solution 2

Wget has built in switches that will prevent a download if the file already exists. It has switches to do most of what you want so you won't have to put the "if" statements in the batch file at all.

wget has an extensive list of switches. Really it's dev has thought of just about everything. Read those docs, if I remember they are about 150-200 pages long.

Share:
17,432
Leo92
Author by

Leo92

Updated on June 08, 2022

Comments

  • Leo92
    Leo92 almost 2 years

    I am trying to download a file using wget in batch file,I don't want to download the file if the file all ready exist and it didn't change so I am using -N also I am downloading the file from my personal FTP server so I want to hide my username and password details so I decided to hide output using >nul 2>&1 so my batch file is:

    @echo off
    blah blah
    .....
    echo please wait...
    wget -N ftp://[email protected]/file.jpg >nul 2>&1 
    

    now there are 2 problems:

    1. The window title will still show my username & password , how I can hide the title or change the title ?

    2. the user wont know if the operation was successful (download was done) or fail (no Internet or no file exist) or it didn't download because the file already exist , I wonder if I can make 3 IF STATEMENTS

      IF file was downloaded then echo file download
      
      IF file wasn't downloaded then echo error
      
      IF file wasn't downloaded because was the same then echo file didnt change
      
  • Derek
    Derek almost 11 years
    Part of the OP's question was feedback regarding results of the wget. I have to admit unfamiliarity with wget, it may provide feedback on what exactly it did, but by checking the file before and after running wget you set yourself up to accomplish the correct action for your particular application in each situation.
  • Jorge Bellon
    Jorge Bellon over 7 years
    Consider using SFTP and a pair of keys to authenticate. That way you dont need to put the password in plain text. See stackoverflow.com/questions/14031773/…
  • Derek
    Derek about 7 years
    I agree, using SFTP and not storing your password in the batch file is by far a better way to go. However, from your link it seems like there is some complication in providing the key to the end users as well. Assuming that problem is not insurmountable the extra security is definitely worth it.