How to input batch variable information into a website? - batch-file

I am working on a decoder, and as of now, I have the main part. I was wondering if you could set a variable in batch (via set /p) and put that variable into a website's input box. Then how would I continue to decode.
#echo off
setlocal EnableDelayedExpansion
Set /P Input=[Enter Input Here]
for /F "tokens=1,2 delims=()" %%a in ("%input%") do (
set bit=%%a
set "output="
for /F "delims=" %%c in ('cmd /D /U /C echo %%b^| find /V ""') do (
for /L %%i in (1,1,%%c) do set "output=!output!!bit!"
set /A "bit=^!bit"
)
)
echo %output%
How would I put output into the "binary to decode" section and put click the "to text" button from batch, then echo the txt output from the website. If this includes vb or vbs, you would have to include the program for me. I don't know any vb/vbs. Here is the site: http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_to_Text.asp

Why are you even asking this question? It isn't possible. I know everything in batch and HTML. If you want to do that, you can do it in PHP.

Related

Find a string into each file of a folder with for loop

I try to find a string "blabla" inside a file. If I find the string I replace it with "bloblo", if not do nothing. And I want to do this operation for all the files that I have in a folder.
When I apply the script on one file, it works well. When I use a for loop it doesn't work. it does not enter inside the for loop
Here is my script:
set "Var1=blabla"
set "Var2=bloblo"
FOR %%i IN (C:\Users\UserName\Desktop\TEST\*.txt) DO (
find /c "%Var1%" %%i
if %errorlevel% equ 1 goto notfound
call PathFile\FUNCTION_REPLACE.cmd %Var1% %Var2% %%i
:notfound
)
I use a script "Function_replace" to replace the values, here is the script:
SetLocal EnableExtensions DisableDelayedExpansion
Set "search=%~1"
Set "replace=%2"
Set "File=%3"
For /F "Tokens=1*Delims=]" %%A In ('Find /V /N ""^<"%File%"^&Break^>"%File%"'
)Do (Set "line=%%B"
SetLocal EnableDelayedExpansion
(If Not "%%B"=="" (Echo(!line:%search%=%replace%!)Else Echo()>>"%File%"
EndLocal)
I used to use directly the call of my replace function for each files of my folder. But it takes a long time to go inside each file. That's why I want to check before going inside each file, if the string exists or not (by using find /c).
Can you spot where is the issue coming from?
Based upon your latest edit, here's an example which sends only the files containing the string to be replaced, to the sub-function:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "search=%~1"
Set "replace=%~2"
For /F Delims^=^ EOL^= %%A In ('FindStr /LIMP "%search%" *.*')Do Call :Sub "%%A"
GoTo :EOF
:Sub
Set "File=%~1"
For /F "Tokens=1*Delims=]" %%A In ('Find /V /N ""^<"%File%"^&Break^>"%File%"'
)Do (Set "line=%%B"
SetLocal EnableDelayedExpansion
(If Not "%%B"=="" (Echo(!line:%search%=%replace%!)Else Echo()>>"%File%"
EndLocal)
Please note that the FindStr command uses only example options, whilst the /M option is important to this solution you should check the output from FindStr /? to determine which of the others you would consider the most appropriate.
Why not simply list all files that has the string and exclude the files that don't? That seems like a more obvious plan to me:
#echo off
set "search=blabla"
for /f "delims=" %%i in ('findstr /m %search% "%USERPROFILE%\Desktop\TEST\*.txt"') do (
echo found "%search%" in "%%~fi"
)
If you want to still show files that does not contain the path, then #stephan already posted a comment on how to, no need for me to repeat.

Error when using findstr on file directory

So I'm trying to learn the ins and outs of findstr since it's come up a few times on other batch script questions I've had. I'm trying to have it look for a word (in this case 'webview') through multiple files in a directory, ideally it would pull the line it was found as well as the file name. However, the program gets stuck in this sort of infinite loop with it and I have to force an exit. Any help on if its being caused by the findstr or what is causing it would be amazing as I've been staring at it for several hours now. My current code is below:
ECHO off
SETLOCAL enabledelayedexpansion
ECHO Please input the path to the app directory you'd like scanned
SET /p directorypath=
CD %directorypath%
ECHO Scanning files for Webview
(
FOR /F "delims=" %%a in ('findstr /I /S /M "webview" *.json') DO (
SET "line=%%a"
SET "line=!line:*webview=!"
FOR /F "delims=<" %%b in (!line!) DO ECHO %%b
)) > WebviewScanResults.txt
:eof
UPDATE: Code updated and functional for use as a reference. I pretty much just run the above code a couple of time with different file types replacing *.json and it works fine.
Just an untested try, too late for me:
#ECHO off
SETLOCAL enabledelayedexpansion
ECHO Please input the path to the app directory you'd like scanned
SET /p directorypath=
PushD "%directorypath%"
ECHO Scanning files for Webview
(
FOR /F "tokens=1*delims=:" %%a in ('findstr /I /S "webview" *.html') DO (
SET "line=%%b"
SET "line=!line:*webview=!"
FOR /F "delims=<>" %%b in ("!line!") DO ECHO %%a:%%b
)) > WebviewScanResults.txt

Batch File: Assign random line of text file as variable for later use

I'm trying to write a very simple batch file for personal use...It's complete except for one thing I'm stumped on. Hopefully this is an easy fix (I'm effectively illiterate when it comes to code).
Basically what I'm trying to do is have the script choose a random line from a text file, do this a couple times with a couple different text files, then I wish to assign the output from each text file to a variable so that I can easily use them in various combinations...then repeat the process.
Here is what I have right now...
#ECHO OFF
:START
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions
SET "list1=list1.txt"
FOR /f %%a IN ('type "%list1%"^|find /c /v ""') DO SET /a numlines=%%a
SET /A list1random=(%RANDOM% %% %NumLines%)
IF "%list1random%"=="0" (SET "list1random=") ELSE (SET "list1random=skip=%list1random%")
FOR /F "usebackq tokens=* %list1random% delims=" %%A IN (`TYPE %list1%`) DO (
>> output.txt ECHO %%A
)
:Finish
ENDLOCAL
GOTO START`
This procures the random line, and spits it to a text file. All is well, next step, take that random result and assign it to a variable...
#ECHO OFF
:START
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions
SET "list1=list1.txt"
FOR /f %%a IN ('type "%list1%"^|find /c /v ""') DO SET /a numlines=%%a
SET /A list1random=(%RANDOM% %% %NumLines%)
IF "%list1random%"=="0" (SET "list1random=") ELSE (SET "list1random=skip=%list1random%")
FOR /F "usebackq tokens=* %list1random% delims=" %%A IN (`TYPE %list1%`) DO (
SET output1=%%A
)
>> output.txt ECHO %output1%
:Finish
ENDLOCAL
GOTO START
Now the output ceases to be random...instead it is always the last line of the referenced text file.
EDIT: The site suggested another question that was similar to mine. However, that person was having trouble getting the script to choose a valid line. I get a valid line every time, and a random one too (when I check it via echo), but a non-random line when proceeding on, assigning the output to a variable. I don't understand because it seems like a post-facto derandomization. I.E. the difference between the two scripts has nothing to do with procuring the random result, only what to do with that result AFTER it has it, right?
I appreciate any help in advance, this is the last step before I know everything I need to finish this, I'm excited!
Sorry, you're right...anyways, I figured out a simple workaround, probably not the quickest in terms of processing time, but whatever. Basically allow the initial part of the script to spit out the random result to a text file (as seemed to work just fine) then reference the text file as a variable.
#ECHO OFF
:START
SET "list1=list1.txt"
FOR /f %%a IN ('type "%list1%"^|find /c /v ""') DO SET /a numlines=%%a
SET /A listchoice=(%RANDOM% %% %NumLines%)
IF "%listchoice%"=="0" (SET "listchoice=") ELSE (SET "listchoice=skip=%listchoice%")
FOR /F "usebackq tokens=* %listchoice% delims=" %%A IN (`TYPE %list1%`) DO (
>> listoutput.txt ECHO %%A
)
Set /p list=<listoutput.txt
>> result.txt ECHO %list%
:Finish
DEL listoutput.txt
GOTO START
This is easy to do in PowerShell using the built-in Get-Random cmdlet.
$line = (Get-Content file.txt | where { $_ } | Get-Random)
Which makes it also easy in batch.
set filename=file.txt
for /f "tokens=*" %%a in ('powershell -ex bypass -c "gc %filename% | ? { $_ } | Get-Random"') do (
set "var=%%a"
)
The where { $_ } clause is only necessary to filter out any blank lines. You can omit it if you know your file has none.

Batch - Search for part/exact name and copy line from text file into batch as var

This information below is contained in a text file and formatted as such.
/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7
I also have a variable called %file_name% in the batch file already defined.
So lets say that is it is %file_name% = V001-video_folder_6.mp4
As you can see there is some more extra information, V001- and .mp4.
I would like to use the var %file_name% to search the text file and return the entire line. In this case it would return /var/www/xxx/html/videos/video_folder_6 and then put this information in a new var, let us say, %folder_path%.
I think I would use findstr however I have been playing around and not getting the best results.
The problem with the methods that use findstr is that they are slow, because they require to execute findstr.exe (a ~30KB file) each time. A simpler/faster solution is to use just internal Batch commands with the aid of an array. If the number of names to process is large, the difference in time between the two methods may be marked.
#echo off
setlocal EnableDelayedExpansion
rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
set "line=%%a"
for %%b in (!line:/^= !) do set "lastPart=%%b"
set "folder[!lastPart!]=%%a"
)
set "file_name=V001-video_folder_6.mp4"
rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"
echo Folder path is: %folder_path%
Let us assume the posted lines are in file Test.txt in current working directory.
#echo off
set "file_name=V001-video_folder_6.mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto NextCommand
)
:NextCommand
echo Full folder path is: %folder_path%
Open a command prompt window, enter the command for /?, hit key RETURN or ENTER and read output help to understand this little code.
The command goto inside FOR loop results in an immediate exit from loop processing output of findstr.exe after first found line containing the folder path of interest.
Perhaps better in case of searched folder is not found in text file:
#echo off
set "file_name=V01-VIDEOS for school (Miss Patrick).mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto FoundFolder
)
echo "%folder%" not found in file Test.txt.
pause
goto :EOF
:FoundFolder
echo Full folder path is: "%folder_path%"
pause
This should work:
::file_name=V001-video_folder_6.mp4
::file containing folder paths is called paths.txt
for /f "tokens=2 delims=-." %%a in ("%file_name%") do set FN=%%a
for /f %%a in ('findstr /E /L "%FN%" "paths.txt"') do set folder_path=%%a
echo %folder_path%
Which does what you want in effectively two lines.

Replace a character in a file using a batch script

I am new to batch file scripting and need to develop a script to replace a character in a file using a batch script.
I have to replace "servername/ActionService.asmx,1"
with "servername/ActionService.asmx,0"
in file called APP.
Please let me know if there is any solution using only commands.
You can use GNUWin32 sed:
#ECHO OFF &SETLOCAL
set "string=servername/ActionService.asmx,1"
FOR /f %%a IN ('echo "%string%" ^| sed "s/[0-9]/0/"') DO set "newstring=%%~a"
ECHO %newstring%
If you're toggling back and forth between these two states, it might be easier to create two copies of the file with different names, together with a couple of batch files (e.g. actionService1.bat and actionService2.bat) to copy the appropriate file over your APP file.
Otherwise you might consider getting Windows versions of the Unix tools sed and awk, which excel at this type of file manipulation.
The Batch file below assume that there is precisely one line with the target string. This method is relatively fast.
#echo off
for /F "delims=:" %%a in ('findstr /N "servername/ActionService.asmx,1" theFile.txt') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" theFile.txt do (
set "line=%%b"
setlocal EnableDelayedExpansion
if %%a equ %lineNum% (
echo !line:1=0!
) else (
echo(!line!
)
endlocal
)) > theFile.new
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: Way the first - suppresses emptylines
FOR /f "delims=" %%i IN (app) DO SET line=%%i&set line=!line:servername/ActionService.asmx,1=servername/ActionService.asmx,0!&ECHO(!line!
ECHO ====================
:: Way the second
FOR /f "delims=" %%i IN ('type app^|findstr /n "$"') DO (
SET line=%%i
set line=!line:servername/ActionService.asmx,1=servername/ActionService.asmx,0!
SET line=!line:*:=!
ECHO(!line!
)
ECHO ====================
GOTO :EOF
Two ways here. You would need to redirect your choice to a new file as you cannot update in-place.
Doing a quick google search I found this http://www.dostips.com/?t=Batch.FindAndReplace
Using a helper batch file called repl.bat from here: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
type app |repl "servername\/ActionService.asmx,1" "servername/ActionService.asmx,0" >appnew.txt

Resources