0

i am trying to remove prefix from all files in current folder and subfolders i tryed following code which work only for current folder

setlocal enabledelayedexpansion
for %%F in (*) do (
  set "FN=%%F"
  set "FN=!FN:~15!"
  ren "%%F" "!FN!"
)
goto :eof

Please Help me to solve this

Nilesh12
  • 99
  • 1
  • 2
  • 15

2 Answers2

1
for /f "delims=" %%a in ('dir /b /a-d /s') do (
    set "fname=%%~nxa"
    set "fpath=%%~dpa"
    setlocal enabledelayedexpansion
    set "nname=!fname:~15!"
    ren "!fpath!!fname!" "!nname!"
    endlocal
)

This is the safe way to preserve exclamation marks.

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • 1
    It does work, but it renames itself if it is in the same folder tree. Like the previous renaming solution, add `pushd "c:\myfolder\"` (with your main folder in it) to the top and launch it from another folder. – foxidrive Oct 14 '13 at 06:28
-1

If you are using windows 7, you could try this:

forfiles /s /c "cmd /c ren @file @fname"

It took me a bit of time to find, but suddenly I realised that the batch file was not working because it had renamed itself!.

If this becomes an issue you could try naming the batch file zzzzzzzzz.bat which I think would prevent it from renaming itself first.

Mona

Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • It will still get renamed as it is in the top level folder. It doesn't remove the prefix, and it doesn't handle files in folders, and it renames folder names. Apart from all that it's fine. ;) – foxidrive Oct 14 '13 at 06:32
  • it's usefull for renaming folder but not for removing prefix of all files from current dir and subdir – Nilesh12 Oct 14 '13 at 06:32
  • I had the benefit of reading and answering the previous post of this user, where the task was to add a prefix to all files in a tree. Now he wants to remove that prefix. – foxidrive Oct 14 '13 at 06:35
  • yes bro i'm really thankfull for your help i just want to know the viseversa solution that's all – Nilesh12 Oct 14 '13 at 06:36
  • Mona, you didn't test this in a folder with subdirectories (with periods in the name) and files in a tree. – foxidrive Oct 14 '13 at 07:10
  • @foxidrive I tested it with files in a tree but not with subdirectories with periods in it, but that can be fixed with: `forfiles /s /c "cmd /c if @isdir==FALSE ren @file @fname"` and that should work fine. – Monacraft Oct 14 '13 at 08:21
  • It tries to rename files in a subdirectory using the filename only. How can that possibly work? You haven't actually realised that the task is to remove the leading 15 characters from each filename. Your code output has no resemblance to the desired task. – foxidrive Oct 14 '13 at 08:24