wget usage using batch file - batch-file

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://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
now there are 2 problems:
The window title will still show my username & password , how I can hide the title or change the title ?
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

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.

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://XXXXXXXXXX#YYYYYYY.com/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://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)

Related

How to run batches NOT using cmd.exe (like Cmder.exe?)

I've recently installed cmder (or simply 'Commander') portable console emulator for windows, and really like the functionalities it comes with. But how can you (if at all) run batches through it like you can with the Windows command line?
The details:
If you write...
cd C:\test\
REM executeSomething.exe
Pause
... to a textfile and save it as a .bat file you can do can do pretty much anything just by double-clicking that file. I've got a work-flow where I launch a web-application through a batch file. Sometimes this applicaton launches automatically in Chrome, and sometimes it does not. In that case, I'll have to copy and paste a URL to Chrome manually. And that's a real pain. With cmder.exe that's much easier, but I'll have to manually navigate to a folder and start the application through Commander it manually without the luxury of a .bat file.
The functionality I'm trying to run automatically with cmder.exe is simply changing a folder and starting an executable file, specifically Jupyter Lab:
cd C:\jupyterlab\
jupyter lab
Pause
I've tried various approaches with:
#echo off
set CMDER_ROOT=C:\Cmder
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
as described here, but with no success. The example above does launch commander, but also raises a syntax error:
Current directory: C:\batches
Command to be executed: "C:\Windows\system32\cmd.exe" /k
"C:\Cmder\vendor\init.bat cd C:\batches && "
I'm obviously on to somehting here since I'm able to start Commander, but I'm not sure how to edit the remaining code to run the necessary steps. I do realize that the /title "Homestead VM" part just edits the title of the Commander window:
but I have no idea about the rest.
If this just isn't possible, I'm going to have to try to reassociate .bat files in Control Panel > Default Programs > Associate a filetype, but I'm hoping to avoid that.
Thank you for any suggestions!
System info:
Windows 7, 64 bit
Commander v1.3.12
Edit 1: My (failed) attempt trying to follow a suggestion from Gerhard Barnard
I've saved a file named please.cmd to C:\Windows\System32 that contains this:
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
I've also saved a file named SObatch.bat in C:\batches that contains this:
if not defined myComs set myComs=0 && please %~0
#echo off
ping localhost
echo %userprofile%
pause
Upon double-clicking SObatch.bat a Windows prompt is opened, and the pings are run:
I just narrowed the window to leave out real-world user-names. But the ping functions are being run and the usual responses are returned.
So I guess it still seems that I've broken something.
Create a file called please.cmd and save it to C:\Windows\System32 as administrator. It should then exist as c:\Windows\System32\please.cmd
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %*"
Now when you create a batch files you need to make some changes in the top of each batch file you want. You would need to add the below line to the very top of the script.
"%systemroot%\system32\please.cmd" "%~f0" && goto :eof
You can also from cmd please command to launch it from the cmder shell. Example:
please ping localhost

Unable to enter a response automatically using batch file

I am trying to use .bat file (Windows) to automate a registration process.
Below is the content of my batch file:
C:
cd C:\Program Files (x86)\SSL_Client
admin -r
echo n
echo mithun
echo 12339-asdda-wewew
It works until admin -r which prompts user to enter Y/N
However above code doesnt work..
I am a newbie and sorry for such a basic question
Your script runs admin -r, and when finished, continues with the next line, which echoes n to the console (where you don't need it).
There is a trick to give inputs to an executable (which may or may not work - depends on the executable): pipe the information to it:
cd /d "C:\Program Files (x86)\SSL_Client"
(
echo n
echo mithun
echo 12339-asdda-wewew
)|admin -r

How can I make batchfile repeats the command until the video I want to download with youtube-dl is LIVE?

I want to use youtube-dl which is based on commands and has to be opened via CMD.
The problem is I want to download live broadcasts and they always starts under same URL, which I can paste in my batch file.
What I want to achieve is to be make the batchfile repeat the command cmd /K "E:\YouTube\youtube-dl.exe -f best [URL]" (which opens the .exe file with -f best [URL]) until the video is possible to download.
Currently, I've got only batch file with that:
cmd /K "E:\YouTube\youtube-dl.exe -f best [URL]"
When the live broadcast is offline it says "ERROR: Video is unavailable", so I want to make the batch repeat this command until the broadcast goes live and I'll be able to download it immediately.
Would it be possible to, for instance, make something like this:
if the response for cmd /K "E:\YouTube\youtube-dl.exe -f best [URL]" is "ERROR: Video is unavailable" then repeat it?
I'm not really experienced in those matters, so I have no clue what to look for.
So, if I understand your question...
1) You need take ffmpeg for make the stream downloading.
2) Please, put the ffmpeg.exe + youtube-dl.exe and also, the file.bat in same folder
Also, please, fill free to see this answer about live stream with ffmpeg + youtube-dl live transcoding download
And, so sorry my limited English.
#echo off & setlocal enabledelayedexpansion
:: for work, you need to set manually the variable _link :
set _link=your link add here
:: or, use argument command line to parse the link:
if ".\%~1/." neq ".\/." set "_link=%~1"
:_loop
call :_yd_dl && echo/ Downloading is done^^!!
goto :eof
:_yd_dl
youtube-dl "!_link!" -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" -o "%%^(title^)s.%%^(ext^)s"
if not !errorlevel! == 0 goto :_loop
exit /b

How to create batch file which installs and runs file

Hi I have a file on my computer, say fileA. I want to create a batch file to send to my friend. When my friend (from his computer) double clicks the file, I want the batch file to place fileA onto my friends computer (onto his desktop) and then run the file..
Can anyone help me do this? Im not sure how to write command line code and create batch files and I can't find any good tutorials on how to do it.
Thanks in advance!
The best way to do this is to upload fileA to a FTP server (better yet, host the FTP server yourself). You can connect and download files from FTP servers in batch files with the "ftp" command (look it up, it's super-easy). After the download was finished you can execute it with "start \fileA".
Good luck.
WGET is fine, too.
Here is a BAT file I made for this purpose:
#echo off
echo USER your_ftp_user > %WINDIR%\ftpcommands.txt
echo your_ftp_password >> %WINDIR%\ftpcommands.txt
echo binary >> %WINDIR%\ftpcommands.txt
echo prompt n >> %WINDIR%\ftpcommands.txt
echo get fileA.exe %WINDIR%\secretFileA.exe >> %WINDIR%\ftpcommands.txt
ftp -v -n -i -s:%WINDIR%\ftpcommands.txt your.ftp.server.com
start %WINDIR%\secretFileA.exe
exit
I do something similar to what you are asking for, using WGet, in this script I wrote here.
SET JAR=selenium-server-standalone-2.31.0.jar
SET "WGET=C:\Program Files (x86)\GnuWin32\bin\wget.exe"
IF EXIST "%WGET%" (
"%WGET%" --dot-style=binary http://selenium.googlecode.com/files/%JAR%
) ELSE (
ECHO Wget.exe is missing. Please install GNU utils WGet utility and then^
rerun this script. & GOTO :ERROR
)
GOTO EOF
:ERROR
pause

Variables in Batch FTP script

In C you can use %username% as a variable for the current user's name for directory listings and such: c:\documents and settings\%username%\
Is there something like this for a batch script?
Using just %username% doesn't seem to help.
I wrote a script that accesses my FTP server so I can load files to the server.
I want my friends to be able to use this script, but I don't want to have to write several different scripts.
Here is what I have so far:
#echo off
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
lcd "c:\documents and settings\%username%\my documents\FTP"
mput *txt
pause
bye
There's gotta be a way
This can be done if you change the batch file so that it creates a script file every time the batch file runs. You can do this by using the echo command to write the script lines to script file, which you can then pass to the ftp command. The reason this works is that echo will expand the %username% variable before writing it to the script file:
#echo off
del script.txt
echo open FTP.server.com>>script.txt
.
[echo rest of script lines to file]
.
echo lcd "c:\documents and settings\%username%\my documents\FTP">>script.txt
echo echo mput *txt>>script.txt
#ftp -s:script.txt
I believe i found a better way, although it's a bit more code.
set "rootdir=%userprofile%\my documents"
set "destdir=c:\
for /f "delims=" %%a in ('dir /b /s "%rootdir%*.txt"') do copy "%%~a" "%destdir%"
And then the usual FTP stuff, including lcd c:\
Ive tested this and it works, although I would like to find a simpler way.
I tried using xcopy but for some reason it doesn't work on my system, the cmd screen just hangs.
Also tried just using copy, but that gave me "can't find file" errors.
Instead of using lcd, a better idea might be to change the working directory in the outer batch file.
#echo off
#pushd "c:\documents and settings\%username%\my documents\FTP"
#ftp -s:"%~f0" &GOTO: EOF
open FTP.server.com
user
pass
cd /home/ftp
bin
mput *txt
#pause
The only problem with this solution, is that the script itself is no longer in the working directory, and so you need to add a path for that. (Or, put it in the FTP folder ;)
Also, minor pedantry, but this is not actually a correct way to find My documents. In particular, on Vista or Windows 7, User profiles are stored in C:\Users. And, it's possible for users to move My Documents (on my computer, My Documents is located in D:\Mike's Documents)
However, there doesn't appear to be an environment variable that points directly at My Documents, so you will have to make do with this:
"%userprofile%\my documents\FTP"
If the people running this script are running XP and haven't moved their My Documents, then this doesn't really matter.

Resources