0

I wanted to take list of files to delete from user as a argument. One line per argument.

How can store the list of files separated by new line in a variable.

I am using below command.

Set DeletionFiles=${p:DeleteFiles}"

for %%i in (%DeletionFiles%) do (
  echo %%i
)

Then i wanted to iterated them on a loop.

${p:DeleteFiles} will get replaced by it's value from external app, which will contain list of files separated by new line.I can not change it.

Pavan Tiwari
  • 3,077
  • 3
  • 31
  • 71
  • `${p:DeleteFiles} will get replaced ... which will contain list of files separated by new line.` -- Then it will not work in a bach file! As this wont set a multi line variable, it simply sets the first line and the remaining lines result in errors. – jeb Mar 02 '16 at 13:27

3 Answers3

0
@ECHO OFF
SETLOCAL
SET "deletionfiles="

:dloop
SET "deleteme="
SET /p "deleteme=Which file to delete ? "
IF DEFINED deleteme SET "deleteme=%deleteme:"=%"
IF DEFINED deleteme SET "deletionfiles=%deletionfiles%,"%deleteme%""&goto dloop
ECHO delete %deletionfiles:~1%

GOTO :EOF

There is no need to use a newline. Your for command (or a del command) will operate perfectly happily on a comma-(or space-)separated list.

Note that there are certain characters that batch uses for special purposes and batch string-processing may not process them in the expected manner. These characters include % ^ and &.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

${p:DeleteFiles} will get replaced by it's value from external app, which will contain list of files separated by new line.I can not change it.

After the replacement the batch file looks like:

Set DeletionFiles=file1.jpg
file2.jpg
file3.jpg
"

This isn't a valid batch file anymore.
Furthermore it's a bad idea to modify the batch file itself, as this works only once.

You could place the ${p:DeleteFiles} into another file, like input.txt.

Your batch would look like

echo ${p:DeleteFiles} > input.txt

<external program for replacing the DeleteFiles> input.txt

for /F "tokens=*" %%A in (input.txt) do (
  echo File: %%A
)
jeb
  • 78,592
  • 17
  • 171
  • 225
0

If I understand you correctly, your external program will generate a list of files. You then want to store this multi-line list to a variable. What do you want to do with the variable once you have it? I assume you want to delete the files, but your question isn't clear on that point, so I'll try to over-answer to cover it.

for /f "delims=" %%a in ('{command that generates your list}') do (
    echo Doing stuff to %%a...
    echo %%a>>listOfFilesToDelete.txt
    set var=%%a
    if "%var:~0,7%"="DoNotDelete" copy "%%a" \someArchiveFolder\
    del "%%a"
)

This will read each line in your generated list as variable %%a. It will then do whatever command(s) you specify. This way, you can run a command on each of the files in the list. In the above code it's

  1. Printing each line to the console embedded in some text
  2. Outputting it to a file
  3. Checking the first 7 characters of the line against a specified string and then copying it to a folder if it matches
  4. And then deleting it

If you still need to reference each line from your generated list, you can even setup an array-like structure. (See Create list or arrays in Windows Batch)

setlocal EnableDelayedExpansion
:: Capture lines in an 'array'
set /a i=0
for /f "delims=" %%a in ('dir /b') do (
    set /a i+=1
    set var!i!=%%a
)

:: Loop through the 'array'
for /L %%a in (1,1,%i%) do (
   echo Do more stuff with !var%%a!
)

Just like above, this will read each line in your generated list as variable %%a. It will then set a variable var!i! equal to the value of the current line. You can then reference each line as var1, var2, and so on, or, as the second section shows, you can loop through them all using for /L. You'll need to get a grasp on working with delayed expansion variables, though.

Community
  • 1
  • 1
Wes Larson
  • 1,042
  • 8
  • 17