Advertisement
BrainWaveCC

ReadAFile.BAT

Jul 17th, 2024 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.36 KB | Source Code | 0 0
  1. @REM - ReadAFile.BAT (17 Jul 2024 // 17 Jul 2024): Innovative Way to Read a File and Save Each Line as a Variable
  2. @ECHO OFF
  3.  
  4.  ::: This script will read an input file with up to 1000 lines (#MAXLINES=999)
  5. ::: and save each line as a variable (#LINE000000 to #LINE000999) as necessary.
  6. ::: How high you can go will depend on system memory, and possibly some other
  7. ::: heap or stack resource.  The largest file I tested was one with 823 lines.
  8. :::
  9. ::: You would call this from the command-line as:
  10. :::   ReadAFile.BAT "x:\Some\Folder\InputFile.TXT
  11. ::: -----------------------------------------------
  12. :::
  13. ::: Full script can be found here .....  https://pastebin.com/4am7S36Y
  14. :::
  15. ::: Tested on Windows 10 x64 and Windows 11
  16. :::
  17.  
  18.  
  19. :Variables
  20.  SETLOCAL ENABLEDELAYEDEXPANSION
  21.  SET #MAXLINES=999
  22.  SET #FILENAME="%~1"& IF "%~1"=="" SET #FILENAME="x:\Some\Folder\SomeFile.TXT"
  23.  
  24.  
  25. :Check4File -- Check for the existence of the input file
  26.  IF NOT EXIST %#FILENAME% (
  27.      ECHO FILE NOT FOUND: %#FILENAME%
  28.      GOTO :ExitBatch
  29.  )
  30.  
  31.  
  32. :ReadFile -- Read a file up to maxlines+1 (0 - #MAXLINES)
  33.  < %#FILENAME% (
  34.      FOR /L %%C IN (0,1,%#MAXLINES%) DO (
  35.          SET #COUNT=000000%%~C
  36.          SET #COUNT=!#COUNT:~-6!
  37.          SET /P #LINE!#COUNT!=
  38.      )
  39.  )
  40.  
  41.  
  42. :FinalReport -- Show all the variables with the lines that were read
  43.  SET #LINE
  44.  
  45.  
  46. :ExitBatch
  47.  TIMEOUT 60
  48.  ENDLOCAL
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement