| 1. |
Solve : Batch Problems and Executing Files with spaces in path? |
|
Answer» So I'm working on a batch file to help automate rather large repetitive install. The problem is that Start is unable to find the file for some reason and in general absolute pathing hasn't been working. I'm at somewhat of a loss as all the usual suspects don't appear to be the problem. I'm running Batch from a Windows 7 Command Prompt. Any ideas? FOR /F "eol=; tokens=1,2,3* delims=," %%i IN (list.txt) DO (You need to verify this block of code with a simple test. Make a dummy list of file names that are just 'stubs'. A stub is just a test program that does nothing. You want to see what happens just going through a list of files that do nothing. Never try to use a dubious block of code until it has been really verified. Unless you are a Genius. In which case you would not need help. The FOR /F thing is among the most difficult tools in DOS batch. I've tested and debugged it extensively. The file list.txt (bottom op) is my stub file. the FOR part works perfect the trouble happens when I add the start command in the loop. Here's a summary of what I have done up to present (~ 18 hours, "What can i say.. i am stubborn"). Tested with echo as only command in loop, displays variable %%i no problems for entire stub. Add Start Into the Mix, Opens new Command Prompt at specified directory. --Not what I was expecting Added "" to the front of start after a little reading and received error invalid switch /quiet /norestart. --Progress! Added " " needed to be around the entire STATEMENT after /wait and switches. 30 minutes of work leads me to my current state of output at "cannot find E:\Maintenance\Patches and" --smack Brickwall. OP was posted sometime ~hour seven or nine, lost track of time as the sun was rising and sleep deprivation was kicking and you can bet my coworkers were peaches today The weird thing is echo runs perfectly and I have double quotes surrounding not just the statement of the start, but ALSO around the path/[file].exe/msu which should deal with issues of long file names but apparently doesn't. In retrospect I should have just written a perl script up to do what I needed done, but I hadn't programmed in batch before and thought it would have been a nice challenge of my programming skills. Oh well, C'est la Vie. I see you have used the set /p ... Code: [Select]FOR /F "eol=; tokens=1,2,3* delims=," %%i IN (list.txt) DO ( This line looks a little odd, considering what you appear to want to do. The delims block... Code: [Select]"eol=; tokens=1,2,3* delims=," says Code: [Select]eol=; consider a semicolon to be the end of line marker, that is, override DEFAULT behaviour which is to IGNORE lines starting with ; Code: [Select]tokens=1,2,3* Split the line into 3 delimited tokens: %%i (explicit) %%j, %%k (implicit) plus the rest of the line %%l (implicit) (that's a lower case L) Code: [Select] delims=, Set the token delimiter to be a comma Since your example test.txt does not contain any lines that start with a semicolon, or have comma separated values, I don't quite see why you are doing this. The FOR /F command will pick up the entire line, but only because none of the things envisaged in the delims block are present! You appear to want to take the whole line so you may as well do this Code: [Select]FOR /F "delims=" %%i IN (list.txt) DO ( This delims block... Code: [Select]"delims=" ...means "set the only token delimiters to be the start and end of the line" However, I see that the paths and filenames that you show in test.txt are enclosed in double quotes. The FOR construct will pick these up and they will be included in the string to which %%i expands. When you add further quotes fore-and-aft in the Start /wait line I believe you are causing the command to be parsed in a way otherwise than you intend. One of the variable modifiers you can use with FOR metavariables of the %%x type is the tilde ~ which removes enclosing quotes (if present). So try this - I have not got any KB files to test it on but I have tried others (.txt) Code: [Select]@echo off FOR /F "delims=" %%i IN (list.txt) DO ( set /p t=Installing %%i . . . . . . . < NUL Start /wait "" "%%~i" /norestart /quiet echo Success!! ) Each command has its own sometimes extensive documentation which you can see by typing the command followed by a space and /? e.g. FOR /? Start /? [Update] The above seems to test out OK... the format for START in these circumstances is: quote the program drive\path\name and then pass the parameters: start /wait "Window Title" "D:\Path\with spaces\program name.exe" /PARAM1 /PARAM2 /PARAMn "Window Title" can be (and usually is) a null string "" Code: [Select]FOR /F "eol=; tokens=1,2,3* delims=," %%i IN (list.txt) DO ( Initially I was using semicolons in my stub file, but having the program ignore them was more trouble than not using them at all. Since the loop was operating correctly I neglected to change the working part until I had the rest of the pieces working. Code: [Select] delims=," I was planning on splitting the drive, path, file into three variables so I could use them individually elsewhere more readily and tighten up the code but I never got the core functionality running properly so that fell by the wayside. (i.e. echoing filename .... installed! ). As the script is now, it fails at each iteration of the start command. Using Code: [Select]START /wait "" "%%i /norestart /quiet" Where %%i = "E:\Maintenance\Patches and Updates\Win7\x64\[file].msu" in list.txt The output is an Alert Window (Title=E:\Maintenance\Patches) 'Windows cannot find "E:\Maintenance\Patches'. Make sure you typed the name correctly and then try again." Its not displaying the full correct path, cutting off where the spaces are in the long path name, /Patches and Updates/, which leads me to THINK its the long file name thats causing problems. I thought that correctly quoting would have dealt with that but apparently not. Update: Changed Working Directory to test whether it was the Patches and Updates -> Patches and still same output. Something else is going on here than long path. |
|