1

Windows 10, in my own Videos folder under UserProfile.

I'm trying to string together a couple of things to make a complete txt file containing various information, in one single command line, right.

The goal is to make a single txt with a header, some generated information and a footer. The txt will be named the same thing as the folder name with .txt at the end, for simplicity's sake.

I'm aiming to run the string of commands once per folder, and since there are quite a few calls to the folder's name that's being targeted, I figured to use a variable to shorten it.

type header.txt > VeryLongFolderNameFooBar\VeryLongFolderNameFooBar.txt & c:\mediainfo\mediainfo.exe --output=file://template.txt ./VeryLongFolderNameFooBar/*.mkv >> VeryLongFolderNameFooBar\VeryLongFolderNameFooBar.txt & type footer.txt >> VeryLongFolderNameFooBar\VeryLongFolderNameFooBar.txt

This worked absolutely perfectly, but got quite tedious to change the folder name for all occurrences, so why not use a variable right?

set a=VeryLongFolderNameFooBar && type header.txt>%a%\%a%.txt & c:\mediainfo\mediainfo.exe --output=file://template.txt ./%a%/*.mkv>>%a%\%a%.nfo & type footer.txt>>%a%\%a%.txt

This gives three access denied messages. Initially I was using spaces before and after the variable calls and removed them after reading a post that spaces can mess things up, but still no avail and I am really stuck on this. When I do echo %a% it returns the folder name with no issues at all.

What am I missing?

1
  • 1
    For your actual goal (not your Q) instead of repeating the filename you could do (type header & mediainfo blah & type footer) >outputfile Commented Mar 21, 2019 at 9:27

1 Answer 1

2

CMD scans the entire line for variables before it actually processes any of it.  Therefore, it replaces %a% with null (or whatever it was previously set to) before it processes the set a=... command.  You'll need to do that on a separate line:

set a=VeryLongFolderNameFooBar
type header.txt>%a%\%a%.txt & c:\mediainfo\mediainfo.exe --output=file://template.txt ./%a%/*.mkv>>%a%\%a%.nfo & type footer.txt>>%a%\%a%.txt

But, in addition (as you discovered), the set command (stupidly, IMHO) includes everything up to a command separator.  So

set a=SoLong & echo /%a%/

will set a to SoLong  (with a trailing space).  One way to handle that is to leave out the space before the &:

set a=SoLong& echo ...

but that's widely considered to be less readable.  A slightly nicer solution is to put the set command in parentheses:

(set a=SoLong) & echo ...

so the value gets terminated by the ).

4
  • echo %a% VeryLongFolderNameFooBar type header.txt>%a%\%a%.txt& c:\_yaiba\mediainfo\mediainfo.exe --output=file://template.txt ./%a%/*.mkv>>%a%\%a%.txt & type footer.txt>>%a%\%a%.txt Access denied. Access denied. Access denied. Edit: I was unable to do the linebreaks but I hope it's legible anyway.
    – Chokehold
    Commented Mar 21, 2019 at 2:18
  • Turns out it actually works fine if you only set the variable on its own sole line. Since I echo'd %a% and it returned the correct result I figured it would work anyway. Also, after testing again, it seems that the space was the issue. My initial line worked, had I only removed the first space after the variable name. set a=VeryLongFolderNameFooBar&type header.txt etc worked perfectly.
    – Chokehold
    Commented Mar 21, 2019 at 2:44
  • Thank you for reminding me about the trailing space issue; I keep forgetting about that. Commented Mar 21, 2019 at 3:03
  • 1
    Or set "var=value" & blahblah -- but not set var="value" which includes the quotemarks and trailing space. Also, if DELAYEDEXPANSION is enabled you can set "a=value" & use !a! on same line -- which is easy in a batchfile (script) but a bit of a nuisance interactively. Commented Mar 21, 2019 at 9:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .