0

In short I was provided this code by my professor, im getting an error where it says "%%X" the output says %%X unexpected error.

SETLOCAL ENABLEDELAYEDEXPANSION

SET CURRENT_DIR=%CD%
SET DIR_NAME=%CURRENT_DIR%\CIS153
SET COUNTER=1

MKDIR %DIR_NAME%

FOR /F "DELIMS=" %%X IN (MyData.txt) DO (
    ECHO %%X > myFile!COUNTER!.txt
    SET /A COUNTER=!COUNTER!+1
)

What I expect the the code to do is make a directory called CIS153 and based on the content from MyData.txt it'll create a new directory and write it to an entire new file.

lit
  • 14,456
  • 10
  • 65
  • 119
  • 2
    from what you've shown us, there appears to be no error and it should do as you describe. I suspect that you are attempting to execute the commands individually from the prompt. The script should be written to a file named *whateveryouwanttocallit*.bat and then executed as *whateveryouwanttocallit*. Executing from the prompt requires use of `%X` instead of `%%X`. Note that the case of `X` here must be consistent - it's one of the few times batch is case-sensitive. I ran the code but unsurprisingly it reported `The system cannot find the file MyData.txt.` since that file doesn't exist for me. – Magoo Apr 29 '19 at 03:55
  • Could it be that I am entering the code via regular CMD line by line and not as an Actual .BAT file? – Jamil Ellis Apr 29 '19 at 04:26
  • Yes in batch one use `%%A` and interactively `%A`. Also state in lost after every line is typed unlike batch. – Noodles Apr 29 '19 at 04:26
  • Run in command prompt window `set /?` and read the output help. It explains that in an __arithmetic__ expression, which is the string after `set /A`, environment variables can be specified with just their names, i.e. `SET /A COUNTER=COUNTER+1` and the available __arithmetic__ operators like `+=`. So the increment of variable `COUNTER` by one is written best with `SET /A COUNTER+=1`. And run also `for /?` in cmd window and read also its help. Finally see [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). – Mofi Apr 29 '19 at 05:09
  • @JamilEllis run `for /?` and read the documentation. It mentioned the syntax as `FOR %variable IN (set) DO command [command-parameters]` and later noted `To use the FOR command in a batch program, specify %%variable instead of %variable` – phuclv Apr 29 '19 at 15:53

1 Answers1

0

@Magoo is right. This script must be in a file. Use Notepad.exe or another editor to create a file.

There is also some quoting that should be added. Use quotes around the SET command to ensure that invisible characters like SPACE and TAB are not at the end of the value.

Also, paths need to be quoted since they may contain SPACE or other special characters.

Checking to see if a directory exists before making it will avoid an error message if it already exists.

Finally, I note that while it does create a directory, there is nothing that sets it as the current directory. The result is that the "myfile*.txt" files will be created in the current directory and not in the newly created directory.

=== CIS153.bat

SETLOCAL ENABLEDELAYEDEXPANSION

SET "CURRENT_DIR=%CD%"
SET "DIR_NAME=%CURRENT_DIR%\CIS153"
SET "COUNTER=1"

IF NOT EXIST "%DIR_NAME%" (MKDIR "%DIR_NAME%")
rem CD "%DIR_NAME%" ???

FOR /F "DELIMS=" %%X IN (MyData.txt) DO (
    ECHO %%X > myFile!COUNTER!.txt
    SET /A COUNTER=!COUNTER!+1
)
lit
  • 14,456
  • 10
  • 65
  • 119