Home > Enterprise >  Save all the content inside a text into a varible in batch
Save all the content inside a text into a varible in batch

Time:11-14

(Sorry for the bad english.) Example,I Have A Text File called text.txt,contain like this:

D:\jobs
C:\something
C:\localsata
C:\system
C:\contanier
C:\coolkidzv2

My question is if there is a batch script that can save all of these location(path) into one single variable exactly like this:

set CONTRAST=D:\jobs;C:\something;C:\localsata;C:\system;C:\contanier;C:\coolkidzv2

Any ideas how to do it using only batch script(Powershell script is ok too but i would love to see a batch script.)

CodePudding user response:

Please understand that this site is not a code writing service, we do not perform searches for existing batch files to match your specific requirements, and we are not here to provide a free code writing service.

What I will do, on this occasion is provide two single line batch files which can achieve what you've posted. One gets the help of PowerShell, as you've implied it is installed, and the other uses only built-in cmd.exe commands. However, I will not explain any of the code, or make adjustments or additions to it upon request.

@Set "CONTRAST=" & For /F "Delims=" %%G In ('%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "(Get-Content -Path '.\text.txt') -Join ';'" 2^>NUL') Do @Set "CONTRAST=%%G"
@Set "CONTRAST=" & For /F UseBackQ^ Delims^=^ EOL^= %%G In (".\text.txt") Do @If Defined CONTRAST (SetLocal EnableDelayedExpansion & For %%H In ("!CONTRAST!") Do @EndLocal & Set "CONTRAST=%%~H;%%G") Else Set "CONTRAST=%%G"

CodePudding user response:

I suggest to open first a command prompt window, run set /? and read the output help. There is explained when and how delayed expansion must be used on an IF and a FOR example. The FOR example is nearly what you need.

So there could be used:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "CONTRAST="

rem Concatenate all lines in file test.txt in directory of the batch file
rem with using a semicolon as separator between the directory paths.
for /F "usebackq eol=| delims=" %%I in ("%~dp0test.txt") do set "CONTRAST=!CONTRAST!;%%~I"

rem Remove the semicolon at beginning of the list of directory paths.
if defined CONTRAST set "CONTRAST=!CONTRAST:~1!"

rem Do something with the environment variable CONTRAST.
rem There is just output the environment variable with its name as an example.
set CONTRAST

endlocal

This code works only if no directory name in the text file contains an exclamation mark. Otherwise it would be necessary to use the following code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "CONTRAST="

rem Concatenate all lines in file test.txt in directory of the batch file
rem with using a semicolon as separator between the directory paths.
for /F "usebackq eol=| delims=" %%I in ("%~dp0test.txt") do call :MergePaths "%%~I"
goto ProcessContrast

:MergePaths
set "CONTRAST=%CONTRAST%;%~1"
goto :EOF

:ProcessContrast
rem Remove the semicolon at beginning of the list of directory paths.
if defined CONTRAST set "CONTRAST=%CONTRAST:~1%"

rem Do something with the environment variable CONTRAST.
rem There is just output the environment variable with its name as an example.
set CONTRAST

endlocal

Please note following limitations:

  1. The first code is faster, but does not work for a directory path with one or more ! in any path.
  2. A problem on further processing of CONTRAST is a directory path containing the separator ; which is very unusual, but is in general possible.
  3. There is the command line argument length limit of 8192 characters which means that " left to environment variable name CONTRAST plus the environment variable name CONTRAST plus the equal sign plus the current length of the semicolon separated directory names plus " at end of argument string to process by SET plus the string terminating null byte cannot be more than 8192 characters. In other words the list of ; separated directory paths cannot be longer than 8180 characters.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

See also: Where does GOTO :EOF return to?

  • Related