-1

EDIT: My program takes filetypes as string inputs, then loops through a directory converting matching filetypes. The converted files get prepending with "output". Currently, the loop runs infinitely if the target filetype and conversion filetype are the same, as it adds each converted file back into the loop.

How can I get my loop to NOT match on filenames that contain output?

set /p from="Enter filetype to convert from: "
set /p to="Enter filetype to convert to: "

for %%A in (*.%from%) DO (if findstr /V /I /S /L /C:"output" "%%A" (ffmpeg -i "%%A" -max_muxing_queue_size 9999 "output-%%A.%to%"))
    
pause

This just errors, but without the negative match it gets stuck in an infinite conversion loop if the filetype variables are the same. Because the loop prepends a "output" to the file, thats what I want to negatively match with.

EDIT: Sample input: mp4 mp4 Output will just put converted files in the same directory.

ancsjs
  • 37
  • 6
  • You cannot nest results of one command to be used at argument for another command. Open up a command prompt and type: `IF /?` to read the syntax usage for the `IF` command. – Squashman Feb 03 '21 at 14:19
  • 1
    Also, please show examples of what your input is and what your expected output is. I am not understanding from your description or code what you are trying to do. Please [edit] your question. – Squashman Feb 03 '21 at 14:22
  • You should also read `findstr/ ?`. Let's say you'e input `avi`, then `mp4` at the two prompts, your code loops through every avi file in the current directory, lets call them `file1.avi`, and `file2.avi`, it next appears to be looking for every line of content in every `file1.avi` in the current directory and all its subdirectories which do not contain the case insensitive string `output`, it would then repeat that process for every `file2.avi` in the current directory and its subdirectories. I really do not think that your code comes close to doing what you intended it to do. – Compo Feb 03 '21 at 14:45
  • @Compo yeah my example doesn't work at all, I'm aware. If you cut the ```findstr``` expression so it just does ```DO ffmpeg```, it works but get stuck in a loop as I said. I'm asking for help on how write a loop that does a negative match on files with "output" in it. – ancsjs Feb 03 '21 at 14:59
  • Your edit has clarified nothing. Your code as posted fails because the `if` statement has incorrect syntax. You were asked to describe what you are trying to do. What do you expect your process to achieve, given example filenames? – Magoo Feb 03 '21 at 15:03
  • No you're asking for code, _(which incidentally is off topic)_, and not even asking for it in a way that everyone understands what you're trying to do. I'm going to assume that you want to loop through all `%from%` extensions in the current directory, except for those which have the string `output` in their filenames, so that you don't process pre-processed ones. Is that correct? and if so, as it wasn't difficult for me to put that in an understandable form on an international platform, could you please [edit your question](https://stackoverflow.com/posts/66029282/edit) to make that clear? – Compo Feb 03 '21 at 15:04
  • Does this answer your question? [How to conditionally take action if FINDSTR fails to find a string](https://stackoverflow.com/questions/8530976/how-to-conditionally-take-action-if-findstr-fails-to-find-a-string) – T3RR0R Feb 03 '21 at 15:45
  • @Compo Thats exactly it. Thank you, I see now where I was phrasing it poorly and have included your edit. – ancsjs Feb 04 '21 at 11:33

1 Answers1

0

It appears to me as if you're looking to filter out your files within the current directory, which have the .%from% extension, but whose filename does not begin with the string output-.

To do that, you should use a for /f loop instead. Its usage information is available by opening a Command Prompt window, typing for /?, and pressing your ENTER key.

Here's an example, (using your unverified inputs):

For /F "EOL=? Delims=" %%G In ('Dir "*.%from%" /B /A:-D 2^> NUL ^| findstr.exe /B /L /V "output-"') Do ffmpeg.exe -i "%%G" -max_muxing_queue_size 9999 "output-%%~nG.%to%"

If you don't already have files in the current directory prefixed with output-, prior to your script being run, you could simply remove the findstr.exe portion. The reason is that in a standard for loop, each of your files are passed to the do portion immediately, which means that your output is being created, whilst your *.%from% files are still being parsed. When you use the same extension in both %from% and %to%, those files are added, to the yet to be parsed list. However, when you use a for /f loop, all results are parsed before anything is passed to the do portion, menaing nothing will be re-added to the parse list.

For /F "EOL=? Delims=" %%G In ('Dir "*.%from%" /B /A:-D 2^> NUL') Do ffmpeg.exe -i "%%G" -max_muxing_queue_size 9999 "output-%%~nG.%to%"
Compo
  • 36,585
  • 5
  • 27
  • 39