Rename Files using wildcard paths

geisterfurz007

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
)

)

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\*.*

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

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

I get the following:

path: C:\Some\Path
name: C:\Some\Path
ending: -empty-
datsave: C:\Some\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 ;)

geisterfurz007

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!

Code of the Main file:

@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
)

)

And the code of the sub-file:

@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 )

EDIT: 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 :)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Using rename to rename files and directories

分類Dev

Delete and rename files using unlink() and rename()

分類Dev

Rename files, using text-file as source

分類Dev

List files on SFTP server matching wildcard in Python using Paramiko

分類Dev

Listing numbered files using wildcard sequence with predefined range

分類Dev

Rename files in a folder using Python, using name map from Excel

分類Dev

How to copy local files AND PATHS to network share using PowerShell?

分類Dev

Using Total Commander search to find paths within files

分類Dev

find and copy files using input list and partial file paths

分類Dev

find and copy files using input list and partial file paths

分類Dev

How does one rename multiple files using python?

分類Dev

Copy and rename files recursively using part of folder name for new name

分類Dev

How to rename files with count?

分類Dev

Rename multiple files randomly

分類Dev

How to rename files with count?

分類Dev

Efficient way to rename files

分類Dev

How To Edit/Add/Delete/Rename Files/Folders In Ubuntu Server Using FileZilla?

分類Dev

AWK, SED, REGEX to rename files

分類Dev

Looping two files (Rename and Move)

分類Dev

How to rename the set of files with pattern

分類Dev

Batch rename files to a sequential numbering

分類Dev

How to rename files with different extensions

分類Dev

Filter collection using like and wildcard

分類Dev

SQL: Using <= and >= to compare string with wildcard

分類Dev

In Scala find files that match a wildcard String

分類Dev

Using relative paths for Windows shortcuts

分類Dev

How to change paths of multiple hyperlink files in excel?

分類Dev

batch rename using windows powershell

分類Dev

Problem with "rename" using regex (Linux)

Related 関連記事

  1. 1

    Using rename to rename files and directories

  2. 2

    Delete and rename files using unlink() and rename()

  3. 3

    Rename files, using text-file as source

  4. 4

    List files on SFTP server matching wildcard in Python using Paramiko

  5. 5

    Listing numbered files using wildcard sequence with predefined range

  6. 6

    Rename files in a folder using Python, using name map from Excel

  7. 7

    How to copy local files AND PATHS to network share using PowerShell?

  8. 8

    Using Total Commander search to find paths within files

  9. 9

    find and copy files using input list and partial file paths

  10. 10

    find and copy files using input list and partial file paths

  11. 11

    How does one rename multiple files using python?

  12. 12

    Copy and rename files recursively using part of folder name for new name

  13. 13

    How to rename files with count?

  14. 14

    Rename multiple files randomly

  15. 15

    How to rename files with count?

  16. 16

    Efficient way to rename files

  17. 17

    How To Edit/Add/Delete/Rename Files/Folders In Ubuntu Server Using FileZilla?

  18. 18

    AWK, SED, REGEX to rename files

  19. 19

    Looping two files (Rename and Move)

  20. 20

    How to rename the set of files with pattern

  21. 21

    Batch rename files to a sequential numbering

  22. 22

    How to rename files with different extensions

  23. 23

    Filter collection using like and wildcard

  24. 24

    SQL: Using <= and >= to compare string with wildcard

  25. 25

    In Scala find files that match a wildcard String

  26. 26

    Using relative paths for Windows shortcuts

  27. 27

    How to change paths of multiple hyperlink files in excel?

  28. 28

    batch rename using windows powershell

  29. 29

    Problem with "rename" using regex (Linux)

ホットタグ

アーカイブ