Check existence of directory on local machine during ftp session - ftp

I am copying a large number of files during an ftp session from a remote host to my local machine. I need to save the files in a local directory tree. My problem is that a particular directory may not exist before the session. The way I have handled this in the past is to do set up my script by first creating the directory tree and not even worrying if the directory exists and then from within the same batch file (I create the batch file using Python) I start my session and use lcd to change to the correct directory
md c:\123
md c:\234
md c:\234\2009
loginname
password
cd remotedirectory
lcd c:\123
get somefile.txt
So all of the above is written out to one batch file and I start it to run. If the directory exists when I try to create it then I see a message in the terminal window that the directory exists and since nothing bad happens I have not worried about it.
What I would really like to do is check the existence of the local directory when I am ready to move to that directory and if it does not exist it gets created but I have not found out how to do this without closing the session and restarting it so I go back to the shell.
Is there a way to do this during the ftp session while maintaining the connection with the host?

You can use the ! command prefix to execute CMD MD command in order to make sure a local directory is already exist. e.g.:
! md "c:\my data\download" will create c:\data\download directory if it's not yet exist (and assuming you have the required permission). If it's already exist, it'll just display a harmless error. You can also use ! md "c:\my data\download" > nul to omit the error message. Or perform other commands.
Basically, everything after the ! ftp command prefix, will be passed to CMD.EXE as cmd /c {your command(s)}. If ! is used alone, it'll escape to CMD session prompt and wait for user input. The CMD's EXIT command will close the CMD session and return to the FTP prompt.

Related

How to execute further batch file commands after running "ftp" commands contains in the batch file itself

I have a .bat document where I do some operations with FTP. I want to end the FTP connection and get to finish with the terminal on the desired directory.
I'm trying this, but when it gets to quit, it stops.
cd %USERPROFILE%\Foilder\project\angular
call ng build
#FTP -i -s:"%~f0"&GOTO:EOF
open connection
user
password
cd httpdocs/project
mdelete *.woff
quit
cd %USERPROFILE%\Foilder\Subfolder
It gives me this message right after doing the quit and never runs the next line.
221 Goodbye.
Ideal scenario, run this last cd command and have the terminal ready on the folder that I want to run it. Thing is that I have to do some uploads to two different FTP servers and it would be great to have the console finishing in the folder where I run "dev.bat" to get it to work
Is it possible to do this?
Your script uses a known hack that allows you to specify ftp commands directly in the batch file.
The %~f0 is replaced with the path to the batch file itself. So the ftp -s:%~f0 runs ftp and tells it to use the batch file itself as ftp script file. You have probably noticed that it results in couple of errors, as ftp fails on the first few lines of the batch file, which are not valid ftp commands (the cd ..., call ... and ftp ...).
Equivalently, the batch file would try to run all commands after ftp ... once ftp finishes, also failing, as those are not valid batch file commands. To avoid that, the hack uses GOTO:EOF to skip the rest of the batch file (EOF=eof of the file).
While you actually want to execute some commands after the ftp. At least the cd command. So do not skip the rest of the batch file. Skip only the ftp commands:
ftp -i -s:"%~f0"&goto AFTER_FTP
(ftp commands)
quit
:AFTER_FTP
cd %USERPROFILE%\Foilder\Subfolder
Note the # before ftp. That (and the GOTO:EOF) are clear signs, that the script you based your batch file on, was designed to start with ftp on the very first line and contain nothing else, but the ftp commands. You have deviated from that.
Alternatively, use some more capable FTP client that allows specifying the commands on its command-line without hacks.
For example with my WinSCP FTP client, you can do:
cd %USERPROFILE%\Foilder\project\angular
call ng build
winscp.com /ini=nul /command ^
"open ftp://user:password#connection/" ^
"cd httpdocs/project" ^
"rm *.woff" ^
"exit"
cd %USERPROFILE%\Foilder\Subfolder
There's a guide for converting Windows ftp script to WinSCP script.

mget in batch script does not work

i'd like to create a batch script which logs on a ftp-server and copies some files to a remote folder. but my script does not work. mget runs into a timeout (has to be terminated manually).
i'm starting ftp connection with ftp.bat:
ftp.exe -s:getdata.bat
getdata.bat:
OPEN host-ip
user
password
lcd "C:\tmp"
cd config
mget C1000.xml
close
starting, logging in and changing the directory works but getting the file doesn't work. nothing happens until i end the script manually. any hints?
regards,
michael
mget prompts for user confirmation for each file it finds, even when you only specify one specific file.
To get around this, you can either use get if you know the file name, or you can use prompt to disable the interactive prompt before you use mget.
OPEN host-ip
user
password
lcd "C:\tmp"
cd config
prompt
mget C1000.xml
close

bat/cmd file does not return back

I want to launch some .cmd file and to remain in the same directory.
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
The problem is that while executing the setDomainEnv.cmd wit about 10 other cmd files called from it, I am left in totally another directory. And I want to be where I have started. As I always start in the same directory, I am adding a cd line:
Echo on
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
Echo on
cd /d C:\workspaces\DS8\swprods\dist-4.1.8-local-devel\
No effect! Again I am left in
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server>
And I never even see the 3,4 lines to appear in the console. I could understand all that if some of the inner skripts ended with error, but they finish OK, without errors.
It would be understandable if some of the scripts changed the dir to another disk and back change would need /d key. But all is done on the same C: disk.
All operations in the script are conducted in the same source directory. The directory is changed after leaving the batch file.
....................................................
C:\workspaces\DS8\swprods\dist-4.1.8-local-devel>if "Oracle" == "Apple" (set MEM
_ARGS=-Xms2048m -Xmx2048m -XX:CompileThreshold=8000 -XX:PermSize=128m -XX:MaxPermSize=256m -XX:MaxPermSize=256m )
C:\workspaces\DS8\swprods\dist-4.1.8-local-devel>if exist C:\Oracle\MIDDLE~1\ORACLE~1\USER_P~1\domains\WL_SER~1\bin\setStartupEnv.cmd (call "C:\Oracle\MIDDLE~1\ORACLE~1\USER_P~1\domains\WL_SER~1\bin\setStartupEnv.cmd" )
^this is the last run line of the script
"Why?" and sometimes I thought, "Wherefore?" and sometimes I thought, "Inasmuch as which? And how can I change the directory back?
Use call:
call C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
Call will invoke another script and after it gets completed - it will returns to your script.
If you do not use call - your execution flow goes to that another script but does not return back.
Use pushd to save the current directory then use popd to restore it after you run the other cmd file.
pushd .
call C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
popd
Good thing about pushd/popd is they work even if the other batch file changes the working directory to a directory on another drive.

Script to remap current network drive?

We need to disconnect and re-map a network drive on Windows 7, using a set of scripts (or an app) that runs off the same network path.
That is, I need something that loads itself into RAM before it runs, so it continues to run after the drive is disconnected.
Any ideas?
Please note that 16-bit apps are NOT supported in 64 bit systems (this explains why the Novell utility failed).
You would need a vbs file running throughout a logon session to remap drives if it's disconnected by user. Need to make this script to run when domain user logs on - e.g. Logon Script in AD or GPO. There are many ways to do it.
You could even disable "Remove Network drives" feature from Explorer GUI via GPO or Reg key (net use command still works).
Or you can tweak solution by Julius for this SO question to fit your need. But consider performance impact of the vbs - only check every n minute(s) in an infinite loop.
We do something similar. We have a batch file on the network that maps the drives a user needs. We update the batch file from time to time, and users run it from a shortcut that we've placed on their desktop:
C:\WINDOWS\system32\cmd.exe /c (#echo off&if not exist \\172.x.x.x\Login (echo Unable to access server&pause) else (md c:\TMP > NUL 2>&1 &copy \\172.x.x.x\Login\MapDrives.bat C:\TMP /y > NUL 2>&1 &call C:\TMP\MapDrives.bat&del C:\TMP\MapDrives.bat&rd c:\TMP))
You can see that it checks to see if they can access the server, and if they can, it creates a folder C:\TMP, copies the MapDrives.bat file locally, then runs it. Since it is running locally, it can remap network drives without terminating it own execution. And we can update the batch file on the server without pushing it to each user's computer.
If you don't want to create a shortcut with the long command line above, it might work to create a second batch file on the server (e.g., RunMe.bat) that users run from the server. You could place all of the code from the shortcut in the RunMe.bat and accomplish the same thing. Of course, you'd want to add one more line of code to change to the local drive (so Windows doesn't hold open a handle to the network drive). Something like this:
#echo off
C:
if not exist \\172.x.x.x\Login\MapDrives.bat (
echo Unable to access server
pause
) else (
md c:\TMP > NUL 2>&1
copy \\172.x.x.x1\Login\MapDrives.bat C:\TMP /y > NUL 2>&1
C:\TMP\MapDrives.bat
)
I kept the if not exist ... because you might place the RunMe.bat in a different location than the MapDrives.bat, so it still makes sense to verify the user can access the file. Because I didn't use call C:\TMP\MapDrives.bat, it transfers control to the local batch file and any handles to the server should be closed so the drive can be remapped. This means however, that you cannot place more commands after the C:\TMP\MapDrives.bat command.

Batch file - Remote Copy and Execute issue

I have a batch file that part of it copies a folder and its content to a local directory and then continues through the batch process which then tries to execute the contents of the folder . locally run it works on all wintel os and copies the files from \networkshare\folder*.* to %systemdrive%\temp\ .
I have attempted to use e.g psexec #serverlist.txt -c batfile.bat -s -f -d to copy the file and then execute it remotely how ever the issue is that this will not copy the files when run remotely . I think its a authentication issues after you a have remotely executed the batch file the remote system will not allow me to access/authenticate the networkshare
i have tried xcopy , copy , robocopy .
AFAIK you can only authenticate against the next hop using implicit credentials. Connection attempts from the first hop to a second hop will fail, even if your user has the required permisssions. See e.g. here for an explanation.
Try this:
psexec #serverlist.txt -u %USERDOMAIN%\%USERNAME% -d -c batfile.bat
Use explicit credentials so you're authenticated against the remote host. With that the second hop will be the next hop for your authenticated session. Don't run the script as LOCAL SYSTEM (-s), because that account is restricted to local resources.
I've had this issue in the past. Instead of trying to copy/run the BAT file just run the script things from a UNC path if you are able to. I think the problem lies in the BAT file not actually able to run through PSEXEC and copy like you think. Does it give an exit code?
Here's an example I used a while ago to install Adobe reader. I tried to do it through a batch file but no dice. I could not get it to run within the batch file whether I copied it or not. I can't remember the exact reason, I think it had something with the way a batch file is called in the system and runs in some sort of local context. Don't quote me on that though as I can't remember the exact why.
Here is my code example:
psexec /accepteula \\%computer% -s cmd /c msiexec /i "\\UNC\Software\adobe\Adobe Reader 7.0.9.msi" TRANSFORMS="acrobat7.mst" /qn

Resources