check existing file in ftp batch file [duplicate] - batch-file

I am writing a quick batch to see if a file exists after executing an exe.
The file is created with a YYYYmmDDnumbernumbernumber.xml file name according to the current date.
How do I check for the file with a variable in the beginning? Here is what I have so far:
#echo off
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
if not exist "ftp://FTPsite/%mydate%*.xml" (echo nah) else (echo yea)
pause

you cannot check existence of file on ftp server with IF.Instead try this after replacing the parameters that starts with MY_ :
!cls&echo off&setlocal ENABLEDELAYEDEXPANSION
!cls&goto :ftp_end
open MY_FTP_SERVER
user MY_USER
pass MY_PASS
cd MY_REMOTE_DIR
ls . local.file
bye
:ftp_end
ftp -s:%0
set mydate=%date:~10,4%%date:~4,2%%date:~7,2%
type local.file | findstr /B "%mydate%" | find ".xml" && echo FILE IS OUT THERE && goto :skip_file_is_not_there
echo FILE IS NOT THERE
:skip_file_is_not_there
del local.file /q >nul

Related

If else statement in bat

The code below checks if there is a folder called Made_files if so it will cd to that folder and make a new file using variables
Else it will make a folder called Made_files THEN cd to that folder and run the code but at the moment all it does is flash up then close instantlly can someone help
if EXIST Made_files (
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo #echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
) else mkdir Made_files
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo #echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
echo ___
pause
#ECHO OFF
SETLOCAL
:: Ask for filename and type
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
:: Required file to be located in "Made_files"
SET "subdir=Made_files"
MD ".\%subdir%" 2>NUL
CD "%subdir%"
(
echo #echo off
echo color 0a
)> %name%.%Filetype%
echo ___
pause
GOTO :EOF
What your code does:
If the subdirectory already exists, switch to that directory, ask for the filename&type, then create a new file using the requested data. Otherwise, create the directory.
THEN
Change to the subdirectory again (so if made_files already existed, try to change to .\made_files\made_files), ask for the filename and type and create the file.
===
BUT Please read Stephan's DELAYEDEXPANSION link. Since you are not using delayedexpansion, when the if statement is parsed (and the if statement continues until the end of the else clause) the values of the variables name and filetype are replaced by their values when the statement was parsed (~verified), NOT as they were input, so the destination filename will be nothing.nothing , which is "." - a directory-name (sort-of).
It's likely that this is the source of the drama.
When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.
===
So : the code I suggest:
Asks for the file name and type.
Attempts to make the directory. If it already exists, this will generate an errormessage which is suppressed by the 2>nul.
switches to that subdirectory.
Creates the file.
Note that if the echoes are surrounded by parentheses, then their output is combined and directed to the file, saving a plethora of redirect-to-file clauses.

Getting the standard output from CD into a file within a loop

Using some components I found here, I have built a batch file to loop through a directory tree starting from the directory where the batch file runs.
The batch file works as expected but I need to capture the output from the cmd.exe command CD into a file that I created earlier in the run.
The problem is that if I attempt to redirect the standard output into the .txt file I only see the first found directory.
I have found some code which uses PowerShell, to pull the listing from the Command Prompt screen, but to me this is inelegant, (although it seems to work).
I have read the material on setlocal enabledelayedexpansion but it appears to be above my paygrade as I haven't been able to make it work.
The working code is below, with a Remark where I think the export to the .txt file should go.
Help would be appreciated.
Rem Recursively Traverse a Directory Tree
Rem Notes:
Rem "For /r" command can be used to recursively visit all the directories in
Rem a directory tree and perform a command in each subdirectory.
Rem In this case, save the output to a text file
Rem for /r = Loop through files (Recurse subfolders).
Rem pushd = Change the current directory/folder and store the previous folder/path for
Rem use by the POPD command.
Rem popd = Change directory back to the path/folder most recently stored by the PUSHD
Rem command.
#echo off
CLS
echo.
echo.
Rem FirstJob - Generate a date and save in the work file.
Rem Grab the date/time elements and stuff them into a couple of variables
set D=%date%
set T=%time%
set DATETIME=%D% at %T%
Rem OK. We now have the date and time stuffed into the variable DATETIME
Rem so now stick it into our work file along with a heading.
Echo List of Found Directories > DirList.txt
Echo %DATETIME% >> DirList.txt
echo. >> DirList.txt
echo. >> Dirlist.txt
Rem SecondJob - Do the looping stuff and save found directories to file.
Rem Start at the top of the tree to visit and loop though each directory
for /r %%a in (.) do (
Rem enter the directory
pushd %%a
CD
Rem ------------------ direct Standard Output to the file DirList.txt -----------------
Rem exit the directory
popd
)
: END
Rem All finished
Echo Done!
exit /b
The additional lines of code when added to the above scrip before the :END marker. which did produce the wanted output were:
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^a')
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^c')
powershell Get-Clipboard>>DirList.txt
The problem is your pushd command, because you change the current directory, the file dirlist.txt must use an absolute path, else you create it in every pushed directory.
I used %~dp0 here, it's the path of the batch file itself.
You could try
cd >> %~dp0\dirlist.txt
Or just
echo %%a >> %~dp0\dirlist.txt
Or you could use a single redirecton of the complete bock
(
for /r %%a in (.) do (
pushd %%a
echo %%a
popd
)
) > dirlist.txt

Opening A Batch File In Notepad++ In The Folder Where Notepad++ Is Opened

I Made A Batch File Which Would Open In Notepad++ From The CURRENT DIRECTORY where the batch file is located(with run.. feature in notepad++), But I Want batch file to open on the folder where i have opened the file.
Example:
My Batch File Is Located In D:\Projects\Java\Executor Java.bat
I Have Opened A File Of .java Extension in D:\Java\Files
I Want to open at file location,i.e. D:\Java\Files
My batch file Looks like this:
#ECHO OFF
ECHO WELCOME TO EXECUTOR
ECHO -Garvit Joshi(garvitjoshi9#gmail.com)
ECHO USER:%USERNAME%
cd /d "%~dp0"
:first
ECHO LOOKING FOR FILES IN:"%~dp0"
set /p "input=Enter The File You Want To Execute:"
ECHO ===============================
javac %input%.java
ECHO ===============================
set /p "input=Enter The Class You Want To Run:"
ECHO ===============================
ECHO OUTPUT:
ECHO ===============================
java %input%
ECHO ===============================
pause
ECHO =======================================================
ECHO *******************************************************
ECHO =======================================================
goto first
You'll need to pass the path as a parameter from Notepad++ in the 'Run...' dialog, e.g:
cmd /c "D:\Projects\Java\ExecutorJava.bat $(CURRENT_DIRECTORY)"
..then have your batch file use the parameter with something like:
cd /d "%1"
(In this example, have removed the space from the "Executor Java.bat" filename for convenience)

Download a .rar on mediafire with a bat File without Any Program

Hello i was trying to download a .rar archive on mediafire but it doesnt seem to work it the file doesnt get to the specific folder
"c:\potato"
Here is my code:
#Echo off
echo download press any key
pause
bitsadmin.exe /transfer "JobName"
http://download1142.mediafire.com/sxd533x3fosg/ymr5y2r0yax2fx8/minecraft+generator.zip C:\Potato.exe
pause
Try something like this:
#echo off
setlocal EnableDelayedExpansion
set "infile=http://download1142.mediafire.com/3b1mz9yb72tg/ymr5y2r0yax2fx8/minecraft+generator.zip"
set "outfile=Potato.zip"
cd "C:\"
echo download press any key
pause
powershell.exe -Command (new-object System.Net.WebClient).DownloadFile('"%infile%"','"%outfile%"') | findstr "Exception error" >nul 2>nul
set "downloadComplete=%errorlevel%"
if %downloadComplete% neq 1 echo Something went wrong, please try again
if %downloadComplete% neq 1 pause
if %downloadComplete% neq 1 goto :eof
unzip "%outfile%" "minecraft generator.zip" >nul
unzip "minecraft generator.zip" "minecraft generator.exe" >nul
rename "minecraft generator.exe" "potato.exe"
pause
Please note that you used the mediafire URL instead of the actual file URL, and that you try to download a zip file to .exe. You should first download the file as zip, and then use unzip to unzip the files.
Also note that your second zip seems to be password protected.

Batch file to copy files to auto generated folder based on auto generated folder name

I'm publishing content to autogenerated folders and after the publishing has finished I want to copy files to that folder based on the foldersname using a batch
The autogenerated folders always have a language name, for example German, Dutch, French and English.
What I want my batch to do is that when the folder name is German it copies all the files from C:\Sourcefolder\DE\ to the new generated folder I'm running the batch from. I've tried to find something myself but my lack of knowledge results in this:
CHDIR /D %1
#ECHO OFF
SETLOCAL
SET "sourcedir=%cd%"
IF "%1"=="German" goto :German
:German
xcopy /Y "C:\Sourcefolder\DE\*.jpg" "%1"
GOTO :EOF
Is there anyone who can help me in the right direction?
Thanks in advance!
Here is the code for a batch file which might do what you want.
#echo off
rem Is this batch file called without any parameter?
if "%~1"=="" (
echo.
echo Run %~nx0 with language as first parameter.
echo.
echo Example: %~nx0 English
echo.
pause
goto :EOF
)
if /I "%~1"=="German" set "ShortName=DE" & goto CopyFiles
if /I "%~1"=="English" set "ShortName=EN" & goto CopyFiles
rem This batch file was called with a (language) string not listed above.
echo.
echo Error: "%~1" is not a supported language.
echo.
pause
goto :EOF
:CopyFiles
rem Copy all JPEG files of the specified language from source folder
rem to the specified language folder in current working directory.
xcopy /H /I /K /Q /R /Y "C:\SourceFolder\%ShortName%\*.jpg" "%~1"
set "ShortName="
Some notes additionally to the comments in the batch code:
%~1 is replaced by cmd.exe on execution of the batch file with the string of first parameter with removing double quotes if batch file was called for example with "English" instead of just English as first parameter.
If you want to know more about %~1 or %~nx0 (name of batch file with extension but without drive and path), open a command prompt window, run there call /? and read help output for this command.
/I option of command if makes the string comparison case-insensitive.
The ampersand in set "ShortName=DE" & goto CopyFiles concatenates the two commands set and goto on a single line which makes it possible here to specify 2 commands for each if without using parentheses. See Conditional Execution for details about this special operator.
For details on the switches used on command xcopy run in a command prompt window xcopy /? and read output help.
By the way: The batch files copies the JPEG files into specified language subfolder of current working directory. The current working directory can be the directory the batch file is stored, but can be also a different directory depending on how batch file was started and from which directory. If you want to make sure that the JPEG files are copied into specified language subfolder of batch file directory, you would need for the line with xcopy:
xcopy /H /I /K /Q /R /Y "C:\SourceFolder\%ShortName%\*.jpg" "%~dp0%~1"

Resources