简体   繁体   中英

Issue with a variable in my batch script

I want my script to:

  • Accept a variable
  • Create a path using that variable as input
  • Display the path
  • Display the contents of the directory

What is wrong with the following code? The ECHO statement just prints Your directory is set to ; the DIR statement works as expected.

@ECHO OFF
SET custompath = "C:\Users\%1"
ECHO  Your directory is set to %custompath%
DIR %custompath%

It's the space around the = .

@ECHO OFF
SET custompath="C:\Users\%1"
ECHO  Your directory is set to %custompath%
DIR %custompath%

Check this post .

Personally, I would do it this way:

@ECHO OFF
SET /P "custompath=Enter a custom windows path: "
ECHO Showing contents of directory ^"%custompath%^"
DIR /b "%custompath%"
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM