-3

I have a folder in %temp%\test\ with some files, now i need to run each files from cmd like:

for /r %%f in (%temp%\test\*) do (
  start "'%~nxI'"
)

This code not working files are .exe, jpg and others...

abelenky
  • 63,815
  • 23
  • 109
  • 159
Marcus J.Kennedy
  • 680
  • 5
  • 22
  • 1
    Describe *"not working"*: Are you getting an error message? what is the result? – abelenky Nov 16 '18 at 20:09
  • 1
    You are using a `FOR /R` command which walks a directory tree but you are only using the file name with the start command. You will always get an error for any files in a sub folder. Also with the `FOR /R` command you put the starting path after the `/R`. – Squashman Nov 16 '18 at 20:15
  • not working = nothing, cmd console close – Marcus J.Kennedy Nov 16 '18 at 20:33
  • 3
    You never reference the replacement variable `%%f` in the `for` statement. – jwdonahue Nov 16 '18 at 20:33
  • @MarcusJ.Kennedy, take the [tour], read [ask], and [mcve]. [Edit] your post to include actual vs expected behavior. Tell us how you launch your script. I recommend that when you are debugging a script, you run it from a console command line, not from the Windows shell environment. – jwdonahue Nov 16 '18 at 20:36
  • Please read [debugging a batch file](https://stackoverflow.com/questions/42435648/batch-file-comparison-of-variable-with-constant-fails/42448601#42448601) – Squashman Nov 16 '18 at 20:46
  • 2
    Is `@For /R "%TEMP%\test" %%A In (*) Do @Start "" "%%~nxA" 2>Nul` what you're meant to do? – Compo Nov 16 '18 at 20:48
  • 4
    Correction to the above, _as you'll need the full path_, `@For /R "%TEMP%\test" %%A In (*) Do @Start "" "%%A" 2>Nul` ...or from the Command Prompt, `For /R "%TEMP%\test" %A In (*) Do @Start "" "%A" 2>Nul` – Compo Nov 16 '18 at 20:56

1 Answers1

1

As @jwdonahue pointed out, you didn't refer to your variable.
As written, you are referring to a variable I, which does not exist.

Also, if you are running from the command-line, you want to use a single %, not a double %%.

Try instead:

for /r %f in (%temp%\test\*) do (
  start "'%~nxf'"
)

The portion: %~nxf means, "Refer to variable %f from the for-loop, but process it with ~n and ~x to get just the raw file name and extension.

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • You can't run multi-line from the command line and the FOR variable reference wasn't the only problem with the code. Need to use the whole file path or it wont find the file. – Squashman Nov 16 '18 at 20:43
  • 1
    *"You can't run multi-line from the command line"* - Yes, you can. When the first line ends with a `(`, CMD will prompt you with : `"More?"` so you can continue typing subsequent lines. (I just opened a window and tested it to be sure) – abelenky Nov 16 '18 at 20:46
  • That is neat. Never knew you could do that. The code in your answer will still fail to open or execute any files. – Squashman Nov 16 '18 at 20:51
  • I agree there will continue to be problems, but at least the loop will be functional, and return meaningful error messages. OP can work on it from there. (The idea of trying to launch every EXE, JPG and other file in a temp directory sounds HORRIBLE) – abelenky Nov 16 '18 at 20:55
  • Well it won't return any error message at all. Your code is valid. It just won't open or execute any files. – Squashman Nov 16 '18 at 20:56
  • I haven't tested it, but I would expect errors like, `"The system cannot find the file stackoverflow.jpg"`, when the file exists in a subdirectory. – abelenky Nov 16 '18 at 21:01
  • 1
    With the `START` command the first quoted parameter becomes the `TITLE` of the Window. So all your code does is open up another command prompt with the `WINDOW TITLE` being the value of the `FOR` variable. – Squashman Nov 16 '18 at 21:06
  • 1
    also both single quotes *and* double quotes `"'%~nxf'"` will do nothing good. – Stephan Nov 16 '18 at 22:10