auto schedule upload using batch file and command prompt - batch-file

I am trying to use a cmd file to run a batch file that uploads a file of mine to a directory of my website. The batch file looks like this:
open ip address
username
password
option confirm off
cd \public_html\wp-content\uploads\2014\07
put "C:\Users\a\Documents\pdf\Contact Info.pdf"
quit
The cmd file looks like this:
ftp -s:c:\Users\a\Documents\upload.bat
When I run the cmd file I get output in command prompt that says that my username and password are ok and then tells me that option confirm off is an invalid command and that the cd filepath is a prohibited file name. It then says my file is uploaded but I can't find where the file is put. Is there a reason why this is happening? Is it possible to upload the file to that directory?

Use winscp. Download: http://winscp.net/eng/download.php
option confirm off is a valid WinSCP command, see http://winscp.net/eng/docs/scriptcommand_option
Example: Uploading a single file with WinSCP
http://winscp.net/eng/docs/script_upload_single_file
Step 1
create file: winscp-upload-script.txt
option batch abort
option confirm off
open sftp://username:password#example.com/
put "C:\Users\a\Documents\pdf\Contact Info.pdf" \public_html\wp-content\uploads\2014\07
exit
Step 2
create file: do-upload.bat
"C:\Program Files (x86)\WinSCP\winscp.com" /script=winscp-upload-script.txt
Step 3
Go scheduler, create new task, like described here (http://www.sevenforums.com/tutorials/12444-task-scheduler-create-new-task.html) and use do-upload.bat and configure event timing.
-Done-

Related

Why does my VBScript function differently if it is opened by a batch script rather than a person?

Simply put, I have a VBScript titled "tyrian_soundtest.vbs" that plays an .mp3 that is titled "tyrian_soundtest.mp3"
The VBScript code is below
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "tyrian_soundtest.mp3"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 1
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000
When opened, it plays the .mp3. Simple enough.
The trouble comes in when I run a batch script titled "tyrian_soundtest.bat". Relative to it, the .vbs and .mp3 are in a folder called sfx. Here is what one iteration of that file contained.
#echo off
start %cd%\sfx\tyrian_soundtest.vbs
exit /b
The result is an error stating that Windows couldn't find the file path, likely due to it containing a space. Other attempts of the .bat were replacing line 2 with
start .\sfx\tyrian_soundtest.vbs
or
start "%cd%\sfx\tyrian_soundtest.vbs"
Any attempt I've made gives one of three results. Option 1: There is no error, but the audio simply never plays. Option 2: An error is thrown about the file directory not being found. Option 3: That file path opens up in a new cmd window, but the .vbs is never run.
Is there any way format the .bat to get the .vbs to run through the without an error being caused?
The main issue is that there is used in batch file and in the VBScript file the current directory path. The current directory on starting %SystemRoot%\System32\cmd.exe to process the batch file can be any directory.
The Windows Explorer sets the directory of the batch file as current directory on double clicking the batch file resulting in starting cmd.exe by explorer.exe to process the double clicked batch file of which full qualified file name is passed to cmd.exe after the option /c as additional argument. But if the batch file is stored on a network resource accessed using UNC path, the Windows command processor changes the current directory from the network resource to %SystemRoot% (the Windows directory) and the batch file fails to start Windows Script Host to process the VBS file. See also: CMD does not support UNC paths as current directories.
A batch file can be also started by right clicking on it and using Run as administrator. This can result in making the directory %SystemRoot%\System32 (the Windows system directory) the current directory. See also: Why does 'Run as administrator' change (sometimes) batch file's current directory?
That are just two of many examples where the current directory is different to the directory containing the batch file. So if a batch file references other files stored in same directory as the batch file itself or a subdirectory of the batch files directory, it is advisable to reference these files with the full path of the batch file instead of using a path relative to current directory.
Example:
The used files are stored in directory C:\Temp\Development & Test as follows:
sfx
tyrian_soundtest.vbs
tyrian_soundtest.mp3
tyrian_soundtest.bat
A user opens a command prompt window which usually results in making the directory referenced with %USERPROFILE% or with %HOMEDRIVE%%HOMEPATH% the current directory. A user executes next in the command prompt window:
"C:\Temp\Development & Test\tyrian_soundtest.bat"
So the current directory is definitely not the directory containing the batch file.
The batch file can be coded as follows to start nevertheless the Windows Script Host for processing the VBS file tyrian_soundtest.vbs and successfully play the MP3 file tyrian_soundtest.mp3.
#start "" /D "%~dp0sfx" %SystemRoot%\System32\wscript.exe //NoLogo "%~dp0sfx\tyrian_soundtest.vbs"
%~dp0 references the drive and path of argument 0 which is the full path of the currently processed batch file. The batch file path referenced with %~dp0 always ends with a backslash. For that reason the concatenation of %~dp0 with a file/folder name or wildcard pattern should be always done without using an additional backslash as that would result in two \ in series in complete argument string and the Windows file management would need to fix that small error by replacing \\ by just \ before passing the argument string to the file system. See also the Microsoft documentation about Naming Files, Paths, and Namespaces which explains which automatic corrections are usually applied on file/folder strings before passing them to the file system.
The internal command START of cmd.exe interprets the first double quoted argument string as title string for the console window as it can be seen on running in a command prompt window start /? and reading the output help. For that reason it is not enough to use just:
#start "%~dp0sfx\tyrian_soundtest.vbs"
That would result in starting one more command process with its own console window with the full qualified file name of the VBS file as title of the console window.
The Windows Script Host processing a VBS file by default on Windows exists in two versions:
%SystemRoot%\System32\cscript.exe is the console version.
%SystemRoot%\System32\wscript.exe is the Windows GUI version.
The usage of the console version cscript.exe results usually in opening a console window by the parent process if the parent process is not itself a console application running already with an opened console window like on execution of a batch file processed by %SystemRoot%\System32\cmd.exe being also a console application.
The usage of the Windows GUI version wscript.exe results in no opening of a window by default at all. The processed script file must contain commands to open a window if that is wanted at all.
The difference can be also seen on running from within a command prompt window cscript /? and next wscript /?. The first command results in printing the help for the command line options of Windows Script Host in already opened command prompt window while the second command results in showing a graphic window by wscript.exe displaying the same usage help.
The usage help of Windows Script Host explains also how each user can define which version of Windows Script Host is the default for executing scripts. So it is not advisable to specify just the VBS file name with full path on the command line with start and let cmd.exe look up in Windows registry which version of Windows Script Host to run to process the VBS file. It is better to explicitly run the version of Windows Script Host most suitable for playing the MP3 file which is in this case the Windows GUI version wscript.exe opening by default no window at all to play the MP3 file in background.
So it would be possible to use:
#start "" %SystemRoot%\System32\wscript.exe //NoLogo "%~dp0sfx\tyrian_soundtest.vbs"
There is an empty title string defined with "" as the started executable wscript.exe is a Windows GUI application for which no console window is opened at all by cmd.exe. So the title string does not really matter and can be an empty string.
But there is left one problem with that command line. The VB script references the MP3 file without path which means with a path relative to current directory. The current directory is %USERPROFILE% and not C:\Temp\Development & Test\sfx which contains the MP3 file tyrian_soundtest.mp3. So the VB script would fail to find the MP3 file to play.
There are two solutions to fix this:
The usage of the following command line in the batch file:
#start "" /D "%~dp0sfx" %SystemRoot%\System32\wscript.exe //NoLogo "%~dp0sfx\tyrian_soundtest.vbs"
The option /D of command START is used to explicitly set the subdirectory sfx of the batch file directory as start in respectively current directory for the process wscript.exe which cmd.exe starts using the Windows kernel library function CreateProcess with an appropriate created structure STARTUPINFO.
The VBS file references the MP3 file tyrian_soundtest.mp3 with its full path which the VB script file determines itself from its own full qualified file name. That can be achieved in the VB script file tyrian_soundtest.vbs by using in the second line:
Sound.URL = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) + "\tyrian_soundtest.mp3"
Best would be using both possible solutions as in this case the VB script file would work also on being executed by a different process than cmd.exe processing the batch file tyrian_soundtest.bat and deletion of directory %~dp0sfx is not possible as long as started wscript.exe is running because of this directory is the current directory of running Windows Script Host.
So the batch file as well as the VB script file work now both independent on which directory is the current directory as both reference the files with full path determined by the scripts themselves from their full qualified file names.

Batch script issue (Tomcat install the service but start using batch file )

I saved the below code as batch file but script able to install the tomcat service but not able to execute the line 4. Please let me know why it is not able to execute the line 4.
If i copied all the code and paste cmd window it is working fine and able to execute each command. but if double click on the batch file then not able execute the line 4 & 5. Please do the needful help. Thanks in advance
cd %CATALINA_HOME%
net stop SERVICE_NAME
sc delete SERVICE_NAME
service.bat install SERVICE_NAME
tomcat7.exe" //US//SERVICE_NAME --JvmMs=1024 --JvmMx=1024 --Jvm="C:\Program Files\Java\jre7\bin\server\jvm.dll" ++JvmOptions="-XX:MaxPermSize="512m" --Startup auto'
net start SERVICE_NAME
Use call command to start another batch script and return to calling batch as follows:
call service.bat install SERVICE_NAME
Recheck the tomcat7.exe line for proper use of " double quotes.
Use full paths to service.bat or tomcat7.exe if an error occurs similar to
'ddd' is not recognized as an internal or external command, operable
program or batch file.

How to create batch file?

I need to run the below command from command prompt. How do i make it a batch file so that I do not have to type the whole command again?
c:\program files (x86)\microsoft ajax Minifier\ajaxmin.exe c:\css\reset.css c:\css\main-styles.css c:\css\lightbox.css c:\css\shortcodes.css c:\css\custom-fonts.css c:\css\custom-colors.css c:\css\admin_menu.min.css -out c:\css\mc.css -clobber
Add the >"file.bat" echo to the start of the command to create a simple batch file.
Quotes around paths and filenames with spaces and & characters are needed too.
>"file.bat" echo "c:\program files (x86)\microsoft ajax Minifier\ajaxmin.exe" c:\css\reset.css c:\css\main-styles.css c:\css\lightbox.css c:\css\shortcodes.css c:\css\custom-fonts.css c:\css\custom-colors.css c:\css\admin_menu.min.css -out c:\css\mc.css -clobber
In some cases certain characters will need escaping but for your example it will work.
Just create a new file with a .bat extension and copy these lines into it.
Then you can run it by double clicking it in Windows explorer or by typing its name in cmd prompt.
There are many ways, by the looks of it you are already running that command from a command prompt, you can write the command to a batch file like this:
echo <command> > mycommand.bat
So:
echo ajaxmin.exe c:\css\reset.css c:\css\main-styles.css c:\css\lightbox.css c:\css\shortcodes.css c:\css\custom-fonts.css c:\css\custom-colors.css c:\css\admin_menu.min.css -out c:\css\mc.css -clobber > mycommand.bat
You can also type the command into notepad or other text editor and save with a .bat extension instead of .txt.
How to create a Batch file:
Right click on your desktop or whereever you want it to
Click new text document
Rename it
Open it in notepad or whatever you want, and now you have a batch file!

Execute .exe from a batch file

In an application folder, there are n number of files. The application exe name "ClearMongoDb.exe" take some parameter like dbname.
ex: clearMongoDb.exe -db "SynchoMeshDB"
I am stuck with below :
I want to execute the exe from a batch file with same parameters
the batch file will be placed in the same application folder.
user can copy the application folder to any location
If user double clicks on the .bat file the exe should start working.
User should not be required to make any changes in .bat file
If the batch file is in the same folder as the executable, then you can do like this:
clearMongoDb.exe -db "SynchoMeshDB"
Just add this line in your batch file. Now the refference is in the same folder as the executable, no matter where the ENTIRE folder is moved (or at least the executable and batch file).
update:
As foxidrive mentioned, in order to see the output, place a PAUSE command at the end. So, your batch file should be like this:
clearMongoDb.exe -db "SynchoMeshDB"
PAUSE
If you just want to pass all the parameters given to a batch file to an EXE called from that batch file, use %*.
foo.exe %*
How do I pass command line parameters to a batch file?
You can just use a shortcut to the file and add the parameters on the path. no need for an extra batch file.
edit: unless you want to pass the batch file parameters to the .exe, as some people read this. what do you want to do? execute a .exe with the same parameters each time, or pass the .bat parameters to the .exe?

ftp batch file log data to txt

me again :P
I have some problems with some bat file, that bat file , connects to and ftp and download all files from a remote folder, then delete it, but my problem/question is: i need a txt(log) of every file before download it from the remote server, here is my file
bat file:
ftp -i -s:ftpfile.txt site.com
txt file
user-name
user-pass
lcd c:\localfolder\some\folder
cd remotefolder-name
mget .
mdelete \\remotefolder-name\ .
quit
If i use the >>mylog.txt on this line:
ftp -i -s:ftpfile.txt site.com>>mylog.txt
I get some extra data that i dont want to. I just need the file names before download it, something like this:
log.txt
file001x.xml
filedfx.xml
file023x.xml
filed33x.xml
Ps:sorry for my english ;)
You may redirect ls output to local file. Syntax is: ls pattern local_file. An example would be: ls * ftplist.txt or ls . ftplist.txt. Both will produce a file ftplist.txt (in local current directory if you do not specify path) with a list of files (and possibly subdirs) in current directory on remote server (in slightly different formats)

Resources