使用通配符路径重命名文件 [英] Rename Files using wildcard paths

查看:247
本文介绍了使用通配符路径重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始工作,我的第一个任务是编写一个批处理文件,该文件会自动将文件名更改为filename_date并带有原始文件结尾. 为此,您应该能够将路径写入文本文件(例如,paths.txt),并且在启动程序时,它应该从此处取任意一行(= path-> file)并重命名. 我可以在安静的PC上正常运行它,但是在进行测试时,他们要求使用通配符Z:\Path\*.*成为可能. 我当前的代码如下:

Recently I started working and my first task is to write a batch file that automatically changes filenames to filename_date with the original file-ending. For that you should be able to write paths into a textfile (e.g. paths.txt) and when you start the program, it should take any line (=path->file) from there and rename it. I got it to work on my PC quiet well but as I gave it to testing they asked to make the use of wildcards Z:\Path\*.* possible. My current code looks as follows:

@echo off
setlocal EnableDelayedExpansion
cd %~dp0    

For /F "tokens=*" %%m in (paths.txt) do (

set path=%%~dpm
set name=%%~nxm

pushd "!path!"
dir

For /r !path! %%f in (!name!) do (

set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf

set "name=!name!_"
set "name=!name!!date:~6,4!"
set "name=!name!!date:~3,2!"
set "name=!name!!date:~0,2!"

set "name=!name!!ending!"

copy "!datsave!" "!name!"

del "!datsave!"
cls
popd
)

)

我知道很多操作都可能更容易,更高效,但这是我的第一个批处理项目,除了通配符问题外,我很高兴. 因此,一个例子是: C:\Some\Path\*.*

I know that a lot of it is probably easier and more efficient to do, but this is my first batch project and I am quiet happy except for the wildcard problem. So an example would be: C:\Some\Path\*.*

此行将位于path.txt中. 随着分裂

This line would be in paths.txt. With the splitting

set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf

我得到以下信息:

path: C:\Some\Path
name: C:\Some\Path
ending: -empty-
datsave: C:\Some\Path

因为在第一个FOR-Loop的开头将名称设置为Path.但是,如果我不使用通配符,那似乎是可行的.

because name is set to the Path at the start of the first FOR-Loop. But that seems to be working if I do not use wildcards.

现在的问题是:为什么会发生这种情况,我该如何摆脱呢?还是我只使用了错误的通配符类型?

Now the question: Why does this happen and how do I get rid of it? Or do I just use the wrong type of wildcards?

再次:这是我第一次处理批处理,所以可能很简单;)

Again: This is my first time I work with batch, so it might be something simple ;)

推荐答案

Weeeeell首先,再次感谢@Jean-FrançoisFabre和@aschipfl对我的耐心:) 在显示第二个批处理文件的提示之后,我不得不测试一些事情,因为并非一切都正常,但是现在一切都很好!

Weeeeell First of all thanks again to @Jean-François Fabre and @aschipfl for their patience with me :) After the hint with the second batch file I had to test a few things as not everything worked as fine, but now everything works great!

主文件的代码:

@echo off
setlocal EnableDelayedExpansion
cd %~dp0
set DEPTH=20

For /F %%m in (paths.txt) do (
pause
set pth=%%~dpm

  REM pushd !pth!
  REM set origpth=!cd!
  REM popd

set z=%%m
set name=!z!

For /L %%i in (1,1,%DEPTH%) do set 
name=!name:*\=!
set chkname=!name:*\=!
if not !chkname!==!name! ( echo depth to small
pause
exit /B)

rem set name=%%~nxm
pushd "!pth!"

For /r . %%f in (!name!) do (

pushd %~dp0
call renamefiles.bat %%f REM "!origpth!"
popd
)

)

以及子文件的代码:

@echo off   

  REM set pth=%~dp1
  REM set origpth=%2
  REM set origpth=%origpath:"=%\

  REM If !pth!==%origpth% (

set path=%~dp1
set name=%~n1
set ending=%~x1
set datsave=%~nx1

pushd !path!

set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!"

pause

echo renaming "!datsave!" to "!name!"
rem "!datsave!" "!name!"

cls
popd

  REM )

经过我大概的测试,这些子文件夹也包括在内!我在标有REM的代码和两个额外的空格上都添加了额外的代码.删除那些REM,重命名时,程序将不再包含子文件夹:)

After testing around a bit I figured, that subfolders are included as well! I put extra code to both codes marked with REM and two extra spaces. Take out those REM's and the programm will not longer include subfolders when renaming :)

这篇关于使用通配符路径重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆