Batch file to rename folders

January 12, 2010 at 07:40:29
Specs: Windows XP
I was tasked with cleaning up a system at work and have been trying to automate a batch file that moves through a server directory and renames folder based on '-XYZ' in the name of the folder, and remove the '-XYZ'.

Prior to a new software implementation the end users had control of the file system and would change folders at whim, now the software creates and maintains the folders, but I need to go in and clean up the folders changed by the end users who had a habit of adding thier initials after the fiolder name.
Example: 'folder1-XYZ' needs to then be turned back into 'folder1' for the software system to find it...
The problem lies in the fact that I would need to go though subfolders of subfolders of subfolders to even get to the ones that may be changed. For example; Root has 5 folders, each of those 5 folders have multiple subfolders (6-10 depending on location) and each of those hundreds of subfolders, and finally each of those hundreds of subfolders have hundreds - thousands of subfolders for a given unit. Here is how I setup a test folder structure on my machine when I was tring to buiild something that I couldn't get to work: C:\bat_test\home\one\loc1\type1\unit5-JOS in that example the unit5 folder would need to be renamed to have the -JOS removed.

I have attempted to do this via REM and XCopy commands but can only get it to work if I tell it to do one folder at a time and give it a direct path to that folder with the full folder name. IS there a way to get ti to look though the directory for anything with '-XYZ' and then remove the '-XYZ' from the name?

See More: Batch file to rename folders

Report •

January 13, 2010 at 07:19:27
Warning: Batch not tested

@echo off & setlocal
for /R C:\ %%j in (.) do call :REN "%%j"
goto :EOF

  set folder=%~dpn1
  if not "%folder:~-3,1%"=="-" goto :EOF
  move "%folder%" "%folder:~0,-3%"
  echo."%folder%" -^> "%folder:~0,-3%"
  goto :EOF
:: End_Of_Batch

Report •

January 14, 2010 at 03:38:54
I revised and corrected the script I posted above as I coded it in a rush before leaving. I add the exclusion of system folders as scanning them may be harmful. If the folders to be renamed are subfolders of a specific directory, replace C:\ with that directory to speed up the scanning.

@echo off & setlocal
for /R C:\ %%j in (.) do call :REN "%%j"
goto :EOF

  set folder=%~dpn1
  if not "%folder:Program Files=%"=="%folder%" goto :EOF
  if not "%folder:WinNT=%"=="%folder%" goto :EOF
  if not "%folder:Windows=%"=="%folder%" goto :EOF
  if not "%folder:~-4,1%"=="-" goto :EOF
  move "%folder%" "%folder:~0,-4%"
  echo."%folder%" -^> "%folder:~0,-4%"
  goto :EOF
:: End_Of_Batch

Report •

January 30, 2010 at 09:04:42
I think IVO already provided the answer. Here is an alternate script - it will rename all files by removing $pattern from the names of files in top directory $topdir.

# Script DropPattern.txt
var str topdir, pattern, list, file, name
echo > syscmds.bat
lf -r -n ("*"+$pattern+"*") $topdir > $list
while ($list <> "")
    lex "1" $list > $file
    stex -p "^/^l["  $file > $name
    sal ("^"+$pattern+"^") "" $name > $name
    echo ("rename \""+$file+"\" \""+$name+"\"") >> syscmds.bat
system syscmds.bat

Script is in biterscripting ( ). Save it in file C:/Scripts/DropPattern.txt, run it as

script DropPattern.txt topdir("/path/to/top folder") pattern("-JOS")

Will rename all "abc-JOSxyz" files under top folder "/path/to/top folder" to just "abcxyz". Test it first on a test folder.

Report •
Related Solutions

Ask Question