dos bat批量创建软链接

本文介绍如何在Windows环境下,使用mklink创建软链接,将train2017/和val2017/目录下的图片合并到images/,避免了额外空间占用。通过详细步骤和代码展示了如何批量处理,并对比了ln-s与mklink的差异。

windows 下,要将 train2017/val2017/ 两个目录下的图片并入一个目录 images/,用 mklink 创建软链接[1]可以不用额外空间。

win10 也可以写 .sh 脚本用 ln -s,但效果似乎同 copy?因为用 ln -s 得出的 images/ 是有空间占用的,而用 mklink 是 0 bytes。

Code

@echo off
setlocal enabledelayedexpansion
cls

set SRC=G:\dataset\COCO\2017
set DEST=G:\dataset\COCO-stuff\images
if not exist %DEST% (
	mkdir %DEST%
)

set n_dir=0
for /d %%d in (train2017 val2017) do (
	set "src=%SRC%\%%d"
	echo --- !src! ---

	set n_image=0
	for %%f in (!src!\*.*) do (
		REM `n`: 文件名, `x`: 后缀名
		REM echo %%~nxf
		set "src_img=!src!\%%~nxf"
		REM echo !src_img!
		set "dest_img=%DEST%\%%~nxf"
		REM echo !dest_img!

		REM 屏蔽 mklink 的输出
		mklink !dest_img! !src_img! > nul 2>&1

		REM 每 1000 张提示一下
		set /A n_image += 1
		set /A r = !n_image! %% 1000
		if !r! EQU 0 (
			echo !n_image!
		)
	)
	set /A n_dir += 1
)
echo #dir: !n_dir!

References

  1. windows软链接
  2. bat文件循环、字符串
  3. BAT脚本之判断文件是否存在
  4. Create list or arrays in Windows Batch,数组
  5. Batch Script - Arrays,数组
  6. Arrays, linked lists and other data structures in cmd.exe (batch) script,数组
  7. for,截文件名
  8. bat批处理 if 命令示例详解,if、比较运算
  9. Exiting out of a FOR loop in a batch file?,break
  10. How to break inner loop in nested loop batch script,break
  11. “continue” equivalent command in nested loop in Windows Batch,continue
  12. batch script “continue” in loop?,continue
  13. Batch Script - Arithmetic operators,四则运算、取模
  14. Suppress command line output,屏蔽命令输出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值