【发布时间】:2016-12-12 20:46:16
【问题描述】:
我正在尝试编写一个脚本,该脚本应该从文件列表中读取并将每个项目复制到一个新位置,遵循相同的结构。
最初,这是为了复制在 Git 提交范围内更改的文件,但由于我找到了一种更简单的方法(首先是 C#“脚本”,然后是 this),现在我只在学习的兴趣。
这是 file_list.txt 的示例:
config.php
repository\file picker.php
user\edit.php
到目前为止,这是我的 .bat 文件:
@echo off
setlocal enabledelayedexpansion
set "source=input dir"
set "target=output dir"
for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
set "FILE=%%A"
set "dest_file_full=%target%\!FILE:%source%=!"
set "dest_file_filename=%%~nxA"
::set dest_file_dir=(!dest_file_full - !dest_file_filename)
if not exist "!dest_file_dir!" (
md "!dest_file_dir!"
)
set "source_file_full=%source%\!FILE:%source%=!"
copy "!source_file_full!" "!dest_file_dir!"
)
pause
注意注释行set dest_file_dir=(!dest_file_full - !dest_file_filename)。这是伪代码。我需要在这里使用变量替换,但是 cmd.exe 不会一步扩展两个局部变量。
我已经试过管子了……
echo set dest_file_dir=^!dest_file_full:!dest_file_filename!=^! | cmd
或使用call:
call set dest_file_dir=!!dest_file_full:!dest_file_filename!=!!
但两者都行不通。
我在这些方面做错了什么,还是有一种优雅的方式(因此不使用临时文件)来完成这个?
谢谢。
【问题讨论】:
标签: windows loops batch-file for-loop