Problems when writing a batch file to repeat a paragraph containing %PATH% - batch-file

I have written the following batch file that will write 100 paragraphs into a .bat file with incremental number changes to the file names referred.
for /l %%N in (1 5 500) do (
echo cd /D "C:\batch"
echo set PATH=C:\batch; &PATH%
echo echo Simulation 'g=0.00%%N.pre' started...
echo call %SOLVERQSP%-fp 1 -nt 3 ^
"C:\batch\0.00%%N.pre" > "C:\batch\g=0.00%%N.out"
echo echo Simulation 'g=0.00%%N' finished...
echo echo.
The problem is that the batch file is not created when the %PATH% and %SOLVERQSP% commands are present. If i remove these the script runs fine. Any suggestions of where I am going wrong or how I can avoid this

You should use some quotes and avoid the caret in your case
for /l %%N in (1 5 500) do (
echo cd /D "C:\batch"
echo set "PATH=C:\batch; %PATH%"
echo echo Simulation 'g=0.00%%N.pre' started...
echo call %%SOLVERQSP%%-fp 1 -nt 3 "C:\batch\0.00%%N.pre" > "C:\batch\g=0.00%%N.out"
echo echo Simulation 'g=0.00%%N' finished...
echo echo.
)

Just escape the % by doubling them:
echo set PATH=C:\batch;%%PATH%%

Related

How to make a Batch File Move another File?

I am trying to make a batch script that moves a file into a startup folder and I want to make it universal
Currently I have this but I want to make it work on any user's computer (C:\users\USERNAME)
Here is the code
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %name%
move C:%user%\Desktop\Directory 1\dile.txt C:%user%\Desktop\Directory 1\file folder 1
:end
cmd /k
The file is called dile.txt located in a folder called Directory 1 on the desktop and I want to make it move to a folder called file folder 1 inside the Directory 1 folder. Is there a way to do this while making it work on anyone's computer?
%USERNAME% can be used to grab the active user account. Try something like this. Make sure to enclose paths in quotes when folders have spaces in their names.
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %USERNAME%
move "C:\Users\%USERNAME%\Desktop\Directory 1\file.txt" "C:\Users\%USERNAME%\Desktop\Directory 1\file folder 1\"
:end
cmd /k
Just for the sake of providing something a little bit different:
#Echo Off & SetLocal EnableExtensions
Set /P "=Startup . " 0< NUL
For /L %%G In (1,1,5) Do (Set /P "=. " 0< NUL
%SystemRoot%\System32\PATHPING.EXE 127.0.0.1 -n -q 1 -p 500 1> NUL)
Echo(&Echo Your name is %UserName%
%SystemRoot%\System32\Robocopy.exe "%UserProfile%\Desktop\Directory 1" "%UserProfile%\Desktop\Directory 1\file folder 1" "dile.txt" /Mov 1> NUL 2>&1
Pause
This uses some animated text, %UserProfile%, instead of C:\Users\%UserName%, and robocopy instead of Move. Using RoboCopy, allows for the creation of your destination directory, if it does not already exist.

batch download images from url with for

I need to download 300 images from site.com/folder/ using the following format: 1.png, 2.png ... 300.png
Is there a way to do this inside a batch file or using the command prompt?
Wth curl like this:
curl -o "#1.png" http://example.com/folder/[1-300].png
Here is an example to download some batch codes from a file that can be created by this script if not exist, and of course you can add or modify what you want of urls in this file !
You can add your urls in the text file named Urls.txt
Firstly, the script check for the text file named Urls.txt if exist in same location where this batch is executed and read from it the urls line by line to download them !
So, if you want to change those urls to yours, just change it from the text file Urls.txt not from the batch, i mean you can create a text file and name it to Urls.txt and put what you want as urls on this file line by line of course and let the script do its job
#echo off
Mode 110,3 & color 0A
Title Download file from web using powershell and batch by Hackoo 2017
Set "List_Urls_File=Urls.txt"
If not exist "%List_Urls_File%" Call :Create_Urls_File
Setlocal enabledelayedexpansion
#For /f "delims=" %%a in ('Type "%List_Urls_File%"') do (
Set "URL=%%a"
Rem we set the Filename from the variable !url!
#for %%# in (!url!) do ( set "File=%%~xn#" )
Rem Check if the file name contains a dot "."
Rem If not we increment the counter +1 for file to be download
ECHO !File! | FIND /I ".">Nul 2>&1
If "!errorlevel!" NEQ "0" (
Set /a Count+=1
cls & echo(
echo Downloading file "File-!Count!.bat" from URL : "!URL!"
Call :BalloonTip 'information' 10 '"Downloading File-!Count!.bat"' "'Please wait... Downloading File-!Count!.bat....'" 'info' 4
Call :Download "%%a" "File-!Count!.bat"
) else (
cls & echo(
echo Downloading file "!File!" from URL : "!URL!"
Call :BalloonTip 'information' 10 '"Downloading !File!"' "'Please wait... Downloading !File!....'" 'info' 4
Call :Download "%%a" "!File!"
)
)
Explorer "%~dp0" & exit
::*********************************************************************************
:Download <url> <File>
Powershell.exe -command "(New-Object System.Net.WebClient).DownloadFile('%1','%2')"
exit /b
::*********************************************************************************
:Create_Urls_File
(
echo https://pastebin.com/raw/XvyhRzT6
echo https://pastebin.com/raw/QqnZ0MjQ
echo https://pastebin.com/raw/tHsKw15V
echo https://pastebin.com/raw/VCnTbLB6
echo https://pastebin.com/raw/3zUTrWUz
echo https://pastebin.com/raw/31auQeFz
echo https://pastebin.com/raw/xF0uXThH
echo https://pastebin.com/raw/uzsGQD1h
echo https://pastebin.com/raw/3TmVYiZJ
echo https://pastebin.com/raw/Ntc8SZLU
echo https://pastebin.com/raw/jnpRBhwn
echo https://www.virustotal.com/static/bin/vtuploader2.2.exe
echo http://devbuilds.kaspersky-labs.com/devbuilds/KVRT/latest/full/KVRT.exe
)>"%List_Urls_File%"
exit /b
::*********************************************************************************
:BalloonTip $notifyicon $time $title $text $icon $Timeout
PowerShell ^
[reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
[reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
$notify = new-object system.windows.forms.notifyicon; ^
$notify.icon = [System.Drawing.SystemIcons]::%1; ^
$notify.visible = $true; ^
$notify.showballoontip(%2,%3,%4,%5); ^
Start-Sleep -s %6; ^
$notify.Dispose()
%End PowerShell%
exit /B
::*************************************************************************
Numbered-Files Downloader 1.0
Here is a complete batch script that is doing exactly what you asked for. You don't need to download any executable files, this is 100% batch script and it should works on any (recent) Windows installation.
All you need to do is to edit the _URL variable (Line 11) and replace "example.com/folder..." with the actual URL of the files you want to download. After that, you can run the script and get your files.
Note that in your URL, this string: _NUMBERS_ is a keyword-filter that will be replaced by the incremented numbers in the final download function.
All your downloaded files will be saved in the directory where this script is located. You can choose an other directory by uncommenting the _SAVE_PATH variable (Line 15).
Finally the following variables can be changed to configure the series of numbers:
_START : The file numbers starts with this value.
_STEP   : Step between each files.
_END    : The file numbers ends with this value.
Leading Zeros
Currently, the counter doesn't support leading zeros.
EX. From Picture_001.jpg to Picture_999.jpg
But otherwise it should work fine for something like this:
EX. From Picture_1.jpg to Picture_999.jpg
I will try to find some time to add this option, it shouldn't be too difficult.
Feel free to modify & enhance this script if you need!
Numbered-DL.cmd
#echo off
setlocal EnableDelayedExpansion
rem STACKOVERFLOW - QUESTION FROM:
rem https://stackoverflow.com/questions/45796990/batch-download-images-from-url-with-for
:VARIABLES
rem WHERE YOU WANT TO SAVE FILES
rem "%~dp0" is a variable for the same folder as this script, so files should be saved in the same folder.
rem If you want to save the downloaded files somewhere else, uncomment the next line and edit the path.
SET "_SAVE_DIR=%~dp0"
rem SET _SAVE_PATH=C:\Folder\
rem DOWNLOAD THIS FILE URL
rem
rem "_NUMBERS_" WILL BE REPLACED BY THE COUNTER
rem CURRENLY IT DOESN'T SUPPORT CHOOSING A NUMBERS OF ZEROS FOR THE COUNTER EX: 001,002,003...
rem BUT IT SHOULDN'T BE TOO HARD TO IMPLEMENT, MAYBE ILL ADD THIS IN THE FUTURE.
rem
rem SET _FILE_URL=https://example.com/folder/_NUMBERS_.png
SET "_FILE_URL=https://cweb.canon.jp/eos/lineup/r5/image/downloads/sample0_NUMBERS_.jpg"
rem FOR THIS EXAMPLE THE SCRIPT WILL DOWNLOAD FILES FROM "sample01.jpg" TO "sample05.jpg"
SET _START=1
SET _STEP=1
SET _END=5
:CMD_PARAMS
IF NOT [%1]==[] SET "_FILE_URL=%1"
IF NOT [%2]==[] SET "_SAVE_DIR=%2"
:PATH_FIX
rem REMOVE THE LAST CHAR IF IT IS "\"
IF [%_SAVE_DIR:~-1%] == [\] SET "_SAVE_DIR=%_SAVE_DIR:~0,-1%"
:DETAILS_DISPLAY
ECHO.
ECHO SCRIPT: Numbered-Files Downloader 1.0
ECHO AUTHOR: Frank Einstein
ECHO.
ECHO.
ECHO INPUTS
ECHO _URL: %_FILE_URL%
ECHO _SAVE_DIR: %_SAVE_DIR%
ECHO.
ECHO _START: %_START%
ECHO _STEP= %_STEP%
ECHO _END= %_END%
ECHO.
ECHO.
CALL :DOWNLOAD_LOOP
ECHO.
ECHO EXECUTION COMPLETED
ECHO.
PAUSE
EXIT /B
:DOWNLOAD_LOOP
SET FINAL_URL=%_FILE_URL%
FOR /L %%G IN (%_START%,%_STEP%,%_END%) DO (
rem REPLACE URL'S KEYWORD WITH NUMBERS
SET NUM=%%G
SET FINAL_URL=%FINAL_URL:_NUMBERS_=!NUM!%
rem CUMSTOM BATCH FUNCTION FOR DOWNLOADING FILES
rem
rem SYNTAX:
rem echo CALL :DOWNLOAD !FINAL_URL!
CALL :DOWNLOAD !FINAL_URL! !_SAVE_DIR!
)
Goto :EOF
rem PAUSE
rem EXIT /B
rem FUNCTIONS
:DOWNLOAD
setlocal
SET "DL_FILE_URL=%1"
SET "DL_SAVE_DIR=%2"
rem EXTRACT THE FILENAME FROM URL (NEED TO FIX THIS PART?)
FOR %%F IN ("%DL_FILE_URL%") DO SET DL_FILE_NAME=%%~nxF
IF "%DL_SAVE_DIR:~-1%" == "\" SET "DL_SAVE_DIR=%DL_SAVE_DIR:~0,-1%"
IF NOT [%2]==[] SET "DL_SAVE_FILE=%DL_SAVE_DIR%\%DL_FILE_NAME%"
IF [%2]==[] SET "DL_SAVE_FILE=%~dp0%DL_FILE_NAME%"
rem :BITSADMIN
ECHO.
ECHO DOWNLOADING: "%DL_FILE_URL%"
ECHO SAVING TO: "%DL_SAVE_FILE%"
ECHO.
bitsadmin /transfer mydownloadjob /download /priority foreground "%DL_FILE_URL%" "%DL_SAVE_FILE%"
rem BITSADMIN DOWNLOAD EXAMPLE
rem bitsadmin /transfer mydownloadjob /download /priority foreground http://example.com/filename.zip C:\Users\username\Downloads\filename.zip
endlocal
GOTO :EOF
try with winhttpjs.bat:
set "baseLink=http://example.org/folder/"
for /l %%a in (1;1;300) do (
winhttpjs.bat "%baseLink%%%a.png" -saveto %%a.png
)

Batch file - Multiple FTP upload with wildcard match

I need to upload via FTP some files (not all) from a folder to a remote server.
I coded the following which performs the wildcard match, but I think I am missing something to FTP the resulting files.
Remark: the problem is not to upload the folder (which is relatively simple), but to exclude some entries from the given folder and upload all other files.
I was successful in excluding those files and keep the rest, but I can't find the way to upload the latter.
In particular, mput cmd seems to not work with the input filename. Why ?
I want to focus on this question: how can I feed the FTP cmds to upload each resulting file from the above filtering ?
echo off
setlocal enabledelayedexpansion
FOR /R localfolder %%F IN (*.*) DO (
set fname=%%~nF
set ext=%%~xF
set filename=!fname!!ext!
set subfname=!fname:~0,4!
IF NOT "!subfname!" == "idat" (
echo ftp
echo open ftp.something.it
echo ftpuser
echo ftppass
echo lcd localfolder
echo cd remotefolder
echo binary
echo mput !filename!
echo disconnect
echo bye
)
)
pause
This is the solution, I was nearly close to it: just fill-in an external batch .bat file with ftp commands and then call this file at the end of each loop. Delete it and start again with the next entry on the list.
p.s.: for any further usage, beware of trimming any blank spaces before >> operator, as shown below.
#echo off
setlocal enabledelayedexpansion
FOR /R localfolder %%F IN (*.*) DO (
set fname=%%~nF
set ext=%%~xF
set filename=!fname!!ext!
set subfname=!fname:~0,4!
IF NOT "!subfname!" == "idat" (
echo open ftp.something.it>> ftp.cmds.bat
echo username>> ftp.cmds.bat
echo password>> ftp.cmds.bat
echo lcd localfolder>> ftp.cmds.bat
echo cd remotefolder>> ftp.cmds.bat
echo binary>> ftp.cmds.bat
echo mput !filename!>> ftp.cmds.bat
echo disconnect>> ftp.cmds.bat
echo bye>> ftp.cmds.bat
ftp -i -s:ftp.cmds.bat
del ftp.cmds.bat
)
)
Next script could help. Read List of FTP commands for the Microsoft command-line FTP client
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "localfolder=D:\test\31441809\root" change
set "lcd-folder=%localfolder%"
set "excludeFile=nico" change to `idat`
set "ftpscript=%temp%\35574454ftp.dat" change
(
echo open ftp.something.it
echo user ftpuser
echo ftppass
echo binary
echo cd remotefolder
echo lcd %lcd-folder%
FOR /R %localfolder% %%F IN (*.*) DO (
set "fname=%%~nF"
set "ext=%%~xF"
set "filename=!fname!!ext!"
set "subfname=!fname:~0,4!"
IF /I NOT "!subfname!"=="%excludeFile%" (
if /I not "!lcd-folder!\"=="%%~dpF" (
rem change local directory only if necessary
set "lcd-folder=%%~dpF"
set "lcd-folder=!lcd-folder:~0,-1!" remove trailing backslash
echo lcd !lcd-folder!
)
echo put !filename!
)
)
echo disconnect
echo bye
)>"%ftpscript%"
type "%ftpscript%"
pause
ftp -i -n -s:"%ftpscript%"
Sample output:
d:\bat> D:\bat\SO\35574454.bat
open ftp.something.it
user ftpuser
ftppass
binary
cd remotefolder
lcd D:\test\31441809\root
put JohnDoe.txt
lcd D:\test\31441809\root\Ian-ionescu
put Ian-ionescuY.txt
lcd D:\test\31441809\root\John-doe
put John-doe.txt
put John-doeA.txt
lcd D:\test\31441809\root\Nicola-sheperd
put SheperdNicola.txt
lcd D:\test\31441809\root\Sara-smith
put Sara-smith.txt
put Sara-smithZ.txt
disconnect
bye
Press any key to continue . . .
ftp> open ftp.something.it
Connected to www.something.it.
220 FTP Server ready.
ftp> user ftpuser
331 Password required for ftpuser.
etc.etc.

Batch Get User Input To Put Into A File

I have this code:
loop
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
set /p Lua=Lua:
%Lua% >> LuaCode.lua
if %Lua% == "LuaIDE.Exit()" ( echo Thanks for using LuaIDE!
timeout /t 3 /nobreak >nul
exit )
goto loop
and what i want it to do is get User-Input and put it into a file and if the user input is "LuaIDE.Exit()" I would like it to say a message for 3 seconds and exit but it just says:
'whatever you type in' is not recognized as an internal or external command,
operable program or batch file.
Why does it do this?
You have a few bugs. Try this:
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
:loop
set /p Lua=Lua:
echo %Lua% >> LuaCode.lua
if %Lua%==LuaIDE.Exit() goto exit
goto loop
:exit
echo Thanks for using LuaIDE!
ping 1.1.1.1 -n 3 -w 1 >nul
exit
Try this:
#echo off
:loop
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
set /p Lua=Lua:
echo %Lua%>>LuaCode.lua
if /i "%Lua%" == "LuaIDE.Exit()" echo Thanks for using LuaIDE & timeout /t 3 /nobreak >nul & exit
goto loop
Let me know how it works.
stop complicating a simple task....
the code is as simple as
#echo off
: start
echo.
set /p input= type your text here:
echo %input%>>myfile.extension
timeout /t 5 >null
cls
goto start
replace myfile with name of your file you want to send text.
replace extension with any that you want txt,log , etc
also you can specify path of your file e,g
echo %input%>>path/directory

Batch Script - CALL another batch script to call line to perform certain command

Is it possible to create a batch file with a bunch of commands (commands.bat)
ECHO HELLO
ECHO HOLA
ECHO KONICHIWA
ECHO ANYONGHASEYO
ECHO BONJOUR
, then within a different batch file, CALL commands.bat and only perform the command on line 2 or line 4 without knowing what is on those lines?
Here's what I meant in my comment.
master.bat
echo abcd
echo hello
echo notepad
echo public
echo wind
echo balance
command.bat
#echo off
more +3 master.bat > temp.bat & temp.bat
Prints the below for me.
public
wind
balance
To start off from the first line, use +0.
If you want this number to be sent via command line, here is a slightly modified version:
command.bat
#echo off
more +%1 master.bat > temp.bat & temp.bat
You can run the above with commands such as command.bat 0 or command.bat 3.
A neat little trick I used to know (back when batchfiles were in vogue)
SET JUMPTO=HOLA
goto BRANCH_%JUMPTO%
:BRANCH_HELLO
echo HELLO
GOTO :QUIT
:BRANCH_HOLA
echo HOLA
GOTO :QUIT
:BRANCH_KONICHIWA
echo KONICHIWA
GOTO :QUIT
:BRANCH_ANYONGHASEYO
echo ANYONGHASEYO
GOTO :QUIT
:BRANCH_BONJOUR
echo BONJOUR
GOTO :QUIT
:BRANCH_
echo Illegal branch?!
:QUIT
It becomes more intesting when you replace the first line with e.g. SET JUMPTO=%1
Some test output:
E:>.\test.cmd HELLO
E:\>SET JUMPTO=HELLO
E:\>goto BRANCH_HELLO
E:\>echo HELLO
HELLO
E:\>GOTO :QUIT
E:>.\test.cmd
E:\>SET JUMPTO=
E:\>goto BRANCH_
E:\>echo Illegal branch?!
Illegal branch?!
E:\>
Give me your input on this as a solution. It works, but I know some people don't like piping the FIND command to anything =/
REM Contents of COMMANDS.BAT
ECHO HELLO & ::1
ECHO HOLA & ::2
ECHO KONICHIWA & ::3
ECHO ANYONGHASEYO & ::4
ECHO BONJOUR & ::5
REM Command to perform ECHO KONICHIWA out of COMMANDS.BAT
CALL C:\COMMANDS.BAT | FIND "3"
Better yet, I discovered this playing around with it yesterday:
REM Contents of COMMANDS.BAT
ECHO HELLO & ::1
ECHO HOLA & ::2
ECHO KONICHIWA & ::3
ECHO ANYONGHASEYO & ::4
ECHO BONJOUR & ::5
-
REM Command to perform ECHO KONICHIWA out of COMMANDS.BAT
FINDSTR ::3 COMMANDS.BAT | START /B
That way I don't have to output the line to another bat file, it just runs the command instantly.
This is similar to Mechaflash's answer but makes use of findstr instead of find.
master.bat
echo abcd &rem line1
echo hello &rem line2
echo notepad &rem line3
echo public &rem line4
echo wind &rem line5
echo balance &rem line5
command.bat
#echo off
findstr line%1 master.bat > temp.bat & temp.bat

Resources