简体   繁体   中英

batch read file name as a variable and then use the variable in another batch process

I have a simple .txt file (myfile.txt) containing the names of the 300+ files in the same directory. Each line contains the name of one file and each file name has spaces - something like:

file number one.txt
file number two.txt
file number three.txt
....
file number three hundred and twenty five.txt

I want to batch process each file without hard coding every file name in my main batch script (process.bat).

My process.bat simply opens and processes a file (currently hard coded) with a command-line app and then saves the processed file to a different directory but maintains the original file name.

@echo off

setlocal enableextensions disabledelayedexpansionstart 
start prooftool -c "file number one.txt" "/processed/file number one.txt"

I somehow need to dynamically replace "file number one.txt" with a variable (ie the file name) from myfile.txt and then write the processed data to a new file named exactly the same as the original file but in a folder called "processed".

To read the file names from mytext.txt I tried:

for /F "tokens=*" %%A in (myfile.txt) do [process.bat] %%A

but process.bat has nothing to process.

I have looked up how to read lines from a text file into variables and I think maybe the spaces in the file names are causing problems!?

I don't understand where the variables are stored (I presume in memory?) also, how do I call a variable to be processed by my process.bat script (file names are currently hard coded) and then how to call the next variable etc so that it loops through and processes all the files in the directory.

Thanks for your time and patience!

Try

for /F "tokens=*" %%A in (myfile.txt) do [process.bat] "%%A"

quoting the metavariable makes the entire string, including the spaces, a single token. Since you haven't shown us process.bat , we're guessing, sadly.

In your process.bat file, you'd need

start "some windowtitle" prooftool -c "%~1" "\processed\%~1"

start uses the first quoted string as a window-title; you can make this an empty string if you wish, but don't omit it.

"%~1" means quote (the-first-parameter-stripped-of-quotes) .

Path separators in winbatch are \\ . / is used to indicate switches. Sometimes, but not always, / is translated to \\ - best to use the correct character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM