开发者

Creating sub-directories using first 2 characters of a file name in a batch file

I'm trying to create a batch file which will loop around a list of JPGs/PNGs in a folder, and create sub-directories using the first 2 characters of these image names. After creating the sub-directories, it will then move the image into the correct sub folder.

For example, ABC.jpg and DEF.png will开发者_如何学运维 create AB and DE, and move ABC.jpg into AB and DEF.png into DE.

The problem I'm having is extracting the first 2 characters and creating the sub-directories. Here is the relevant code I have so far:

for %%A in (*.jpg,*.png) do (

  set _xx=%%A

  md %_xx:~0,2%
)

[Error / duplication handling, and the file move itself has been removed for clarity]

Echoing out the variable _xx shows no value assigned to it, however echoing out %%A gives the correct file name.

Running this script actually creates 2 sub-directories called '2' and '~0'

Any suggestions?


You need to use

setlocal enabledelayedexpansion

at the top of the file, and then instead of

md %_xx:~0,2%

use

md !_xx:~0,2!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜