none
Batch Variable Not Printing RRS feed

  • Question

  • Hi Guys,

    I am facing a problem while printing a variable in Batch script.

    In the below code, print method does not print the variable value.

    I have saved the file as .cmd

    @echo off
    cls
    setlocal EnableDelayedExpansion

    set "name=abcdefghijklmnopqrstuvwxyz1234567890, .;'[]{}=+-_)(&^%$#@!`~"
    call :print !name!

    pause

    :print
    echo.%~1
    exit /b


    Monday, April 27, 2015 10:20 AM

Answers

  • Your script contains several errors, one of them fundamental (missing goto :eof). Compare your version with the one below to see what I mean.

    @echo off
    setlocal EnableDelayedExpansion

    set name="abcdefghijklmnopqrstuvwxyz1234567890, .;'[]{}=+-_)(&^%$#@!`~"
    call :print !name!
    goto :eof

    :print
    echo %1
    pause

    • Proposed as answer by Frederik Long Monday, April 27, 2015 3:52 PM
    • Marked as answer by HimanshuGoyal Monday, April 27, 2015 3:58 PM
    Monday, April 27, 2015 11:43 AM

All replies

  • Your script contains several errors, one of them fundamental (missing goto :eof). Compare your version with the one below to see what I mean.

    @echo off
    setlocal EnableDelayedExpansion

    set name="abcdefghijklmnopqrstuvwxyz1234567890, .;'[]{}=+-_)(&^%$#@!`~"
    call :print !name!
    goto :eof

    :print
    echo %1
    pause

    • Proposed as answer by Frederik Long Monday, April 27, 2015 3:52 PM
    • Marked as answer by HimanshuGoyal Monday, April 27, 2015 3:58 PM
    Monday, April 27, 2015 11:43 AM
  • Thanks for your valuable suggestions.

    Can you find something abnormal in this code:

    @echo off
    setlocal EnableDelayedExpansion

    set formatted_source="C:\Test\*.txt"

    :: Above directory contains a file named as abcdefghijklmnopqrstuvwxyz1234567890, .;'[]{}=+-_)(&^%$#@!`~.txt

    set formatted_source_file="C:\Test\abcdefghijklmnopqrstuvwxyz1234567890, .;'[]{}=+-_)(&^%$#@!`~.txt"
    set formatted_dest="C:\backup"

    call :copyVerifyMove !formatted_source_file! !formatted_dest!

    if "!formatted_source!" NEQ "" (
    for %%l in ("!formatted_source!") do (
    set formatted_source_file1="%%l"
    call :copyVerifyMove !formatted_source_file1! !formatted_dest!
    )
    )
    goto :eof


    :copyVerifyMove
    echo.%1
    echo.%2
    pause

    First call prints correctly but 2nd does not.

    Would you please help me to fix it.



    Monday, April 27, 2015 2:01 PM
  • Can you find something abnormal in this code:

    Yes, I can:
    Above directory contains a file named as abcdefghijklmnopqrstuvwxyz1234567890, .;'[]{}=+-_)(&^%$#@!`~.txt
    -> Characters such as )(&^%! in file names are called "poison characters" because batch files are not designed to handle them. Use PowerShell or VBScript instead.

    Seeing that I answered your first question, I suggest you mark it as "answered", then start a new thread if you have further questions.

    Monday, April 27, 2015 2:48 PM
  • I know these are poison characters but these characters are allowed in any file name (in windows).

    I am aware that batch files have certain limitations.

    I am just trying to figure out why it is not working in 2nd call (and why working in 1st call)



    Monday, April 27, 2015 3:10 PM
  • I know these are poison characters but these characters are allowed in any file name (in windows).

    I am aware that batch files have certain limitations.

    I am just trying to figure out why it is not working in 2nd call (and why working in 1st call)


    Sorry, I cannot see your new post on the new subject.
    Monday, April 27, 2015 3:53 PM
  • Done.

    Please see the new thread:

    https://social.technet.microsoft.com/Forums/en-US/0a43ee8f-6b4c-4f56-98b7-425bbce290d5/batch-variable-conflict?forum=ITCG

    Monday, April 27, 2015 4:01 PM