Run cmd command work but run from batch file doesn't

梦想的初衷 提交于 2020-01-24 12:38:32

问题


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"
)

回答1:


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"
)



回答2:


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.



来源:https://stackoverflow.com/questions/21128185/run-cmd-command-work-but-run-from-batch-file-doesnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!