Saved Bookmarks
| 1. |
Solve : Reading text file line by line and do some operations - Windows Batch File? |
|
Answer» What I am TRYING to do here is to parse every row in filename.txt file and then run 2nd FOR loop to output it into another text file. Filename.txt has multiple rows which looks something like this: [email protected]|Filename.csv [email protected]|Filenam1.csv [email protected]|Filename.csv and so on. I need help in building a script to read the lines one by one for example I want to read [email protected]|Filename.csv 1st and then do some operations and after the operations are done, I want to to goto the next line and do the same operations. The output of the operations will be stored in a file and will be overwritten every time the loop counter increments and goes to the next line. My output file should contain only 1 row at a time and not all the rows. What I am thinking is to create a counter and then use that counter in the for loop and then do the operations but I am not getting the desired output. I went through the threads where something similar was asked but could not solve my issue.One more important point there is a pipe ('|') in the text file. I have tried this, till now: set /a count=0 for /F "tokens=*" %%a in (C:\filename.txt) do ( Set /a count+=1 Set output[!count!]=%%a echo %output[!count!]% Pause ) I am not ABLE to call or access this outside the loop, like I would like to use the 1st row which will be represented by counter=1 first in a different for loop do some operations and then come back and increment the counter=2 pointing to the 2nd row and do the same operations.Lets say we have a file called file.txt. Within that file I have the following rows: [email protected]|Filename.csv [email protected]|Filenam1.csv [email protected]|Filename.csv So what I want is to read/access each line one at a time. For instance, 1st we access [email protected]|Filename.csv then do some operation where I will be inputing [email protected]|Filename.csv into a temp file and use this file in a different for loop for some other operations. After this is done I want to goto [email protected]|Filenam1.csv and do the same operations and finally [email protected]|Filename.csv. Every time the increment happens, the temp file will be overwritten with the current row field and we will exit out of the loop when %1 = ''. I hope I have explained my problem clearly.Hi We can do like this script without using a temp file : Code: [Select]echo off set "File=C:\filename.txt" set /a count=0 SETLOCAL enabledelayedexpansion for /F "tokens=* delims=" %%a in ('Type "%File%"') do ( Set /a count+=1 Set "output[!count!]=%%a" ) For /L %%i in (1,1,%Count%) Do ( Call :Action "!output[%%i]!" pause ) Exit ::******************************************************* :Action echo We treat this line : %1 exit /b ::*******************************************************I've subscribed to this forum just to thankyou "Hackoo" for your solution. Worked like a charm. Cheers! Quote from: jonasgozdecki on September 03, 2019, 08:01:52 AM I've subscribed to this forum just to thankyou "Hackoo" for your solution.jonasgozdeck You are very welcome and anytime too Bro Have a nice day I have JOINED this form because this is as close to what I'm looking for as I can find... I want to take what you've created here and be able to select a line by choice and then use those variables only on that line... My objective is to take something like the following - Code: [Select]Variable1 Variable2 Variable3 Variable4 Description1 ComputerName1 ComputerUser1 ComputerPassword1 Description2 ComputerName2 ComputerUser2 ComputerPassword2 Description3 ComputerName3 ComputerUser3 ComputerPassword3 Description4 ComputerName4 ComputerUser4 ComputerPassword4 Description5 ComputerName5 ComputerUser5 ComputerPassword5 Description6 ComputerName6 ComputerUser6 ComputerPassword6 And then use it for creating something that would look like this - Code: [Select]Variable1's listed from text file - Description1 Description2 Description3 Description4 Description5 Description6 Make a selection: ((User ENTERS Variable1)) cmdkey /generic:"<line1variable2>" /user:"<line1variable3>" /pass:"<line1variable4>" <-------from a specific line a user selects mstsc /v:"<line1variable2>" /f <-------from a specific line a user selects Any help would be greatly appreciated... Quote from: Hackoo on June 13, 2017, 08:26:55 PM HiI had reposted this question as my own and Sidewinder answered! Here's his perfect reply -- thanks! Quote from: Sidewinder on May 21, 2020, 12:03:41 PM Batch language is truly ugly. That said, the batch LOGIC here is to create an in-memory array (of sorts), let the user make a selection and then create the parameters for the cmdkey and mstsc commands.My data in txt file is paths. I used command Code: [Select]start %1% to open a file, but it don't work. This is my code: Code: [Select]echo off set "File=D:\listpathfile.txt" set /a count=0 SETLOCAL enabledelayedexpansion for /F "tokens=* delims=" %%a in ('Type "%File%"') do ( Set /a count+=1 Set "output[!count!]=%%a" ) For /L %%i in (1,1,%Count%) Do ( Call :Action "!output[%%i]!" pause ) Exit ::******************************************************* :Action echo We treat this line : %1 start %1% exit /b ::*******************************************************Any help I appreciate and welcomelinhkythuat You should try something like that : Code: [Select]echo off set "File=D:\listpathfile.txt" set /a count=0 SETLOCAL enabledelayedexpansion for /F "tokens=* delims=" %%a in ('Type "%File%"') do ( Set /a count+=1 Set "PathFile[!count!]=%%~a" ) For /L %%i in (1,1,%Count%) Do ( Call :Action "!PathFile[%%i]!" pause ) Exit ::----------------------------------- :Action echo We treat this line : "%~1" to start "%~nx1" start "%~nx1" "%~1" exit /b ::----------------------------------- |
|