批处理脚本可循环访问以特定字符串开头的目录

问题描述 投票:0回答:1

我当前必须手动编辑批处理文件,以手动告诉它要在哪个目录上工作。

它必须爬网的目录总是以“ 2020-”开头(至少今年是这样),如果我想念一天的话,有时会有多个目录。

((如果更简单,它们将始终采用以下格式:“ 0000-00-00”)

是否有办法对此进行修改以解决该问题?

我曾尝试仅将source_directory设置为“ 2020 *”,但我知道这可能不是这样的,大声笑,朝着正确方向的任何帮助或指针都将是惊人的。

@echo off

pushd %~dp0

SET source_directory=2020-05-03
SET target_directory=%~dp0

for /f %%a IN ('dir "%source_directory%" /b') do (
move %source_directory%\%%a %target_directory%
)
loops for-loop batch-file move
1个回答
0
投票

抱歉,我不太经常使用SO,因此我忘记了需要做些什么。

这是我想出的解决方案。我不认为这是直观的方法,但我对批处理的理解还不够好,没有嵌套循环。

@echo off

pushd %~dp0

SET source_directory=????-??-??
SET target_directory=%~dp0

for /d %%s in (%source_directory%) do (
    for /f %%a IN ('dir "%%s" /b') do (
        move %%s\%%a %target_directory%
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.