-4

I have a folder which contains for example the following files:

RAW_123432542_343.text
231453254_213.text
RAW_324324_32432423.text
32432423_4543.text

What I need is renaming all files which do not have RAW_ as prefix. The folder has thousands of files.

How to rename all files within the folder not starting with RAW_?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Alain
  • 3
  • 1
  • 1
    There is a similar question, which may help you: http://superuser.com/questions/699711/rename-multiple-files-exclude-pattern – Freddy Aug 18 '16 at 12:46
  • StackOverflow is not a free code writing service, so you need to show your own efforts if you expect help. Please lean [how to ask](http://stackoverflow.com/help/how-to-ask)! – aschipfl Aug 18 '16 at 12:47
  • Please note that https://stackoverflow.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 18 '16 at 15:22
  • so far this is what i achieve ... but not same with strng compare @echo off for %%a in (*) do (set fname=%%a) & call :rename goto :eof :rename IF "%fname:~0,4%"=="RAW_" – Alain Aug 18 '16 at 22:33

1 Answers1

0

This could be done for example with following batch code:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("C:\Temp\*.text") do (
    set "FileName=%%~nI"
    if /I not "!FileName:~0,4!" == "RAW_" ren "%%~I" "RAW_%%~nxI"
)
endlocal

If a string is assigned to an environment variable within a code block starting with ( and ending with a matching ) and referencing the string or parts of the string of this environment variable within same code block is necessary as for this rename operation, delayed expansion must be used as shown above. The help of command set explains this on a simple IF and a simple FOR example very clear.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • ren /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • i tried something `if /I "!FileName:~0,4!" == "RAW_" ren "%%~I" "!FileName:~4!"` but it only rename first type of files.. and it removes the extensions – Alain Aug 22 '16 at 00:22
  • You need the command line `if /I "!FileName:~0,4!" == "RAW_" ren "%%~I" "!FileName:~4!%%~xI"` if you want to make the opposite and rename files with `RAW_` in any case at beginning of *file name* by removing `RAW_` from *file name*. The string assigned to environment variable `FileName` is `"%%~nI"` which is not the __name of the file__ (file name + file extension), but just the *file name* without file extension. Of course you could assign to the variable also `"%%~nxI"` because then above batch script as well as your __IF__ condition for the opposite would work both. – Mofi Aug 22 '16 at 08:28
  • wow.. so that's explain why.. so thankful of your help.. it ease my work so much.. – Alain Aug 22 '16 at 10:53