I need to download a file that is placed in a folder called abc_20140221_123456 in server1 over ftp to my local directory. The problem is that the last six characters of the folder name are not fixed. For example today the folder may be called abc_20140221_123456 and tomorrow it might be called abc_20140221_234567. I am having problems in writing an automation batch script to do the same.
Here's the script I am working on:
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
echo open server1>>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
echo lcd E:\>>file.tmp
:: Not sure how to cd to abc_20140221_* from here
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I know that I can loop through directories using for like this:
for /d %%d in (' %path%/*%folderName%* ') do (
echo get filename.txt>>file.tmp
echo y>>file.tmp
)
But "for" doesn't work inside ftp>.
Any help is highly appreciated. Thanks.
I am assuming that for a given date there is only one abc_YYYYMMDD_nnnnnn directory.
#echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
set root=E:
echo open server1>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
for /D %%D in (%root%\%foldername%??????) do set target=%%D
echo lcd %target%>>file.tmp
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause
I set root to E:\ based on your original script - you can change it to whatever the actual parent directory is. I also changed
echo open server1>>file.tmp
to
echo open server1>file.tmp
so it creates a new temp file. Otherwise, it will keep appending to file.tmp each time the script runs. If that is what you want, or if you are generating a unique script file name each time and were just using file.tmp as a placeholder, ignore that change and use >> as before.
If there is more than one folder for a given day, I believe this script will use whichever one comes up last when the wildcards are expanded. I did not test this out, however.
Related
I have a .bat file that is intended to a create a text file with parameters that could be used to execute a JS file, here me out ok.
The first .Bat file you would see is a SETUP.bat, thats what creates the 2nd .bat file for startup. But when I create the 2nd .bat file (.txt at this point in time) it misses some of my edit and I dont know why?
SETUP.bat
#echo off
break>"C:\Users\%username%\Desktop\test\START.txt"
echo set /p content=<"C:\Users\%username%\Desktop\test\lettercount.txt" >> C:\Users\%username%\Desktop\test\START.txt
echo call node "C:\Users\%username%\Desktop\test\index.js" content >> C:\Users\%username%\Desktop\test\START.txt
When its created it loses <"C:\Users\%username%\Desktop\test\lettercount.txt" and i dont know why?
I tried to do this but it didnt help either
SETUP.bat
#echo off
break>"C:\Users\%username%\Desktop\test\START.txt"
echo cd C:\Users\%username%\Desktop\test
echo set /p content=<lettercount.txt >> C:\Users\%username%\Desktop\test\START.txt
echo call node "C:\Users\%username%\Desktop\test\index.js" content >> C:\Users\%username%\Desktop\test\START.txt
and it still comes out as set /p content= instead of the thing it should be
any help?
According to my comment, and simplified.
#Echo off
(Echo Set /P "content="^<"%UserProfile%\Desktop\test\lettercount.txt"
Echo Call node "%UserProfile%\Desktop\test\index.js" content
)>"%UserProfile%\Desktop\test\START.txt"
Okay, this is my first question on Stackoverflow, so I'll try to make it a good one.
I've searched all over the web and I couldn't find any information to this. I have created a little batch file that prompts you to put in the name of a file that you would like to move. After that, it asks your your usersname. The problem I'm having is cmd tells me I have incorrect syntax.
Can anyone see what I did wrong?
Below is the code I am using. I just pasted in the part that is having trouble.
Thanks guys!
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename%
echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move C:\Users\%USERNAME%\Desktop\%filename%.bat
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\StartMenu\Programs\Startup\%filename%goto COMPLETED
goto FAILED
I editted a bit your script:
Surrounded filepaths with double quotes
Added /y parameter to move command to override automatically the file in new location (if exists)
Spotted some issues:
goto MOVE
:MOVE has no sense as it will continue anyway that path.
goto FAILED GOTO COMPLETED - there're no such labels in your script.
Please shout if you have other problems.
#echo off
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename% echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move /Y "C:\Users\%USERNAME%\Desktop\%filename%.bat" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%filename%" goto COMPLETED
goto FAILED
:FAILED
I am trying to ftp a text file from a Unix machine to a Windows machine. In the process, the data is getting mangled. I want to try transfering the file in binary instead of ASCII - hopefully that will make the data pass thru without getting corrupted.
How can I modify the following script to make the .txt file transfer as a binary file, instead of ASCII? I am not familiar with the syntax.
#echo off
setlocal
set uname=john
set passw=password
set hostname=johncomputer
set filespec=SampleSPEC
echo %uname%> name.ftp
echo %passw%>> name.ftp
echo cd CRMD>> name.ftp
echo get %filespec%>> name.ftp
echo bye>> name.ftp
ftp -s:name.ftp %hostname%
if errorlevel 1 pause
endlocal
Try: echo TYPE I >> name.ftp before the line that adds the get command.
BAT file Code is as below:
set SCRIPT_NAME=FTP_SCRIPT.DAT
#echo open "Server"> %SCRIPT_NAME%
#echo "User">> %SCRIPT_NAME%
#echo "Password">> %SCRIPT_NAME%
#echo bin>> %SCRIPT_NAME%
#echo hash>> %SCRIPT_NAME%
#echo mput export.csv>> %SCRIPT_NAME%
#echo y>> %SCRIPT_NAME%
#echo quit>> %SCRIPT_NAME%
#ftp -n -s:%SCRIPT_NAME%
It creates FTP_SCRIPT.DAT file correctly, however command prompt remains blank and FTP does not occur at all. I am using batch scripting for first time. So do not know much details of it. How will this FTP command work?
I don't see any error in your script, but make sure you put the correct ftp direction at open command: "xxx.xxxx.xxxx.xxxx.:PortNum"
Try this:
SET "SCRIPT_NAME=FTP_SCRIPT.DAT"
(
ECHO open "Server:Port"
ECHO "User"
ECHO "Password"
ECHO bin
ECHO hash
ECHO mput "export.csv"
ECHO y
ECHO close
)>"%SCRIPT_NAME%"
%WINDIR%\system32\FTP.exe -n -s:"%SCRIPT_NAME%"
I want to copy all the *.jar files to another directory i wrote the below script
echo Enter path to ILM_HOME:
set /p ILM_HOME=
echo Deployment in progress
copy WEB-INF/lib/*.jar "%ILM_HOME%"/webapp/WEB-INF/lib
I use C:\Documents and Settings\asimon\Desktop\test as my input
It gives me syntax of the command is incorrect
I think the problem is Documents and Settings I even put "%ILM_HOME%" and I don't need c:\Docume~1\asimon\Desktop\test any other solution?
Update
This is working
#echo off
echo
echo Enter path to ILM_HOME:
set /p ILM_HOME=
IF EXIST "%ILM_HOME%\stopApplimation.bat" (
echo Deployment in progress
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"
CALL "%ILM_HOME%\stopApplimation.bat"
CALL "%ILM_HOME%\startApplimation.bat"
) ELSE (
echo %ILM_HOME% path is incorrect.
)
also any linux solution is also helpfull with .sh for the below 2 statements
"%ILM_HOME%/stopApplimation.bat"
"%ILM_HOME%/startApplimation.bat"
for linux, how can i replace the above 2 statements?
$ILM_HOME/stopApplimation.sh
$ILM_HOME/startApplimation.sh
Did you try
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"?
EDITED:
In your batch use CALL "%ILM_HOME%\stopApplimation.bat"