rss
Search : in
By : Relevance Date Username
Statut : Not resolved

Creating a variable from a txt file

unik1, on Monday June 16, 2008 01:33:20 PM
Hello,


I want to make a batch file that creates variables by parsing a .txt file made from a "dir /b /n > dirlist.txt" command. Each line in the .txt file is a single filename.

I need each line/filename to be a new variable which I can use as part of the syntax for the program i'm trying to run:

program.exe targetfile %1 %2 %3

Using the above works if I just drag & drop the files onto the .bat, but they aren't in order as required, that is the reason the i've used the dir command to create the file.

I found that using:
FOR /F "USEBACKQ DELIMS==" %%I IN (a.txt) DO (SET var=!%%I)
It sets the variable but only holds the last line in the file.

I've also tried: FOR /F "tokens=1,2*" %%I IN (a.txt) DO SET var=%%I %%J %%K
Which works great (using dir /w) until "a.txt" has more than one line. If there's a way to adjust the file via a batch command to make all the text only on the first line separating the filenames with a space/tab, then it would work fine.

Am I even on the right track?
Configuration: Windows XP
Internet Explorer 6.0
Reply to unik1  Report this message to moderators Go to last message

1


  • This message seems useful, vote!
  • Report this message to moderators
 ezkamoo, on Monday June 16, 2008 04:04:47 PM
Hai


The /n in dir/b/n isn't doing anything useful.
But this will set s var to each filename in myfile, incrementing: v1, v2, ... vn
::==========================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
set /a n+=1
set v!n!=%%a
)
set v

::==============================

You can nevermind the file creation:

::==================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b') do (
set /a n+=1
set v!n!=%%a
)
set v
Reply to ezkamoo
Software found in the relevant downloads
Download Disk Defrag 1.4.16.308Disk Defrag - Fragmentation is caused by creating and deleting files and folders, installing new software, and downloading files from the...Category: Optimization
License: Freeware/gratuit
Download PDF Creator 0.9.5PDF Creator - PDFCreator is a free tool to create PDF files from any program that is able to print.Category: PDF
License: Open Source
Download SoftPerfect File Recovery 1.2SoftPerfect File Recovery - SoftPerfect File Recovery is a free and useful tool to restore accidentally deleted files from hard and floppy disks, USB...Category: Backup
License: Freeware/gratuit
Download PC Inspector File Recovery 4.0PC Inspector File Recovery - PC INSPECTORâ„¢ File Recovery is a data recovery program that supports the FAT 12/16/32 and NTFS file systems. Finds...Category: Backup
License: Freeware/gratuit
More freeware about « Creating a variable from a txt file »