2

I try to create script with for loop to move file to sub folder. At the beginning, I work with CMD then I copy command to .bat file and run it. The result is nothing happens. Why the same command on CMD works but run from file not ?

Here is my command.

@echo off
setlocal enableDelayedExpansion 
SET FOL=J:\test
SET ENDNUM=2

for /l %x in (1, 1, %ENDNUM%) do (
md "%FOL%/0%x/subfolder"
move /Y "%FOL%\0%x\*" "%FOL%\0%x\subfolder"
)
Phlume
  • 3,075
  • 2
  • 19
  • 38

2 Answers2

8

You have to double-up the percentage signs on for commands in a batch file.

for /l %%x in (1, 1, %ENDNUM%) do (
  md "%FOL%/0%%x/subfolder"
  move /Y "%FOL%\0%%x\*" "%FOL%\0%%x\subfolder"
)
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
0

Check encoding of your .bat file. When I used UTF-8, command prompt displayed error:

C:\Users\***\Desktop>´╗┐cmd
'´╗┐cmd' is not recognized as an internal or external command,
operable program or batch file.

When I used ANSI encoding, the .bat file worked as expected.