To clarify it First i'm making a url shortener script that use input.txt file as input and outputs as an output.txt where you can put unlimited links in the input.txt
This is what i got
#echo off
set /p firstline=<input.txt
echo %firstline%
for %%a in (%firstline%) DO (
set "text=http://adfoc.us/api/?key=c803bc5b2f2e8ad5ccb0166d4bc898ae^&url=%%a"
)
echo %text%>output.txt
example input.txt has 600 lines and i want to make them all in output.txt
I assumed that you have an input.txt file that contain 600 lines with just the url part, and that you want to complete that lines with fixed http and key parts. If this is true, then the Batch file below do that:
#echo off
(for /F "delims=" %%a in (input.txt) DO (
echo http://adfoc.us/api/?key=c803bc5b2f2e8ad5ccb0166d4bc898ae^&url=%%a
)) >output.txt
Antonio
PS - If my answer is right, then you are NOT making an "url shortener script", but precisely the opposite thing! This script take an url and enlarge it by adding http and key parts, isn't it?
Related
I'm still pretty new to batch but I wanted to try out some different functionalities within the same batch program. Essentially, I am attempting to parse through a .txt file of hostnames and ping them 1 time each, and return a pass or fail by utilizing a find query for the specific ping result. The second find query is redundant but left in. A string of "timed out" indicates that the host is down. I have been able to achieve this with this bulky code, but my actual ping statistics are no longer being written to this log file.
I'm not too familiar with the command pipeline character but it may just be incompatible for this specific use case. If I remove the piped command for the find query, it is able to write the output of the ping command just fine, but then I lose the utility of a return pass or fail value. Is this just a simple syntax error? I've tried moving the location of the actual write to file argument on the line itself but it hasn't worked. Also, why are the errorlevel values inverted (line 21)? I can't seem to return what I'm looking for.
Is there anyway I can get all these parts to play nice together? I apologize if the answer is quite obvious...
#echo off
setlocal
set hosts=temp.txt
set count=0
echo.
echo Parsing File: %hosts%
echo.
echo Start > C:\Users\___\Downloads\pingLOG.txt
for /F "tokens=*" %%i in (%hosts%) do (
set /A count=count+1
echo+
echo.
echo [+] Pinging: %%i
echo [+] Pinging: %%i >> C:\Users\___\Downloads\pingLOG.txt
echo.
ping -n 1 "%%i" | find /I "timed out" >> C:\Users\___\Downloads\pingLOG.txt
if errorlevel == 1 (
echo Pass
) else (
echo Fail
)
echo.
echo %TIME% >> C:\Users\___\Downloads\pingLOG.txt
)
echo.
echo %count% Hosts Scanned
find /c "timed out" C:\Users\___\Downloads\pingLOG.txt
echo.
pause
You can't filter the output for a certain string and expect the complete output (eliminating unwanted parts is the very reason for filtering).
To accomplish your goal, you need a temporary file (the complete output) and filter that file, so you have both variants (filtered and unfiltered) (suboptimal, but well...):
ping -n 1 "%%i" >"%temp%\temp.tmp"
find /I "timed out" "%temp%\temp.tmp" >nul && set "status=Fail" || set "status=Pass"
type "%temp%\temp.tmp" >> "C:\Users\___\Downloads\pingLOG.txt"
echo %status% >> "C:\Users\___\Downloads\pingLOG.txt"
This is what i currently get with FOR loop:
FOR /R "ProgramFolder" %%P in (*) do (
echo %%P
)
C:\Folder\Folder2\ProgramFolder\Managed\bolt.dll
C:\Folder\Folder2\ProgramFolder\Resources\fmodstudio.dll
C:\Folder\Folder2\ProgramFolder\Plugins\dll\DynamicWaterNativeWrapper.dll
How to get pathes like this?
Managed\bolt.dll
Resources\fmodstudio.dll
Plugins\dll\DynamicWaterNativeWrapper.dll
edited on dbenham comments
#echo off
setlocal enableextensions disabledelayedexpansion
pushd c:\somewhere\ProgramFolder
for /f "tokens=1,* delims=\" %%a in (
'xcopy . "%temp%" /l /s'
) do if not "%%b"=="" echo(%%b
popd
The basic idea behind this code is to use the xcopy command not to copy, but to retrieve a list (/l) of the files that should be processed with relative paths. For it to work, it is necessary to first change the current active directory to the required one (pushd) and use a relative reference to the current folder (.)
The output of xcopy command with this configuration will be in the form
.\folder\folder\file.ext
To remove the prefixing dot and backslash the for /f is configured to use the slashes as delimiters and to retrieve the first token (the dot) before the first backslash and the rest of the line as the second token, that is, the relative paths
As the xcopy command output includes an aditional line with the total number of files, and this line will result in an aditional blank line in the output of the script, an aditional if is included to discard this line.
i am trying to parse the output of another function which is output line by line. to understand the function, it returns several lines of parameter and numbers like "top=123456789" or "low=123456789" (without the quotations) -
i try to parse the lines now with
for /F "delims=" %%a in ('%%I ^| findstr top') do set updir=%%1
set "updir=%1:~4%"
echo. %updir%
i am trying to get the pure numbers by trimming the known keywords like top, which would need then to be set to a var to return (%~1% ???) to a calling function back (other batch file).
could anyone help me with this please? shure it would be better to trim right from "=".
UPDATE:
this is the code returning the lines from the script i linked. i tried several ways to parse the return but i seem to be blind or too stupid to see, all is going weird.
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
rem process the HTML line-by-line
org echo(%%I
try1 (echo %%I|findstr top
try2 for /F "delims=" %%a in ('%%I ^| findstr top') do set updir=%%a
try2 echo. %updir%
try3 for /F "delims=" %%a in ('%%I') do findstr top
try3 echo. %2%
)
didn't work either
for /F "tokens=1,2delims==" %%a in ('%%I') do if %1 == top set updir=%%b
echo %updir%
i tried both delim version beneath (too the tokens/delims version) but i don't get it right.
UPDATE SOLUTION:
for the ones reading the question here some additional comment:
rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"
rem test that trimmed line matches "variable=number"
to find a single item like e.g. "top" you have to add "to" or adjust whole first token
echo !line! | findstr /i "^to[a-z]=[0-9]" >NUL && (
rem test was successful. Scrape number.
for /f "tokens=2 delims==" %%x in ("%%I") do set "value=%%x"
echo !value!
)
If all you wish to do is to is to skip all lines until you find one that matches "text=numerals", then scrape the numeric portion of that line, all you need to do is this:
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
rem trim whitespace from beginning and end of line
for /f "tokens=*" %%x in ("%%~I") do set "line=%%x"
rem test that trimmed line matches "variable=number"
echo !line! | findstr /i "^[a-z]*=[0-9]*$" >NUL && (
rem test was successful. Scrape number.
for /f "tokens=2 delims==" %%x in ("%%I") do set "value=%%x"
)
)
I think that's right, anyway. I didn't test it.
But I suspect that this is not going to work as you intend, since what you are scraping will probably include HTML tags. We will probably not be able to help you scrape the HTML unless you pastebin the HTML source of an example page, and explain what you wish to scrape from that source example.
does this fit your needs?
for /F "delims=" %%a in ('type file.txt ^| findstr "top low"') do set /a %%a
set top
set low
echo %top%, %low%
Try this:
for /F "tokens=2delims==" %%a in ('findstr top file.txt') do set "updir=%%a"
echo.%updir%
According to your comment my new code:
#echo off &setlocal enabledelayedexpansion
set "string=%%I"
set "string=!string:*top=!"
for /f "delims== " %%z in ("!string!") do set "string=%%z"
echo !string!
.. output:
123456789
Edit2: "added.
Im attempting to get a block of text from a file I have already created and store that information into a new file. The text file id like to parse is set up like this:
HostName: xxxxxx
(8 or so lines of information)
HostName: xxxxxx
The largest problem I am having is that between the blocks of text I am specifically looking for the word "admin". I am able to find and return the line with the word admin in it, now I need to write a loop to grab the information above and below if the word admin is found within that block of text (I was going to use an empty line as the delimiter as thats consistent throughout my file).
Ive written a few for loops but none of them have given me what id like. Any help would be appreciated.
Thanks
The program below do what you want:
#echo off
setlocal EnableDelayedExpansion
rem Create the info removing empty lines
(for /F "delims=" %%a in ('schtasks /query /fo list /v') do echo %%a) > schtasks.txt
rem Add an additional "HostName:" line at end as delimiter
echo HostName: >> schtasks.txt
rem Create a vector of number of lines containing "HostName:"
set i=0
for /F "delims=:" %%a in ('findstr /N "HostName:" schtasks.txt') do (
set /A i+=1
set header[!i!]=%%a
)
rem Seek for the LINES containing "admin"
for /F "delims=:" %%a in ('findstr /N /I "administrator" schtasks.txt') do (
rem Seek for the NEXT section that contains "admin" line
for /L %%i in (1,1,%i%) do if %%a gtr !header[%%i]! set thisSection=%%i
rem Locate that section
set /A start=header[!thisSection!], nextSection=thisSection+1
set /A end=header[!nextSection!]-1
rem ... and show it
set line=0
for /F "delims=" %%a in (schtasks.txt) do (
set /A line+=1
if !line! geq !start! if !line! leq !end! echo %%a
)
echo ----------------------------------------------
)
del schtasks.txt
PS - Of course that MS-DOS/Windows Batch is a programming language!
I was wondering if it's possible to use findstr and/or gawk to return the output of a windows cmd exactly how I need it to be. I'm currently returning the output raw, then stripping out the blank lines, and parsing out what I need. Just as a learning thing, I was hoping I could see how this can be done better.
The raw output:
Change 2086888 on 2012/01/23 by user1#server1
test_description_1.2.3.4#29816
Change 2086888 on 2012/01/23 by user1#server2
test_description2_4.5.6.7#29816
Change 2078677 on 2012/01/20 by user2#server1
test_description3_7.8.9.10#29816
I take that output and parse it out to this with php:
1. 2086888,test_description_1.2.3.4#29816
2. 2086888,test_description2_4.5.6.7#29816
3. 2078677,test_description3_7.8.9.10#29816
To make it a little easier on myself, I remove the blank lines from the output by piping it to findstr and using /V "^$". So it's | findstr /V "^$".
How can I get the output that I parse out with php directly from the command line?
for command in cmd.exe skips blank lines, so it may be easier to parse it this way:
#Echo Off
setlocal enabledelayedexpansion
set linenum=1
for /f "tokens=1,2" %%A in (output.txt) do #(
if %%A==Change set result=%%B
if not %%A==Change (
echo !linenum!. !result!,%%A
set /a linenum=!linenum!+1
)
)
endlocal