20

I want to enter a command in command prompt after reaching a specific location. How can I achieve this?

e.g.,

set PathName="X:\Web Content Mgmt\Completed Filtering\2013_Folder"
set comd="dir /b /s *.zip"
start "cmd" cd /d %PathName%

I am opening the command prompt and giving it a path using PathName. Now after reaching that specific path I want to insert the comd variable into the command prompt to get the desired result.

These are the specific commands I am trying to execute in the batch file:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\anoopn>x:
X:\>cd
X:\Web Content Mgmt\Completed Filtering\2013_Folder
X:\Web Content Mgmt\Completed Filtering\2013_Folder> dir /b /s *.zip > C:\Users\anoopn\Desktop\abc.csv
Anoop
  • 439
  • 2
  • 7
  • 12

6 Answers6

35

To get a user Input :

set /p pathName=Enter The Value:%=%
@echo %pathName%

enter image description here

p.s. this is also valid :

set /p pathName=Enter The Value:

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • I want to append the user input with the path which I give as an input to the command prompt and execute that command then fetch the appropriate result and store that result in a .CSV file. – Anoop Jul 08 '13 at 11:09
  • Tha's what ive done. look again. you can do anything with the %pathName% var – Royi Namir Jul 08 '13 at 11:11
  • I have to enter two path and 1st i have to reach to X drive and the exact location, then I have to run another script that means I have to perform two actions 1st action I acheived by using set PathName="X:\Web Content Mgmt\Completed Filtering\2013_Folder" start "cmd" cd /d %x% Still i need to perform another action and then work on it so kindly suggest me a perfect alternative – Anoop Jul 08 '13 at 11:40
  • @Anoop you're very unclear. please simplify your question . you have 2 scripts and you want both of them to know the %pathName% ? – Royi Namir Jul 08 '13 at 11:43
  • What I am exactly trying to acheive is to build a .bat file for the script – Anoop Jul 08 '13 at 12:25
  • My command prompt shows below given details: Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\anoopn>x: X:\>cd X:\Web Content Mgmt\Completed Filtering\2013_Folder X:\Web Content Mgmt\Completed Filtering\2013_Folder>dir /b /s *.zip > C:\Users\anoopn\Desktop\abc.csv I want a batch file that executes the above commands – Anoop Jul 08 '13 at 12:38
  • What is that %=%? a typo? When i do set /p=Enter the value:%=% I get C:\\>Enter The Value:%=%myvalue i.e. the %=% comes up as literal after the text of my prompt – barlop May 04 '14 at 09:12
3

You can use the following command. The SET will set the input from the user console to the variable comment and then you can use that variable using %comment%

SET /P comment=Comment: 
echo %comment%
pause
Community
  • 1
  • 1
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
2

Try this: edited

@echo off
set "comd=dir /b /s *.zip"
set "pathName="
set /p "pathName=Enter The Value: "
cd /d "%pathName%"
%comd%
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Still its not working for me. Hope to get a perfect answer from your end. – Anoop Jul 08 '13 at 11:36
  • I had a misplaced quote - try it now. – foxidrive Jul 08 '13 at 12:20
  • My reply answers your question, right? If you have another question then open a new question. That's how stack overflow works, I think. – foxidrive Jul 09 '13 at 09:01
  • that works only from second time when set X is saved in env variable – ses May 06 '14 at 19:31
  • Why does it work from the second time? What is `X`? If you have wrapped it in a loop then it needs delayed expansion and syntax changes, because that is how loops work. – foxidrive May 07 '14 at 09:57
2
set "PathName=X:\Web Content Mgmt\Completed Filtering\2013_Folder"
set "comd=dir /b /s *.zip"
cd /d "%PathName%"
%comd%
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

if I understand you right (not sure), the start parameter /D should help you:

start "cmd" /D %PathName% %comd%

/D sets the start-directory (see start /?)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

If you are creating other batch files from your outputs then put a line like this in your batch file

echo %pathname%\foo.exe >part2.txt

then you can have your defined part1.txt and part3.txt already done and have your batch

copy part1.txt + part2.txt +part3.txt thebatyouwanted.bat
kapex
  • 28,903
  • 6
  • 107
  • 121