0

I want to fetch single file from one specific folder, the folder contains only single file , so don't want to use for loop for that . how can I write batch script for that . Below is my sample code but its uses for loop, and i think if their is only single file then why we use for loop.

cd ${p:DownloadArtifacts}\ReleaseNote
for %%# in (*.xlsx) do echo myvalue=%%~nx#

and one more script is

dir /s /p /b "*.xlsx 

using this script i cant store the value into variable , please help..

  • Possible duplicate of [How to store the result of a command expression in a variable using bat scripts?](https://stackoverflow.com/questions/1427218/how-to-store-the-result-of-a-command-expression-in-a-variable-using-bat-scripts) and [Set output of a command as a variable (with pipes)](https://stackoverflow.com/questions/14952295/set-output-of-a-command-as-a-variable-with-pipes) – FatalBulletHit Feb 16 '18 at 11:56
  • Welcome to Stack Overflow. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and/or risk a look into Jon Skeet's [Stack Overflow question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). You are always welcome to ask, but please keep the above in mind. :) – FatalBulletHit Feb 16 '18 at 11:57
  • The best way to get the output of a command to a variable is a `for /f` loop. But even better is it to use the `for` in the first place. *Why* don't you want to use `for`? – Stephan Feb 16 '18 at 12:07
  • a technically correct answer would be_ `dir /s /p /b "*.xlsx >out.txt` and ` – Stephan Feb 16 '18 at 12:09
  • 1
    You say a "file from one specific folder". Why then use `dir` switch `/s` and even `/p`? – Stephan Feb 16 '18 at 12:11

2 Answers2

1

By saying "from one specific folder, the folder contains only single file" why even do all this fancy stuff?

Assuming you are using copy to copy the file:

cd to dir...
copy /Y *.xlsx "C:\some directory"    
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

I can see no reason whatsoever for wanting to do this without a loop!

The only way I can think of without the use of a temporary holding file would be using ForFiles. However ForFiles is technically a looping command in its own right.

To do it you'd need to run the original script from itself, which I suppose could be termed looping too!

Here it is though, just for the hell of it:

@Echo Off
If Defined me GoTo :Next
Set "me=%~f0"
ForFiles /M *.xlxs /C "Cmd /C 0x220x22%me%0x22 @file0x22"
Exit /B
:Next
Set "var=%~1"
Rem show variable
Set var
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39