7

I wrote a command line script that makes a list of files I want to do things to such as compress, delete or email. The list would be in the following format:

7z-Compression-List.txt:
========================

file1
file2
file3
etc...

For compression, I want to combine all the lines in the list into a single line. like this:

file1, file2, file3, etc

so that I can feed all the files into the 7z command line app at the same time.

Would anyone know how I can do this? Maybe a for loop of some sort?

0

2 Answers 2

7

I ended up finding the answer in another question, here in Stack Overflow

@ECHO OFF
setlocal
(SET file-list=)
FOR /f "delims=" %%x IN (7z-Compression-List.txt) DO (
CALL SET file-list=%%file-list%%, %%x
)
SET file-list=%file-list:~1%
ECHO file-list=%file-list%

It worked beautifully. This is a modified copy of Magoo's answer, so feel free to upvote original author if you like this answer.

2
  • Your script leaves an space at the beginning of the line. Replace ~1 with ~2 to fix it. Apr 28, 2017 at 20:47
  • Thanks, Amir. I'll update the answer as soon as I get a chance to test the change. Apr 29, 2017 at 19:09
0

Based on this community wiki:

@echo off
for /f "tokens=* delims=" %%a in ('type 7z-Compression-List.txt') do (
<nul set /p Test=%%a, 
)

However it leaves an extra ", " at the end of line.

1
  • Hi Amir, thanks for the answer, the comma at the end doesn't work for me but it's always good to know alternative ways of doing things. Apr 28, 2017 at 19:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .