Rename Folders & Files in a Windows System recursi

2019-08-30 19:46发布

问题:

I have the following folder structure:

Top Folder
    -> SubFolder1
        -> MyFolder_Latest
        -> MyFile_Latest.txt
    -> SubFolder2
        -> MyFolder_Latest
        -> MyFile_Latest.txt
    -> SubFolder3
        -> MyFolder_Latest
        -> MyFile_Latest.txt

I wish to change the folders and files in the different SubFolders, given above. All the folders and files have the same name, and I wish to change to the following:

Top Folder
    -> SubFolder1
        -> myfolder
        -> myfile.txt
    -> SubFolder2
        -> myfolder
        -> myfile.txt
    -> SubFolder3
        -> myfolder
        -> myfile.txt

Basically doing two things here: a) Removing the _Latest from all the file and folder names. b) Chnaging the case of all the folders and files to lower case

Anybody has any idea on how to achieve the above in Windows? My system does not allow installation of UNIX and I cannot copy those files to a UNIX system, as our network currently does not provide SCP or FTP permissions on any of the UNIX boxes :-(

Also, if someone has a suitable solution in UNIX, I can try copying the folders to a DVD and trying to run the commands on a UNIX box :-)

回答1:

The folder structure shown does not require recursivity. Just loop through all the folders in the first level of the structure and rename them appropriately.

See HELP FOR

and try this to get you started

@echo off
set TopFolder="c:\temp\Top Folder"
pushd %TopFolder%
for /d %%a in (*.*) do (
  if exist "%%~fa\MyFolder_Latest" echo REN "%%~fa\MyFolder_Latest" myfolder
  if exist "%%~fa\MyFile_Latest.txt" echo REN "%%~fa\MyFile_Latest.txt" myfile.txt
)
popd

after careful testing, remove the ECHO command.