Write a specific line from a text file to another text file using VBS?

I'm currently using batch to create an online updater. I have a file containing the current online version, and I want to copy the download link from the second line to another file so that another batch script can open it. Since this is almost impossible in batch, I would like to implement this read-and-rewrite script in VBS.

If anyone is interested, the batch script is here:

 echo off set /p systemversion=<systemversion.dat cd downloads if exist "currentonlineversion.dat" del currentonlineversion.dat cd.. title BatchOs Downloader cls echo Please move the downloaded file to the opened folder! echo Press any key to begin the download. pause >nul ::open.vbs öffnet den downloads Ordner:: start open.vbs start [Downloadlink zu currentonlineversion.dat mit Version und link] cd downloads :waitloop if exist "currentonlineversion.dat" goto checkforpossibleupdate goto waitloop :checkforpossibleupdate set /p onlineversion=<currentonlineversion.dat echo Checking for updates... echo. if not %systemversion%==%onlineversion% goto downloadupdate

Looking forward to your answers.

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Erzesel
1 year ago

BatchOs

… again one of the tries to make his own "OS" by batch

well, you're a bloody beginner who's deceived…🤗

Batch is one of the most barbaric languages ​​you can do. (unless you are looking for an extreme challenge) . My very special playground: ( so you can see what "restrictions" are necessary to control Batch in a way )

The VBScript frame is not necessary. especially since VBS is technically at the stand of 2009 and on modern systems has increasingly problems with certain file names and some of the snap points of M$ have been deactivated…

But back to your batch:

A certain line from a file reading Skip indicates how many lines (with content) are skipped when reading: (for/f ignores empty lines)

demo.cmd

 for /f "usebackq skip=1 tokens=*" %%a in ("Datei.ext") do ( >"andere Datei.txt" echo %%~a rem lesen nach der aktuellen Zeile beenden goto :breakLoop ) :breakLoop

…with the whole file

Demo.cmd

 @echo off chcp 65001 >nul &rem UTF-8 Codepage wegen evtl Umlaute usw. title BatchOs Downloader &rem immer am Anfang rem kann nur 1. Zeile lesen set /p "systemversion=" < "systemversion.dat" rem Downloadfolder des aktuellen Nutzers set "DownloadFolder=%UserProfile%\Downloads" rem lösche ist eigendlich quatsch, wird beim späteren automatischen Download ohnehin überschrieben (2>nul FehlerOutput unterdrücken) del "%DownloadFolder%\CurrentOnlineVersion.dat" 2>nul rem downloade "currentonlineversion.dat" curl " http://DeineDomain.blubb/currentonlineversion.dat " -o "%DownloadFolder%\CurrentOnlineVersion.dat" 2>nul &&( rem wenn kein Fehler goto :read2eZeile )||( rem bei fehler echo Fehler bei Download "currentonlineversion.dat" pause exit /b ) :read2eZeile rem überspringe beim lesen der Datei eine Zeile und lies die Zeilen komplett for /f "usebackq skip=1 tokens=*" %%a in ("%DownloadFolder%\CurrentOnlineVersion.dat") do ( set "Link=%%~a" rem lesen nach einer Zeile beenden goto :breakLoop

but why the circumstances?

My interpretation of "CurrentOnlineVersion.dat" … so saves I will make you a lot of individual variable assignments later with a slide.

CurrentOnlineVersion.dat

 OnlineVersion=6.6.0815 OnlineArchiv= comment image OfflineArchivName=Erzesel.jpg Disclaimer.001=Das ist die erste Zeile eines ErklärungsTextes Disclaimer.002=statt eines Archivs gibts mein Foto Disclaimer.003=andereZeile blubb und blah

OfflineDemo.cmd

 @echo off chcp 65001 >nul set "DownloadFolder=%UserProfile%\Downloads" rem zur demo statt direkten Online lesens nur die Datei im aktuellen Ordner direkt in Variablen verwandeln. for /f "usebackq tokens=*" %%a in ("CurrentOnlineVersion.dat") do (set "%%~a") echo Die aktuelle OnlineVersion ist: "%OnlineVersion%" echo Link auf das Onlinearchiv: "%OnlineArchiv%" rem DisclaimerVariablen anzeigen: for /f "tokens=1* delims==" %%a in ('set "Disclaimer."') do (echo %%b) rem lass uns mal das Arcive (aka Foto) abholen curl "%OnlineArchiv%" -o "%DownloadFolder%\%OfflineArchivName%" 2>nul rem und mit der standardanwendung öffnen: start "" "%DownloadFolder%\%OfflineArchivName%" pause

directlyOnlineRead in.cmd

 @echo off chcp 65001 >nul set "DownloadFolder=%UserProfile%\Downloads" rem direkten Online lesen und direkt in Variablen verwandeln. for /f "tokens=*" %%a in ('curl " http://DeineDomain.nix.da/CurrentOnlineVersion.dat "') do (set "%%~a") echo Die aktuelle OnlineVersion ist: "%OnlineVersion%" echo Link auf das Onlinearchiv: "%OnlineArchiv%" for /f "tokens=1* delims==" %%a in ('set "Disclaimer."') do (echo %%b) curl "%OnlineArchiv%" -o "%DownloadFolder%\%OfflineArchivName%" 2>nul start "" "%DownloadFolder%\%OfflineArchivName%" pause

actually sausage, from which data is collected. in Batch, the For/f-loop is the hub for all data exchange

whether you relate data from a file or from a program output is irrelevant…

display file with curl directly:

 curl " https://filesamples.com/samples/document/txt/sample3.txt "

You can also use this command in for/f (Leerlines are ignored):

 for /f "tokens=*" %%a in ('curl " https://filesamples.com/samples/document/txt/sample3.txt "') do (echo Inhalt der Zeile: "%%~a")

something else to your pardon:

with pushd "directory" and pope you can just get back to where you came from:

rem to the download folder of the current user

 pushd "%UserProfile%\downloads" echo bin in "%cd%" rem wieder dahin zurück, wo du zuvor warst popd echo bin wieder zurück: "%cd%"

and if the For/f-Loop burns your brain away, you know why Powershell was invented and replaced Cmd as Console as standard from Windows 10.

But that's another story…

Do not plug too much energy into Batch/Cmd anymore. In the next few years, Batch may disappear completely. Microsoft has already sent one of the most important batch commands (WMIC.exe) from the Windows 11 developer versions. In this way, deep system data for Batch are no longer directly accessible… so Batch is in the A****

If you want to work safely with ConsoleScript, learn Powershell.