How to rename files in folders to foldername using batch file

我们两清 提交于 2019-11-29 10:24:09

问题



Could any of you help me with a problem I have with a lot of files with the same name, placed in sperate folders.
The folders are named with numbers, but the files inside are named index.XXXX - where XXXX could be .jpg or .html or .pdf.
I would like to make a 'program' that can rename all the 'index' files to the same name as the folder they are placed in.
The folders are named with 6 digit numbers - all different.
After this hopefully has been done, then I would like to move all the renamed files to a new single folder, so it will be easier for me to see the whole content.
Looking forward to reading your answers.


回答1:


@Echo OFF

FOR /D /R %%# in (*) DO (
    PUSHD "%%#"
    FOR %%@ in ("index*") DO (
        Echo Ren: ".\%%~n#\%%@" "%%~n#%%~x@"
        Ren "%%@" "%%~n#%%~x@"
    )
    POPD
)

Pause&Exit

Tested folder structure:

C:\Users\Administrador\Desktop\Nueva carpeta (3)\123321
C:\Users\Administrador\Desktop\Nueva carpeta (3)\123321\Index.txt

C:\Users\Administrador\Desktop\Nueva carpeta (3)\123456
C:\Users\Administrador\Desktop\Nueva carpeta (3)\123456\Index.php

C:\Users\Administrador\Desktop\Nueva carpeta (3)\123456\000000
C:\Users\Administrador\Desktop\Nueva carpeta (3)\123456\000000\Index.css

C:\Users\Administrador\Desktop\Nueva carpeta (3)\654321
C:\Users\Administrador\Desktop\Nueva carpeta (3)\654321\Index.html

C:\Users\Administrador\Desktop\Nueva carpeta (3)\654321\666999
C:\Users\Administrador\Desktop\Nueva carpeta (3)\654321\666999\Index.jpg

Output:

Ren: ".\123321\Index.txt"      "123321.txt"
Ren: ".\123456\Index.php"      "123456.php"
Ren: ".\654321\Index.html"     "654321.html"
Ren: ".\000000\Index.css"      "000000.css"
Ren: ".\666999\Index.jpg"      "666999.jpg"



回答2:


If you want a GUI to do all the above, Use Bulk File Rename.

Open it up, Browse to your folder. Now check the Sub Folders option in the Selections tab. All your files under the folders will be listed. Select the files in the browser.

In the File tab switch the first option from Keep to Remove. Then go to the Append Folder Name tab and set option Prefix for property Name. Click on Rename to complete.

The advantage with this is that it shows you a preview of what your files will be renamed to.




回答3:


I agree with Brandon that the Bulk File Rename (http://www.bulkrenameutility.co.uk/) is a great way to rename/suffix the files with their directory names under Windows.

To copy all the renamed files to a common directory, use Windows Explorer to do a search for all the filenames with a with a wildcard (e.g. readme*.txt). Then select all the found files, select copy, navigate to the target directory, and paste.




回答4:


@ECHO OFF
SETLOCAL enabledelayedexpansion
SET startfrom=c:\sourcedir
SET destdir=c:\destdir
FOR /f "delims=" %%i IN ('dir /s /b "%startfrom%\index.*"') DO (
 FOR %%e IN (jpg html pdf) DO IF /i .%%e==%%~xi (
  FOR %%d IN ("%%~dpi.") DO (
   SET "var=%%~nxd"
   IF "!var:~6!"=="" (
    SET "var="
    SET /a var=1%%~nxd 2>NUL
    IF DEFINED var IF !var! geq 1000000 IF !var! leq 1999999 (
     ECHO MOVE "%%i" "%destdir%\%%~nxd%%~xi"
    )
   )
  )
 )
)
  • Get the 'index.*' files
  • filter on extensions specified
  • find the immediate directory name
  • check that it is not longer than 6 characters
  • attempt to set a variable to 1000000..1999999 if 6-digit name
  • if successful ECHO the move command (remove ECHO keyword to perform move)

Annotated test structure:

c:\sourcedir\index.pdf                not 6-digit dir
c:\sourcedir\000000\index.jpg
c:\sourcedir\000009\index.css         extension not specified
c:\sourcedir\000009\index.html
c:\sourcedir\000009\index.jpg
c:\sourcedir\000009\index.pdf
c:\sourcedir\000009\index.txt         extension not specified
c:\sourcedir\010+39\index.txt         not 6-digit dir
c:\sourcedir\12345\index.jpg          not 6-digit dir
c:\sourcedir\1234567\index.jpg        not 6-digit dir
c:\sourcedir\123x56\index.jpg
c:\sourcedir\50000-0000\index.jpg     not 6-digit dir
c:\sourcedir\5000000000\index.jpg     not 6-digit dir
c:\sourcedir\999-99\index.jpg         not 6-digit dir
c:\sourcedir\999999\index.jpg
c:\sourcedir\a\index.jpg              not 6-digit dir
c:\sourcedir\yellow\index.jpg         not 6-digit dir

Results:

MOVE "c:\sourcedir\000000\index.jpg" "c:\destdir\000000.jpg"
MOVE "c:\sourcedir\000009\index.html" "c:\destdir\000009.html"
MOVE "c:\sourcedir\000009\index.jpg" "c:\destdir\000009.jpg"
MOVE "c:\sourcedir\000009\index.pdf" "c:\destdir\000009.pdf"
MOVE "c:\sourcedir\999999\index.jpg" "c:\destdir\999999.jpg"


来源:https://stackoverflow.com/questions/16266930/how-to-rename-files-in-folders-to-foldername-using-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!