0

So, I was playing around splitting strings and I came across this situation:

for /f "tokens=1" %%c in ("A B C") do (
  set s=%%c
  echo s: %s%
)
pause

I expected it to print:

s: A

But it actually prints:

s: 

Why?

What I'm trying to do is: I have some folders whose names have a default placement (id name, e: 123 ABC), and inside each folder I have a file I need to copy but the file name is different in each one so I need to get the name of the folder without the id, what I thought was supposed to do that was this:

cd C:\MyFolders
pause
for /d %%a in (*) do (
  set name=%%a
  for %%b in ("%%a\*.txt") do (
    for /f "tokens=1" %%c in ("%%a") do (
      set id=%%c
      set name_without_id=%name%:%id% =
      echo new file name: %name_without_id%
    )
  )
)
pause

I thought it could be a bug in my code, so I did the first one, really simple, but yet I didn't get what I was expecting.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 5
    Please read on [delayed expansion](https://ss64.com/nt/delayedexpansion.html) –  Mar 15 '17 at 18:36
  • 2
    Possible duplicate of [Batch script for loop won't set variable](http://stackoverflow.com/questions/12518242/batch-script-for-loop-wont-set-variable) – aschipfl Mar 15 '17 at 18:40
  • When I use `SETLOCAL EnableDelayedExpansion` the program says that he can't go to `C:\MyFolders` because it doesn't exist, although it does. – Geovane Silveira Mar 15 '17 at 18:41
  • Geovane without context such a comment isn't helpful. Edit your question to include the changes you made and the error message. –  Mar 15 '17 at 21:39
  • Try with `cd /D "C:\MyFolders"`... – aschipfl Mar 15 '17 at 21:43

0 Answers0