2

I am attempting to clean-up some file-names in a particular folder and I'm wondering how I would go about creating a batch file to remove a minus sign from the beginning of each file-name.

I have a bunch of files withthe pattern -FileName.pdf and I'd like to remove the minus sign from the front so I just have FileName.pdf.

So far, I have the following command:

dir /B > Batch.txt
for /f "tokens=1,2" %i in (Batch.txt) DO ren "%i %j" %l

Is there anything simpler I could use?

Thanks in advance

  • A safe methodology would be to generate a file with all the ren lines. Personally i'd use grep and sed. linux style commands, with cygwin or gnuwin32. But there are people very good with batch that can stretch it. – barlop Dec 7 '15 at 16:23
  • Variable names have a single % when used in a cmd shell. In a batch you need to double up and use %% – DavidPostill Dec 7 '15 at 17:12
3

I'd like to remove the minus sign from the front

Use the following batch file:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('dir /b -*.pdf') do (
  set _fname=%%i
  echo ren %%i !_fname:~1!
  )

Notes:

  • This will remove the first character from all filenames matching the expression -*.pdf.
  • Replace echo ren with ren when you are sure the batch file will rename correctly.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • dir - Display a list of files and subfolders.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /f - Loop command against the results of another command.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • setlocal - Set options to control the visibility of environment variables in a batch file.
  • Awesome! Thanks David. It worked. I'll look into the reading material. – JohnNg Dec 7 '15 at 18:05
  • 1
    Another way to do it is to not even use ENABLEDELAYEDEXPANSION which I outlined how to do this in a strikingly similiar question to parse out a hypen along with another character of a file name part. superuser.com/questions/1006119/…. This works quite solid and does a simple CALL to a routine/label in the exact same batch script followed by a GOTO EOF to go back to the loop to finish further calls... easy as pie with no need to pass special arguments to batch SETLOCAL to work a non-default way otherwise!! – Pimp Juice IT Dec 8 '15 at 12:41
0

I use Free Commander XE which has a useful Multi-rename facility that lets you preview the results before renaming a bunch of files using a pattern.

  • I appreciate the response. I'm looking to do this using batch commands, so far I have FOR /f "token=1,2 delims=" %A IN ('dir /B') DO ren "%A %B" %B However, it is not working as intended. I keep getting the error message "A was unexpected at this time." Any ideas? Thanks in advance – JohnNg Dec 7 '15 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.