0

I've seen questions that I think are similar (but which I cannot make work for me), but I am very new to batch files so I don't understand what needs to be different to make it work. I have a folder /source/ with some files, say:

  • red.txt
  • orange.txt
  • yellow.txt
  • green.txt
  • blue.txt
  • purple.txt

I need to make a batch file that will run 7zip on all of them automatically to create zipped version of them. I found some resources which I thought would help but after a few hours of frustration, I think I'm forced to confront the fact that I simply don't know enough about Batch files to understand why it isn't working?

My effort was:

:: Changes directory to this file's directory
cd %~dp0

:: Location of source data
set dirSource="C:\Source\"

:: Target location of zipped data
set dirTarget="C:\Target\"

cd %dirSource%

for %f in (%dirSource%*.txt) do
(
    7z.exe a "%dirTarget%\%%~NF.zip" "%dirSource%\%%~NXF"
    :: based on related searches on the internet
    :: but I can't find an explanation of _why_ this is supposed to work, which makes it hard to know why it isn't
    :: ie, what does "a" do?  What is ~NF vs ~NXF?  etc
)

Lastly - I have been utterly unable to find a good comprehensive guide to batch files because of all the weird character modifiers. Since I don't expect to pick it all up in this question, if anyone has a good guide where I can look up what some of these seemingly random character combinations mean I would be forever grateful.

kumquatwhat
  • 185
  • 1
  • 2
  • 12
  • 2
    Well I can certainly tell that you did not read the help file for the `FOR` command nor have you looked at any answers here on SO that use the `FOR` command with parenthesized code block. Lines 8-10 of the help file should explain two of your problems: **To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I.** – Squashman Mar 30 '21 at 01:37
  • 1
    Best practice is to **NOT** assign quotes to variables but use them to protect the assignment of a variable. `set "dirSource=C:\Source\"`. Then use quotes around your entire file paths to protect spaces and special characters. `"%dirSource%*.txt"` – Squashman Mar 30 '21 at 01:40
  • 1
    Do **NOT** use double colons as a comment inside parenthesized code blocks unless you know the ramifications of doing so. Just stick to using `REM`. – Squashman Mar 30 '21 at 01:42
  • 1
    And read this [answer](https://stackoverflow.com/a/2591782/1417694) on how to properly use parentheses to create a code block of commands from the `FOR` output. – Squashman Mar 30 '21 at 01:51
  • 1
    And here is your [A-Z Index of the Windows command line](https://ss64.com/nt/). Syntax, examples and all the fixings. You can read any commands help by typing the command name followed by a forward slash and question mark at a cmd prompt: `for /?` – Squashman Mar 30 '21 at 01:55
  • re %%, that was a typo, I have it as %% on my batch file, just made a mistake here, sorry. ~~re comments what is the difference between :: and rem? Everything I saw indicated they were identical but I am happy to learn the difference.~~ I see it's also in the link you gave me which seems incredibly thorough. Thank you! – kumquatwhat Mar 30 '21 at 02:53
  • 2
    [See here](https://stackoverflow.com/a/12408045/12343998) for information about commenting styles and their pros / cons – T3RR0R Mar 30 '21 at 03:26

0 Answers0