batch script to read text separated by delimiter - file

I have a text file which contains a list of URLs separated by comma.
e.g. URL1,URL2,URL3 etc.
I have a batch script which just launches the url in the text file.
for /f "delims=," %%a in (ListOfURLs.txt) do (start /wait iexplore.exe %%a)
Currently, it only launches the 1st URL and stops. It is not reading the subsequent URLs.
Help needed.

#ECHO OFF
SETLOCAL
FOR /f "delims=" %%u IN (listofurls.txt) DO (
FOR %%i IN (%%u) DO ECHO START iexplore %%i
)
the ECHO is to show what is intended. To execute, remove the ECHO keyword.

Related

Using a filename variable in a for loop

I have a directory with a file in it (actually it has a lot of files, but I figured getting one to work was the first step before building the loop to hit each file), that needs to be edited and saved with a similar filename. Instead of manually typing in the filename, I'd like to use a variable containing the filename.
Sample Input Data
FileX.txt
Text line
Text line
Text line
Desired Output Data
FileX2.txt
1 Text line
2 Text line
3 text line
I can manage this with one file at a time, I'm struggling to write one script to look through the contents of a folder and do this to each text file within the folder.
I'm on windows 7 and this is what I have so far:
#echo off
setLocal EnableDelayedExpansion
Set N=0
REM THE BELOW LINE IS THE VARIABLE I'M TRYING TO SET AND THEN HAVE PASSED AS THE FOR LOOP PARAMETER
Set F="FILENAME"
REM IF I SKIP A VARIABLE AND HARDCODE THE FILENAME IN THE BELOW LINE, IT WORKS FOR THE ONE FILE
for /f "tokens=* delims= " %%a in (FILENAMEVARIABLE.txt) do (
Set /a N=!N!+1
echo !N! %%a, >> !F!.txt
)
The opening statement of the for loop, is where I can't get the variable to take. In FILENAMEVARIABLE.txt I have tried %%F, %F, !F!, and %%~nxf none of which manage to call the correct file to start the loop. Any ideas what I'm doing wrong here?
Here's what I think you were trying to do:
#Echo Off
SetLocal EnableDelayedExpansion
For %%A In (*.txt) Do (Set "N=0"
(For /F "UseBackQ Tokens=*" %%B In ("%%A") Do (Set /A N +=1
Echo !N! %%B))>"%%~nA2%%~xA")
You could also use FindStr:
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr /N "^" *.txt 2^Nul'
) Do >>"%%~nA2%%~xA" Echo %%B %%C
In both of the above examples I have assumed that the source directory and script directory are the same.

Need to fetch last word from two file using batch script and mail (or) display

I want to write a batch file, which will fetch last word from two files.
Open a file and fetch the last word and mail to me. Currently with the below code , file is opening. Kindly help to complete.
:Variables
set CURRENT_DATE=%date:~7,2%%date:~4,2%%date:~10,4%
for %%X in ("\\plserver01\GO\04052017\"*AmbFile1*.DAT) do notepad %%X
for %%W in ("\\plserver01\GO\04052017\"*BvtFile2*.DAT) do notepad %%W
pause
Regards,
Chs.
using notepad won't help, because your batchfile can not communicate with notepad. Use native batch to get the words:
#echo off
for /f "delims=" %%X in (file1.txt) do for %%Y in (%%X) do set "OneLast=%%Y"
for /f "delims=" %%X in (file2.txt) do for %%Y in (%%X) do set "TwoLast=%%Y"
echo last words in the two files are: %OneLast% and %TwoLast%
For how to send a mail, use our search box on top of the page.
Thanks for the update. I didn't get the files opened and got the value , the output is as given in quotes "last words in the two files are: and ", I think it may be a problem due to the space after the last word in the fil. please guide should i modify the code #echo off for /f "delims=" %%X in (\\pl\IL\Archive\GO\05052017\JIGF.JLB28.20170505.DAT) do for %%Y in (%%X) do set "OneLast=%%Y" echo last words in the two files are: %OneLast% and %TwoLast% due to space i did not given the second file.
I have replaced only the file path in your code and tried as given here. The file did not open. Clarify , If I am wrong. I know DotNet but totally new to batch script.
:Variables
set CURRENT_DATE=%date:~7,2%%date:~4,2%%date:~10,4%
pause
#echo off
for /f "delims=" %%X in (\\pl\IL\Ar\GO\07052017\ILL.20170507070447.DAT) do for %%Y in (%%X) do set "OneLast=%%Y"
for /f "delims=" %%X in (\\pl\IL\Ar\GO\07052017\ILL.20170507080404.DAT) do for %%Y in (%%X) do set "TwoLast=%%Y"
echo last words in the two files are: %OneLast% and %TwoLast%
pause

Batch file out put to both console and text

How can I save the out put of a batch file to a text file and the output should be displayed on console too.
For Eg:
#ECHO OFF
ECHO Hello World
#pause
It will show Hello World on console
#ECHO OFF
ECHO Hello World >text.txt
#pause
It will save Hello World to text.txt
How can I make it both happen together?
Thanks in advance
Use 'FOR' command if you want: (yea this one only execute 1 command)
For /f "tokens=*" %%a in ('ECHO Hello world') do (
echo %%a
echo %%a >text.txt
)
But the solution below only useful if you want to send both output to console and file a command that show a lots of texts like 'SET' command.
So what about you create an external batch file then use it in your main file?
Example: Code for 'printBoth.bat'
#echo off
setlocal ENABLEDELAYEDEXPANSION
set string=
:loop
set string=!string!%1
shift
if not "%1"=="" goto loop
For /f "tokens=*" %%a in ('!string!') do (
echo %%a
echo %%a >text.txt
)
Now if you want to print to both console and file, just type this code: call printBoth.bat [type command in here]
If you want to get the outputs of commands:
if exist "file.txt" del file.txt /f /q
for /f "delims=" %%k in ('command here') do (
echo %%k
echo %%k>>file.txt
)
Note the >>. Don't use >. > would remove all the text in the file, then write the first output-ed line in the command, which is not good if there were multiple lines that will be "echo-ed" by FOR. >> creates a new line instead of replacing the line like >.
DELIMS= works like TOKENS=*, but DELIMS= won't include the executed command.
I added if exist "file.txt" del file.txt /f /q, in order to not append the new output-ed lines. You can remove that if you want to append the lines to the file.
For customized ECHO outputs,
#echo off
echo TEXTHERE & echo TEXTHERE >>file.txt
echo TEXTHERE2 & echo TEXTHERE >>file.txt
rem ...and so on
<command1> & <command2> means "do <command1>, then do <command2>"
If you don't want to append, use > instead of >>.

Rename multiple files in a directory using batch script

I have about 1000 images and they have name like "IMG-12223". I want to rename them to 1 2 3 4 ... 1000. How can I do that. I have written a batch script which list the files but I don't know how to rename each file. e.g. rename first image with name "IMG-12223" to 1 , second image with name "IMG-23441" to 2 and so on ...
for /r %%i in (*) do (
echo %c%
)
Here's the script. Just put the script in your folder and run it.
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *') do (
if not "%%~nxi"=="%~nx0" (
ren "%%i" "!a!"
set /a a+=1
)
)
If you want to keep the extensions, i.e. rename "IMG-12223.jpg", "IMG-12224.jpg", etc to "1.jpg", "2.jpg", etc, you may use the following script.
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
ren "%%i" "!a!.jpg"
set /a a+=1
)
[Update] Here're explanations for the lines mentioned in Jack's comment.
setlocal EnableDelayedExpansion
In general, we want the variable a to be delayed expansion when it's executed but not the line is read. Without it, the variable a cannot get its increased value but always 1.
For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.
for /f "delims=" %%i in ('dir /b *.jpg')
Here dir with /b option, lists only file names of all jpg files.
The for loop traverses and renames all jpg files.
For the delims option, since the default delimiter character is a space, without the option delims=, it fails with the image files with spaces in the file names. I.E. for an image file named "img with spaces.jpg", without the option, the value of %%i is "img" but not the whole name "img with spaces.jpg", which is incorrect.
For for loop, please refer to the page http://ss64.com/nt/for_f.html.
if not "%%~ni"=="%~n0"
I have change it to if not "%%~nxi"=="%~nx0" to be more accurate. And the codes attached have been updated.
It's actually used to avoid to rename the bat file itself. If we limit the renaming only upon "jpg" files, then the line is not needed.
%%~nxi is the file name with extension for each file traversed. And %~nx0 is the running bat file with extension. For details, please refer to the page DOS BAT file equivalent to Unix basename command?.
There is no need for a batch script. A simple one liner from the command line can do the job :-)
I use DIR /B to generate the list of files, piped to FINDSTR to number the files, all enclosed withn FOR /F to parse the result and perform the rename.
for /f "delims=: tokens=1*" %A in ('dir /b *.jpg^|findstr /n "^"') do #ren "%B" "%A%~xB"
Double the percents if you want to put the command in a batch script.
Try this, you have pair of namevalues in a text file then loop values and do the magic. Namevalues are separated by empty spaces. This allows you to map old->new filenames accordingly. Or you keep idx+1 counter and use it for new filenames.
keyvalue.bat
#echo off
set idx=0
for /F "tokens=1,2" %%A in (keyvalue.txt) do call :PROCESS "%%A" "%%B"
GOTO :END
:PROCESS
set var1=%~1
set var2=%~2
set /A idx=%idx%+1
echo %var1% goes to %var2% (%idx%)
GOTO :EOF
:END
pause
keyvalue.txt
file888.dat newfile1.dat
file333.dat newfile2.dat
file9.dat newfile3.dat
file01.dat newfile4.dat

how do i get a batch file to accept input from a txt file?

I would like to run commands in a batch file on multiple computers.
For example:
#echo off
ping %1
pause
exit
If this batch file were called pingme.bat, and I type pingme.bat yahoo.com, then it would ping yahoo.com. My problem is I want the batch file to accept input from a text file.
For example, pingme.bat computers.txt would read the names of computers listed in the computers.txt file, and do whatever command I specified to be done to them.
%1 accepts the input I specify when I type the batch file. Now I would like the batch file to read the lines from that input text file and use them as arguments to the command.
The lines in the text are in list form, not using commas or anything.
One way to do so would be to place the URLS in a text file like so:
www.google.com
www.yahoo.com
Then run the following batch
for /f %%a in (%1) do (
echo Pinging %%a...
ping %%a
)
and run it from cmd as pingme.bat URLs.txt
Alternatively, you may specify the text file's name within the batch, and run it without the parameter
for /f %%a in (URLs.txt) do (
echo Pinging %%a...
ping %%a
)
Here's another approach
This particular batch will pull from the list, and write to output.txt if the ping was successful
#ECHO OFF
SET output=output.txt
IF EXIST "%output%" DEL "%output%"
FOR /f %%a IN (URLs.txt) DO (
CALL :ping %%a
)
GOTO :EOF
:ping
ping -n 1 %1 | find "Approximate round trip" >NUL || ECHO %1>>"%output%"
Hopefully that sets you in the right direction.
You may use a FOR loop - save this as pingme.bat:
FOR /F "tokens=*" %%L IN (%1) DO (
ping %%L
pause
)
and call it with the text file as parameter pingme.bat computers.txt.
To find IP addresses of multiple URL's in text file and to get the output in text file:
FOR /F "tokens=*" %%L IN (%1) DO (
nslookup %%L >> output.txt
pause
)
Save the script as "ping.bat" and call ping.bat URL.txt from the command prompt.

Resources